Class: Rollbacker::ChangeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rollbacker/change_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, options, model) ⇒ ChangeValidator

Returns a new instance of ChangeValidator.



6
7
8
9
10
# File 'lib/rollbacker/change_validator.rb', line 6

def initialize(action, options, model)
  @action   = action
  @options  = options
  @model    = model
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/rollbacker/change_validator.rb', line 3

def action
  @action
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/rollbacker/change_validator.rb', line 4

def options
  @options
end

Instance Method Details

#changes(other_changes = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rollbacker/change_validator.rb', line 21

def changes(other_changes={})
  chg = @model.changes.dup
  chg.reverse_merge!( (other_changes || {}).with_indifferent_access )
  chg.reject!{|key, value| @options[:except].include?(key) } unless @options[:except].blank?
  chg.reject!{|key, value| !@options[:only].include?(key)  } unless @options[:only].blank?
  chg
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
# File 'lib/rollbacker/change_validator.rb', line 12

def valid?
  case @action
  when :destroy
    @model.persisted?
  when :update, :create
    self.changes.any?
  end
end