Sha256: e6e131176dd85baab1f8e2b9303a6ca1fc7c9d6d893c754caf3963be2930d4e3

Contents?: true

Size: 684 Bytes

Versions: 1

Compression:

Stored size: 684 Bytes

Contents

require 'active_support/concern'

module Translatable
  module ActsAsTranslatable
    extend ActiveSupport::Concern

    module ClassMethods
      def translatable(column)
        key_name = column.to_s.pluralize.to_sym
        has_many key_name, -> { where(key: column.to_s) }, :as => :translatable, class_name: 'TranslatedWord',
        :inverse_of => :translatable, :dependent => :destroy
        accepts_nested_attributes_for key_name
        define_method column do |locale = I18n.default_locale|
          localizes = send(key_name)
          localizes.detect{|w| w.locale.to_s == locale.to_s }.try(:value) || localizes.first.try(:value)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar-translatable-0.1.0 lib/translatable/acts_as_translatable.rb