Module: ValidationSets

Included in:
ActiveRecord::Base
Defined in:
lib/validation_sets.rb,
lib/validation_sets/validation_set.rb

Defined Under Namespace

Modules: InstanceMethods Classes: ValidationSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_validation_setObject

Returns the value of attribute current_validation_set.



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

def current_validation_set
  @current_validation_set
end

Instance Method Details

#validation_method(on) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/validation_sets.rb', line 14

def validation_method(on)
  if current_validation_set
    validation_set_method(on, current_validation_set)
  else
    super
  end
end

#validation_set_for(label, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/validation_sets.rb', line 22

def validation_set_for(label, &block)
  [:save, :create, :update].each do |save_method|
    callback_chain = validation_method(save_method)
    callback_chain_for_set = validation_set_method(save_method, label)
    unless respond_to?(callback_chain_for_set)
      define_callbacks(callback_chain_for_set)
      define_method(callback_chain_for_set) { run_callbacks(callback_chain_for_set) }
      send(callback_chain, callback_chain_for_set, :if => Proc.new { |r| label.to_sym == r._validation_set })
    end
  end
  
  validation_set = ValidationSet.new(self, label)
  
  before = current_validation_set
  self.current_validation_set = label.to_sym
  block.call(validation_set)
  self.current_validation_set = before
end

#validation_set_method(on, label) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/validation_sets.rb', line 6

def validation_set_method(on, label)
  case on
    when :save   then "validate_#{label}_set".to_sym
    when :create then "validate_on_create_#{label}_set".to_sym
    when :update then "validate_on_update_#{label}_set".to_sym
  end
end