Module: ActAsDirty::ActiveModel::Cleans::ClassMethods

Defined in:
lib/act_as_dirty/active_model/cleans.rb

Instance Method Summary collapse

Instance Method Details

#act_as_dirtyObject



18
19
20
21
# File 'lib/act_as_dirty/active_model/cleans.rb', line 18

def act_as_dirty
  has_many :dirty_messages, :as => :dirtable, dependent: :destroy
  accepts_nested_attributes_for :dirty_messages, :reject_if => lambda { |a| a[:message].blank? }, :allow_destroy => true
end

#clean(*args, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/act_as_dirty/active_model/cleans.rb', line 56

def clean(*args, &block)
  options = args.extract_options!
  if options.key?(:on)
    options = options.dup
    options[:if] = Array.wrap(options[:if])
    options[:if].unshift("cleaning_context == :#{options[:on]}")
  end
  args << options
  set_callback(:clean, *args, &block)
end

#cleanersObject

List all trackers that are being used to create the message using



68
69
70
# File 'lib/act_as_dirty/active_model/cleans.rb', line 68

def cleaners
  _cleaners.values.flatten.uniq
end

#cleaners_on(*attributes) ⇒ Object

List all validators that being used to validate a specific attribute.



73
74
75
76
77
# File 'lib/act_as_dirty/active_model/cleans.rb', line 73

def cleaners_on(*attributes)
  attributes.map do |attribute|
    _cleaners[attribute.to_sym]
  end.flatten
end

#cleans(*attributes) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/act_as_dirty/active_model/cleans.rb', line 23

def cleans(*attributes)
  defaults = attributes.extract_options!
  options = defaults.slice!(*_cleaning_default_keys)
  
  raise ArgumentError, 'Specify at least one attribute you would like DirtyMe to handle the message for' if attributes.empty?
  
  attributes = self.attribute_names.map(&:to_sym) if attributes.include? :all
  defaults.merge!(:attributes => attributes)
  
  attributes.each do |attr, options|
    raise ArgumentError, "The attribute '#{attr}' doesn't correspond to a column in the database" unless self.columns_hash[attr.to_s]            
    cleans_with(ActAsDirty::ActiveModel::Cleaner, defaults)
  end
end

#cleans_with(*args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/act_as_dirty/active_model/cleans.rb', line 38

def cleans_with(*args, &block)
  options = args.extract_options!
  args.each do |klass|
    cleaner = klass.new(options, &block)
    cleaner.setup(self) if cleaner.respond_to?(:setup)

    if cleaner.respond_to?(:attributes) && !cleaner.attributes.empty?
      cleaner.attributes.each do |attribute|
        _cleaners[attribute.to_sym] << cleaner
      end
    else
      _cleaners[nil] << cleaner
    end

    clean(cleaner, options)
  end
end

#inherited(base) ⇒ Object

Copy validators on inheritance.



80
81
82
83
84
# File 'lib/act_as_dirty/active_model/cleans.rb', line 80

def inherited(base)
  dup = _cleaners.dup
  base._cleaners = dup.each { |k, v| dup[k] = v.dup }
  super
end