Sha256: 99ec4be56b435c9368434192636432f003bc2f9825b0a5c76e2f2734bac54f10

Contents?: true

Size: 1.29 KB

Versions: 29

Compression:

Stored size: 1.29 KB

Contents

module Mobility
  module Plugins
=begin

Adds translated attribute names and values to the hash returned by #attributes.
Also adds a method #translated_attributes with names and values of translated
attributes only.

@note Adding translated attributes to +attributes+ can have unexpected
  consequences, since these attributes do not have corresponding columns in the
  model table. Using this plugin may lead to conflicts with other gems.

=end
    module AttributeMethods
      extend Plugin

      default true
      requires :attributes

      initialize_hook do |*names|
        include InstanceMethods

        define_method :translated_attributes do
          super().merge(names.inject({}) do |attributes, name|
            attributes.merge(name.to_s => send(name))
          end)
        end
      end

      # Applies attribute_methods plugin for a given option value.
      included_hook do
        if options[:attribute_methods]
          define_method :untranslated_attributes, ::ActiveRecord::Base.instance_method(:attributes)
        end
      end

      module InstanceMethods
        def translated_attributes
          {}
        end

        def attributes
          super.merge(translated_attributes)
        end
      end
    end

    register_plugin(:attribute_methods, AttributeMethods)
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
mobility-1.3.1 lib/mobility/plugins/attribute_methods.rb
mobility-1.3.0 lib/mobility/plugins/attribute_methods.rb
mobility-1.3.0.rc3 lib/mobility/plugins/attribute_methods.rb
mobility-1.3.0.rc2 lib/mobility/plugins/attribute_methods.rb
mobility-1.3.0.rc1 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.9 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.8 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.7 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.6 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.5 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.4 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.3 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.2 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.1 lib/mobility/plugins/attribute_methods.rb
mobility-1.2.0 lib/mobility/plugins/attribute_methods.rb
mobility-1.1.3 lib/mobility/plugins/attribute_methods.rb
mobility-1.1.2 lib/mobility/plugins/attribute_methods.rb
mobility-1.1.1 lib/mobility/plugins/attribute_methods.rb
mobility-1.1.0 lib/mobility/plugins/attribute_methods.rb
mobility-1.0.7 lib/mobility/plugins/attribute_methods.rb