Sha256: a730efc408f09b938ed5218d68acbdc46f1e56a57964ee882a4b86990d69a56d

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module PropertySets
  module Delegator
    # methods for moving what was once a literal column on
    # to a property_set table.
    #
    # delegates read, write and query methods to the property record or the property default
    #
    # Examples
    #
    #   # Migrate :is_open to the :settings property set, and rename it :open,
    #   # and migrate :same to property set :same
    #   include PropertySets::Delegator
    #   delegate_to_property_set :settings, :is_open => :open, :same => :same
    #
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def delegate_to_property_set(setname, mappings)
        raise "Second argument must be a Hash" unless mappings.is_a?(Hash)

        mappings.each do |old_attr, new_attr|
          define_method(old_attr) { send(setname).send(new_attr) }
          alias_method "#{old_attr}_before_type_cast", old_attr
          define_method("#{old_attr}?") { send(setname).send("#{new_attr}?") }
          define_method("#{old_attr}=") { |value| send(setname).send("#{new_attr}=", value) }

          define_method("#{old_attr}_changed?") do
            collection_proxy = send(setname)
            return false unless collection_proxy.loaded?
            setting = collection_proxy.lookup_without_default(new_attr)

            if !setting
              false # Nothing has been set which means that the attribute hasn't changed
            elsif setting.new_record?
              collection_proxy.association_class.default(new_attr) != setting.value
            else
              setting.value_changed?
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
property_sets-3.5.1 lib/property_sets/delegator.rb
property_sets-3.5.0 lib/property_sets/delegator.rb
property_sets-3.4.0 lib/property_sets/delegator.rb
property_sets-3.3.1 lib/property_sets/delegator.rb
property_sets-3.3.0 lib/property_sets/delegator.rb