<commit_before># == Schema Information
#
# Table name: alchemy_languages
#
#  id             :integer          not null, primary key
#  name           :string(255)
#  language_code  :string(255)
#  frontpage_name :string(255)
#  page_layout    :string(255)      default("intro")
#  public         :boolean          default(FALSE)
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  creator_id     :integer
#  updater_id     :integer
#  default        :boolean          default(FALSE)
#  country_code   :string(255)      default(""), not null
#  site_id        :integer
#

module Alchemy
  class Language < ActiveRecord::Base
    validates_presence_of :name
    validates_presence_of :language_code
    validates_presence_of :page_layout
    validates_presence_of :frontpage_name
    validates_uniqueness_of :language_code, :scope => [:site_id, :country_code]
    validate :presence_of_default_language
    validate :publicity_of_default_language
    has_many :pages
    belongs_to :site
    after_destroy :delete_language_root_page
    validates_format_of :language_code, with: /\A[a-z]{2}\z/, if: -> { language_code.present? }
    validates_format_of :country_code, with: /\A[a-z]{2}\z/, if: -> { country_code.present? }
    before_destroy :check_for_default
    after_update :set_pages_language, :if => proc { |m| m.language_code_changed? || m.country_code_changed? }
    after_update :unpublish_pages, :if => proc { changes[:public] == [true, false] }
    before_save :remove_old_default, :if => proc { |m| m.default_changed? && m != Language.get_default }

    scope :published,          -> { where(public: true) }
    scope :with_language_root, -> { joins(:pages).where('alchemy_pages' => {language_root: true}) }
    scope :on_site,            ->(s) { s.present? ? where(site_id: s) : all }
    default_scope { on_site(Site.current) }

    class << self

      # Returns all languages for which a language root page exists.
      def all_for_created_language_trees
        # don't use 'find' here as it would clash with our default_scopes
        # in various unholy ways you don't want to find out about.
        where(id: Page.language_roots.collect(&:language_id))
      end

      def get_default
        find_by(default: true)
      end

    end

    def label(attrib)
      if attrib.to_sym == :code
        self.code
      else
        I18n.t(self.code, :default => self.name)
      end
    end

    include Code

    private
<commit_msg>Use pluck instead of collect in `Language. all_for_created_language_trees`.<commit_after># == Schema Information
#
# Table name: alchemy_languages
#
#  id             :integer          not null, primary key
#  name           :string(255)
#  language_code  :string(255)
#  frontpage_name :string(255)
#  page_layout    :string(255)      default("intro")
#  public         :boolean          default(FALSE)
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  creator_id     :integer
#  updater_id     :integer
#  default        :boolean          default(FALSE)
#  country_code   :string(255)      default(""), not null
#  site_id        :integer
#

module Alchemy
  class Language < ActiveRecord::Base
    validates_presence_of :name
    validates_presence_of :language_code
    validates_presence_of :page_layout
    validates_presence_of :frontpage_name
    validates_uniqueness_of :language_code, :scope => [:site_id, :country_code]
    validate :presence_of_default_language
    validate :publicity_of_default_language
    has_many :pages
    belongs_to :site
    after_destroy :delete_language_root_page
    validates_format_of :language_code, with: /\A[a-z]{2}\z/, if: -> { language_code.present? }
    validates_format_of :country_code, with: /\A[a-z]{2}\z/, if: -> { country_code.present? }
    before_destroy :check_for_default
    after_update :set_pages_language, :if => proc { |m| m.language_code_changed? || m.country_code_changed? }
    after_update :unpublish_pages, :if => proc { changes[:public] == [true, false] }
    before_save :remove_old_default, :if => proc { |m| m.default_changed? && m != Language.get_default }

    scope :published,          -> { where(public: true) }
    scope :with_language_root, -> { joins(:pages).where('alchemy_pages' => {language_root: true}) }
    scope :on_site,            ->(s) { s.present? ? where(site_id: s) : all }
    default_scope { on_site(Site.current) }

    class << self

      # Returns all languages for which a language root page exists.
      def all_for_created_language_trees
        # don't use 'find' here as it would clash with our default_scopes
        # in various unholy ways you don't want to find out about.
        where(id: Page.language_roots.pluck(:language_id))
      end

      def get_default
        find_by(default: true)
      end

    end

    def label(attrib)
      if attrib.to_sym == :code
        self.code
      else
        I18n.t(self.code, :default => self.name)
      end
    end

    include Code

    private

{'fraction_non_alphanumeric': 0.1025183422565933, 'fraction_numerical': 0.007535197303192544, 'mean_word_length': 2.8979907264296756, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '2243379', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1589, 'n_words': 383}