Sha256: 2c643ae281f5e4bcc4631477517c9afe2cee7252900e27a9dafb1f2914d17c80

Contents?: true

Size: 1.05 KB

Versions: 9

Compression:

Stored size: 1.05 KB

Contents

# encoding: utf-8

module LocalizableModel
  # = LocalizableModel::ActiveRecordExtension
  #
  # Extends ActiveRecord::Base with the localizable setup method.
  #
  module ActiveRecordExtension
    # Extends the model with Localizable features.
    # It takes an optional block as argument, which yields an instance of
    # LocalizableModel::Configuration.
    #
    # Example:
    #
    #  class Page < ActiveRecord::Base
    #    localizable do
    #      attribute :name
    #      attribute :body
    #    end
    #  end
    #
    def localizable(&block)
      unless is_a?(LocalizableModel::ClassMethods)
        send :extend,  LocalizableModel::ClassMethods
        send :include, LocalizableModel::InstanceMethods
        has_many(:localizations,
                 as: :localizable,
                 dependent: :destroy,
                 autosave: true)
        before_save :cleanup_localizations!
      end
      localizable_configuration.instance_eval(&block) if block_given?
    end
  end
end

ActiveRecord::Base.send(:extend, LocalizableModel::ActiveRecordExtension)

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
localizable_model-0.6.1 lib/localizable_model/active_record_extension.rb
localizable_model-0.6.0 lib/localizable_model/active_record_extension.rb
localizable_model-0.5.5 lib/localizable_model/active_record_extension.rb
localizable_model-0.5.4 lib/localizable_model/active_record_extension.rb
localizable_model-0.5.3 lib/localizable_model/active_record_extension.rb
localizable_model-0.5.2 lib/localizable_model/active_record_extension.rb
localizable_model-0.5.1 lib/localizable_model/active_record_extension.rb
localizable_model-0.5.0 lib/localizable_model/active_record_extension.rb
localizable_model-0.0.1 lib/localizable_model/active_record_extension.rb