Module: ActAsDirty::ActiveModel::Cleans::ClassMethods
- Defined in:
- lib/act_as_dirty/active_model/cleans.rb
Instance Method Summary collapse
- #act_as_dirty ⇒ Object
- #clean(*args, &block) ⇒ Object
-
#cleaners ⇒ Object
List all trackers that are being used to create the message using.
-
#cleaners_on(*attributes) ⇒ Object
List all validators that being used to validate a specific attribute.
- #cleans(*attributes) ⇒ Object
- #cleans_with(*args, &block) ⇒ Object
-
#inherited(base) ⇒ Object
Copy validators on inheritance.
Instance Method Details
#act_as_dirty ⇒ Object
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) = args. if .key?(:on) = .dup [:if] = Array.wrap([:if]) [:if].unshift("cleaning_context == :#{[:on]}") end args << set_callback(:clean, *args, &block) end |
#cleaners ⇒ Object
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
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. = 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, | 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) = args. args.each do |klass| cleaner = klass.new(, &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, ) 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 |