Sha256: a1c2305691a1e9097bb6720a7f2920d4b912605a8ec5376999d1b688c1d221c0

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8

module PagesCore
  module Localizable
    # = Localizable::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
      # Localizable::Configuration.
      #
      # Example:
      #
      #  class Page < ActiveRecord::Base
      #    localizable do
      #      attribute :name
      #      attribute :body
      #    end
      #  end
      #
      def localizable(&block)
        unless self.is_a?(Localizable::ClassMethods)
          send :extend,  Localizable::ClassMethods
          send :include, Localizable::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
end

ActiveRecord::Base.send(:extend, PagesCore::Localizable::ActiveRecordExtension)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_core-3.4.3 lib/pages_core/localizable/active_record_extension.rb
pages_core-3.4.2 lib/pages_core/localizable/active_record_extension.rb