Class: ActAsDirty::ActiveModel::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/act_as_dirty/active_model/cleaner.rb

Overview

DirtyMe Cleaner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cleaner

Accepts options that will be made available through the options reader.



12
13
14
15
16
# File 'lib/act_as_dirty/active_model/cleaner.rb', line 12

def initialize(options)
  @attributes = Array.wrap(options[:attributes])
  raise ":attributes cannot be blank" if @attributes.empty?
  @options = options.freeze
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/act_as_dirty/active_model/cleaner.rb', line 9

def attributes
  @attributes
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/act_as_dirty/active_model/cleaner.rb', line 9

def options
  @options
end

Instance Method Details

#clean(record) ⇒ Object

Performs cleaning on the supplied record. By default this will call clean_each to determine cleanliness therefore subclasses should override clean_each with cleaning logic.



21
22
23
24
25
26
27
# File 'lib/act_as_dirty/active_model/cleaner.rb', line 21

def clean(record)
  return unless record.changed?
  attributes.each do |attribute|
    next unless record.changes[attribute.to_s]
    clean_each(record, attribute)
  end
end

#clean_each(record, attribute) ⇒ Object



29
30
31
# File 'lib/act_as_dirty/active_model/cleaner.rb', line 29

def clean_each(record, attribute)
  record.dirt.set(attribute, generate_message(record, attribute))
end