Sha256: 6157e18806cf48dee83c8ee27394311533814b898618e1e6e5d3fdc37ce6c8e2

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require "has_translatable_attributes/version"

module HasTranslatableAttributes

  module Extensions

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def has_translatable_attributes(options = {})

        column_names.select{|name| name =~ /_#{I18n.default_locale}$/}.each do |name_i18n|
          name = name_i18n.sub(/_#{I18n.default_locale}$/, '')
          next if columns_hash.key? name

          class_eval <<-"EOV"

            class << self
              def find_by_#{name}(*arguments)
                send("find_by_#{name}_#\{I18n.locale\}", *arguments)
              end
            end

            def #{name}
              if (val = send("#{name}_#\{I18n.locale\}")).blank? && I18n.locale != I18n.default_locale
                send("#{name}_#\{I18n.default_locale\}")
              else
                val
              end
            end

            def #{name}=(*arguments, &block)
              send("#{name}_#\{I18n.locale\}=", *arguments, &block)
            end
          EOV
        end

      end
    end 

    module InstanceMethods
    end

  end
end

ActiveRecord::Base.send :include, HasTranslatableAttributes::Extensions

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
has_translatable_attributes-0.0.2 lib/has_translatable_attributes.rb
has_translatable_attributes-0.0.1 lib/has_translatable_attributes.rb