module Mongoid::Changeable::ClassMethods

Class-level methods for changeable objects.

Private Instance Methods

create_dirty_change_accessor(name, meth) click to toggle source

Creates the dirty change accessor.

@example Create the accessor.

Model.create_dirty_change_accessor("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 383
def create_dirty_change_accessor(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_change") do
      attribute_change(name)
    end
  end
end
create_dirty_change_check(name, meth) click to toggle source

Creates the dirty change check.

@example Create the check.

Model.create_dirty_change_check("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 398
def create_dirty_change_check(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_changed?") do |**kwargs|
      attribute_changed?(name, **kwargs)
    end
    re_define_method("will_save_change_to_#{meth}?") do |**kwargs|
      will_save_change_to_attribute?(name, **kwargs)
    end
  end
end
create_dirty_change_flag(name, meth) click to toggle source

Creates the dirty change flag.

@example Create the flag.

Model.create_dirty_change_flag("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 458
def create_dirty_change_flag(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_will_change!") do
      attribute_will_change!(name)
    end
  end
end
create_dirty_default_change_check(name, meth) click to toggle source

Creates the dirty default change check.

@example Create the check.

Model.create_dirty_default_change_check("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 416
def create_dirty_default_change_check(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_changed_from_default?") do
      attribute_changed_from_default?(name)
    end
  end
end
create_dirty_methods(name, meth) click to toggle source

Generate all the dirty methods needed for the attribute.

@example Generate the dirty methods.

Model.create_dirty_methods("name", "name")

@param [ String ] name The name of the field. @param [ String ] meth The name of the accessor.

@return [ Module ] The fields module.

# File lib/mongoid/changeable.rb, line 364
def create_dirty_methods(name, meth)
  create_dirty_change_accessor(name, meth)
  create_dirty_change_check(name, meth)
  create_dirty_change_flag(name, meth)
  create_dirty_default_change_check(name, meth)
  create_dirty_previous_value_accessor(name, meth)
  create_dirty_reset(name, meth)
  create_dirty_reset_to_default(name, meth)
  create_dirty_previously_changed?(name, meth)
  create_dirty_previous_change(name, meth)
end
create_dirty_previous_change(name, meth) click to toggle source

Creates the dirty change accessor.

@example Create the dirty change accessor.

Model.create_dirty_previous_change("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 518
def create_dirty_previous_change(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_previous_change") do
      previous_changes[name]
    end
  end
end
create_dirty_previous_value_accessor(name, meth) click to toggle source

Creates the dirty change previous value accessors.

@example Create the accessor.

Model.create_dirty_previous_value_accessor("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 431
def create_dirty_previous_value_accessor(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_was") do
      attribute_was(name)
    end
    re_define_method("#{meth}_previously_was") do
      attribute_previously_was(name)
    end
    re_define_method("#{meth}_before_last_save") do
      attribute_before_last_save(name)
    end
    re_define_method("saved_change_to_#{meth}") do
      saved_change_to_attribute(name)
    end
    re_define_method("saved_change_to_#{meth}?") do |**kwargs|
      saved_change_to_attribute?(name, **kwargs)
    end
  end
end
create_dirty_previously_changed?(name, meth) click to toggle source

Creates the dirty change check.

@example Create the dirty change check.

Model.create_dirty_previously_changed?("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 503
def create_dirty_previously_changed?(name, meth)
  generated_methods.module_eval do
    re_define_method("#{meth}_previously_changed?") do
      previous_changes.key?(name)
    end
  end
end
create_dirty_reset(name, meth) click to toggle source

Creates the dirty change reset.

@example Create the reset.

Model.create_dirty_reset("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 473
def create_dirty_reset(name, meth)
  generated_methods.module_eval do
    re_define_method("reset_#{meth}!") do
      reset_attribute!(name)
    end
  end
end
create_dirty_reset_to_default(name, meth) click to toggle source

Creates the dirty change reset to default.

@example Create the reset.

Model.create_dirty_reset_to_default("name", "alias")

@param [ String ] name The attribute name. @param [ String ] meth The name of the accessor.

# File lib/mongoid/changeable.rb, line 488
def create_dirty_reset_to_default(name, meth)
  generated_methods.module_eval do
    re_define_method("reset_#{meth}_to_default!") do
      reset_attribute_to_default!(name)
    end
  end
end