Sha256: 2aff3878bc13c53e1eaadd734339c08c63a0447fce98d6c2f7b3b9473462fb62
Contents?: true
Size: 1.8 KB
Versions: 1
Compression:
Stored size: 1.8 KB
Contents
module TranslatableRecords module ActiveRecord module Base extend ActiveSupport::Concern module ClassMethods def translatable? translatable_attrs.any? end def translatable_attrs @translatable_attrs ||= [] end def attr_translatable(*args) make_translatable unless translatable? args.each do |name| define_translatable_attribute_methods name translatable_attrs << name end end protected def make_translatable include TranslatableRecords::ActiveRecord::Translatable default_scope -> { includes(:translations) } attr_accessible :translations_attributes if Rails::VERSION::MAJOR < 4 has_many :translations, class_name: "#{name}Translation", autosave: true, dependent: :destroy accepts_nested_attributes_for :translations end def define_translatable_attribute_methods(attr) ['setter', 'getter'].each { |method| send "define_translatable_attribute_#{method}", attr } end def define_translatable_attribute_setter(attr) name = :"#{attr}=" define_method name do |value| t = translation_by_locale(current_locale) t ? t.send(name, value) : translations.build(locale: current_locale.to_s, attr.to_sym => value) end end def define_translatable_attribute_getter(attr) ['', '_was', '_changed?'].each do |suffix| name = :"#{attr}#{suffix}" define_method name do t = translation_by_locale(current_locale) t ? t.send(name) : nil end end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
translatable_records-1.0.7 | lib/translatable_records/active_record/base.rb |