Sha256: e52c1e71a8b5c6e688a00efdd644d5645b5128994a2b7d2583d2ecfbcf811b7c

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'mongoid/i18n/localized_field'
require 'mongoid/i18n/localized_criteria'

module Mongoid
  module I18n
    extend ActiveSupport::Concern

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

      def criteria
        scope = scope_stack.last rescue nil
        scope || I18n::LocalizedCriteria.new(self)
      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|
            if value.is_a?(Hash)
              val = (@attributes[name] || {}).merge(value)
            else
              val = (@attributes[name] || {}).merge(::I18n.locale.to_s => value)
            end
            write_attribute(name, val)
          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

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_i18n-0.1.7 lib/mongoid/i18n.rb