Sha256: fb101e81ab0bd640eca7c8bd4c011296fbaf8838593477195ab4218eb08d1e4c

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Mobility
  module Plugins
    module ActiveRecord
      module TranslatedAttributes
        def translated_attributes
          {}
        end

        def attributes
          super.merge(translated_attributes)
        end
      end

=begin

Module builder adding translated attributes to #attributes hash on model
instance. See {Mobility::Plugins::AttributeMethods} for further details.

=end
      class AttributeMethods < Module
        def initialize(*attribute_names)
          include TranslatedAttributes
          define_method :translated_attributes do
            super().merge(attribute_names.inject({}) do |attributes, name|
              attributes.merge(name.to_s => send(name))
            end)
          end
          delegate :translated_attribute_names, to: :class
        end

        def included(model_class)
          model_class.class_eval do
            define_method :untranslated_attributes, ::ActiveRecord::Base.instance_method(:attributes)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mobility-0.3.3 lib/mobility/plugins/active_record/attribute_methods.rb