Sha256: ffd4cb4a0a1e77f12c8539b5047cb3b1dfef3d04f5cb01c85852aee5e7ff58fd

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

##
# This concern can be used to monitor associations for changes.
#
# Associations monitored using the methods in this concern will show up in the ActiveRecord model's changes,
# changed and previous_changes methods.
module DirtyAssociations
  extend ActiveSupport::Concern

  module ClassMethods

    ##
    # Creates methods that allows an +association+ to be monitored.
    #
    # The +association+ parameter should be a string or symbol representing the name of an association.
    def monitor_association_changes(association)
      ids = "#{association.to_s.singularize}_ids"

      [association, ids].each do |name|
        define_method "#{name}=" do |value|
          attribute_will_change!(association.to_s) unless send(name) == value
          super(value)
        end

        define_method "#{name}_change" do
          changes[name]
        end

        define_method "#{name}_changed?" do
          changes.has_key?(association.to_s)
        end

        define_method "#{name}_previously_changed?" do
          previous_changes.has_key?(association.to_s)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dirty_associations-0.4.0 lib/dirty_associations.rb