<commit_before>module Alchemy
  class Site < ActiveRecord::Base
    cattr_accessor :current

    attr_accessible :host, :name, :public

    # validations
    validates_presence_of :host
    validates_uniqueness_of :host

    # associations
    has_many :languages

    scope :published, where(public: true)

    # Returns true if this site is the current site
    def current?
      self.class.current == self
    end

    class << self
      def default
        Site.first
      end
    end

    before_create do
      # If no languages are present, create a default language based
      # on the host app's Alchemy configuration.

      if languages.empty?
        default_language = Alchemy::Config.get(:default_language)
        languages.build(
          name:           default_language['name'],
          language_code:  default_language['code'],
          frontpage_name: default_language['frontpage_name'],
          page_layout:    default_language['page_layout'],
          public:         true,
          default:        true
        )
      end
    end
  end
end<commit_msg>Increased thread-safety for Site.current.<commit_after>module Alchemy
  class Site < ActiveRecord::Base
    attr_accessible :host, :name, :public

    # validations
    validates_presence_of :host
    validates_uniqueness_of :host

    # associations
    has_many :languages

    scope :published, where(public: true)

    # Returns true if this site is the current site
    def current?
      self.class.current == self
    end

    class << self
      def current=(v)
        Thread.current[:alchemy_current_site] = v
      end

      def current
        Thread.current[:alchemy_current_site]
      end

      def default
        Site.first
      end
    end

    before_create do
      # If no languages are present, create a default language based
      # on the host app's Alchemy configuration.

      if languages.empty?
        default_language = Alchemy::Config.get(:default_language)
        languages.build(
          name:           default_language['name'],
          language_code:  default_language['code'],
          frontpage_name: default_language['frontpage_name'],
          page_layout:    default_language['page_layout'],
          public:         true,
          default:        true
        )
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07035607035607036, 'fraction_numerical': 0.0, 'mean_word_length': 2.229916897506925, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1044145', 'n_tokens_mistral': 651, 'n_tokens_neox': 621, 'n_words': 175}