Sha256: c3178e1b043d1f6e9788afe2f3e4226633c50b19034fcdb27475e6a404bd3352

Contents?: true

Size: 1019 Bytes

Versions: 2

Compression:

Stored size: 1019 Bytes

Contents

require 'mongoid/i18n/localized_field'
require 'mongoid/i18n/criterion/selector'

module Mongoid
  module I18n
    extend ActiveSupport::Concern

    module ClassMethods
      def localized_field(name, options = {})
        field name, options.merge(:type => LocalizedField)
      end

      protected
      def create_accessors(name, meth, options = {})
        if options[:type] == LocalizedField
          define_method(meth) { read_attribute(name)[::I18n.locale.to_s] rescue '' }
          define_method("#{meth}=") do |value|
            value = if value.is_a?(Hash)
              (@attributes[name] || {}).merge(value)
            else
              (@attributes[name] || {}).merge(::I18n.locale.to_s => value)
            end
            write_attribute(name, value)
          end
          define_method("#{meth}_translations") { read_attribute(name) }
          define_method("#{meth}_translations=") { |value| write_attribute(name, value) }
        else
          super
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_i18n-0.2.1 lib/mongoid/i18n.rb
mongoid_i18n-0.2.0 lib/mongoid/i18n.rb