Sha256: 893f03cae863803713aefcdc1a640a07bccfa367bf8756c3139b167db5adeb56

Contents?: true

Size: 965 Bytes

Versions: 6

Compression:

Stored size: 965 Bytes

Contents

module Mobility
  module ActiveRecord
=begin

Included into model if model has +ActiveRecord::AttributeMethods+ among its
ancestors.

=end
    module AttributeMethods
      delegate :translated_attribute_names, to: :class

      # Adds translated attributes to +attributes+.
      # @return [Array<String>] Model attributes
      # @!method attributes
      def self.included(model)
        attributes_method = Module.new do
          def attributes
            super.merge(translated_attributes)
          end
        end
        model.class_eval do
          alias_method :untranslated_attributes, :attributes
          include attributes_method
        end
      end

      # Translated attributes defined on model.
      # @return [Array<String>] Translated attributes
      def translated_attributes
        translated_attribute_names.inject({}) do |attributes, name|
          attributes.merge(name.to_s => send(name))
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mobility-0.2.3 lib/mobility/active_record/attribute_methods.rb
mobility-0.2.2 lib/mobility/active_record/attribute_methods.rb
mobility-0.2.1 lib/mobility/active_record/attribute_methods.rb
mobility-0.2.0 lib/mobility/active_record/attribute_methods.rb
mobility-0.1.20 lib/mobility/active_record/attribute_methods.rb
mobility-0.1.19 lib/mobility/active_record/attribute_methods.rb