Sha256: a45d40651a0ddb12237cd562dd71d404acd00ec8377a9ca0a5e27c3ba86aa88c

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 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

      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

2 entries across 2 versions & 1 rubygems

Version Path
mobility-1.0.0.beta1 lib/mobility/plugins/attribute_methods.rb
mobility-1.0.0.alpha lib/mobility/plugins/attribute_methods.rb