Module: Perfectline::T9n::TranslatableAttribute::ClassMethods
- Defined in:
- lib/t9n/active_record/translatable_attribute.rb
Instance Method Summary collapse
- #build_translated_attribute(attribute, options, real_attribute) ⇒ Object
- #translate_attribute(attribute, options = {}) ⇒ Object
- #translate_column(column, options = {}) ⇒ Object
- #translated_attributes ⇒ Object
Instance Method Details
#build_translated_attribute(attribute, options, real_attribute) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/t9n/active_record/translatable_attribute.rb', line 32 def build_translated_attribute(attribute, , real_attribute) # build key key = "#{self.table_name}.#{((.delete(:to) || attribute).to_s.gsub(/_/, "."))}" default = .delete(:default) || "#{attribute}_default" # add the attribute name, default and type translated_attributes.store(attribute, { :key => key, :default => default, :real_attribute => real_attribute }) # create a separate method for accessing the original column if real_attribute define_method(default) do self[attribute.to_sym] end define_method("#{default}=") do |value| self[attribute.to_sym] = value end end define_method(attribute) do read_translatable_attribute(attribute, I18n.locale.to_s) end define_method("#{attribute}=") do |value| write_translatable_attribute(attribute, value, I18n.locale.to_s) end unless self.base_class.reflect_on_association(:translations) has_many :translations, :as => :translatable, :dependent => :destroy, :autosave => true end end |
#translate_attribute(attribute, options = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/t9n/active_record/translatable_attribute.rb', line 16 def translate_attribute(attribute, = {}) if columns_hash.keys.include?(attribute.to_s) raise ArgumentError, "#{self.name} already has an attribute named :#{attribute}" end build_translated_attribute(attribute, , false) end |
#translate_column(column, options = {}) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/t9n/active_record/translatable_attribute.rb', line 24 def translate_column(column, = {}) unless columns_hash.keys.include?(column.to_s) raise ArgumentError, "#{self.name} does not respond to column: :#{column}" end build_translated_attribute(column, , true) end |
#translated_attributes ⇒ Object
12 13 14 |
# File 'lib/t9n/active_record/translatable_attribute.rb', line 12 def translated_attributes @translated_attributes ||= {} end |