Class: Themis::AR::UseValidationMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/themis/ar/use_validation_method.rb

Overview

It encapsulates # use_validation method. It makes a model and its nested associations use another validation.

Instance Method Summary collapse

Constructor Details

#initialize(model, new_name) ⇒ UseValidationMethod

Returns a new instance of UseValidationMethod.

Parameters:

  • model (ActiveRecord::Base)

    instance of model

  • new_name (Symbol, String)

    name of new validation set



9
10
11
12
13
14
15
16
17
18
# File 'lib/themis/ar/use_validation_method.rb', line 9

def initialize(model, new_name)
  @model    = model
  @new_name = new_name.to_sym

  unless @model.has_themis_validation?(@new_name)
    raise ArgumentError.new("Unknown validation: `#{@new_name.inspect}` for #{model.class}")
  end

  @new_validation_set = @model.themis_validation_sets[@new_name]
end

Instance Method Details

#execute!Object

Affect model and its nested associations. Make them use the new validation set by assigning ‘@themis_validation` instance variable.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/themis/ar/use_validation_method.rb', line 23

def execute!
  # NOTE: It breaks encapsulation of ActiveRecord model.
  # We do it because we don't wanna public "themis_validation=" method.
  # -- sergey.potapov 2012-08-14
  @model.instance_variable_set("@themis_validation", @new_name)

  nested = @new_validation_set.nested || @model.class.themis_default_nested
  if nested
    association_names = Array.wrap(nested)
    affect_associations(association_names)
  end
end