<commit_before>require 'active_record'
require 'translation'

module IsTranslatable
  module ActiveRecordExtension
    def is_translatable(*kinds)
      include IsTranslatable::Methods
    end
  end

  module Methods
    def self.included(klass)
      klass.class_eval do
        include IsTranslatable::Methods::InstanceMethods

        has_many :translations, :as => :translatable, :dependent => :destroy
        accepts_nested_attributes_for :translations
      end
    end

    module InstanceMethods
      def set_translation(kind, t, locale=nil)
        locale ||= I18n.locale
        translations.build({:kind => kind, :translation => t, :locale => locale})
      end
  
      def get_translation(kind, locale=nil)
        translations.find_by_kind(kind, :conditions => {:locale => locale})
      end
    end
  end
end

ActiveRecord::Base.send(:extend, IsTranslatable::ActiveRecordExtension)<commit_msg>Actually get the set/get translation methods working<commit_after>require 'active_record'
require 'translation'

module IsTranslatable
  module ActiveRecordExtension
    def is_translatable(*kinds)
      include IsTranslatable::Methods
    end
  end

  module Methods
    def self.included(klass)
      klass.class_eval do
        include IsTranslatable::Methods::InstanceMethods

        has_many :translations, :as => :translatable, :dependent => :destroy, :autosave => true
        accepts_nested_attributes_for :translations
      end
    end

    module InstanceMethods
      def set_translation(kind, t, locale=nil)
        locale ||= I18n.locale
        translations.build({:kind => kind.to_s, :translation => t, :locale => locale.to_s})
      end
  
      def get_translation(kind, locale=nil)
        locale ||= I18n.locale
        t = translations.find_by_kind(kind.to_s, :conditions => {:locale => locale.to_s})
        t ||= find_translation(kind, locale)
        t.nil? ? '' : t.translation
      end

    protected
      def find_translation(kind, locale)
        translations.each do |t|
          return t if t.kind == kind.to_s && t.locale == locale.to_s
        end
        nil
      end
    end
  end
end

ActiveRecord::Base.send(:extend, IsTranslatable::ActiveRecordExtension)
{'fraction_non_alphanumeric': 0.09383499546690843, 'fraction_numerical': 0.0027198549410698096, 'mean_word_length': 3.1022304832713754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '7622196', 'n_tokens_mistral': 651, 'n_tokens_neox': 639, 'n_words': 152}