Class: Stall::Checkout::StepForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/stall/checkout/step_form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, step, clear: true) ⇒ StepForm

Returns a new instance of StepForm.



12
13
14
15
16
# File 'lib/stall/checkout/step_form.rb', line 12

def initialize(object, step, clear: true)
  @object = object
  @step = step
  @clear_cart_errors = clear
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/stall/checkout/step_form.rb', line 44

def method_missing(method, *args, &block)
  if object.respond_to?(method, true)
    object.send(method, *args, &block)
  elsif step.respond_to?(method, true)
    step.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#clear_cart_errorsObject (readonly)

Returns the value of attribute clear_cart_errors.



8
9
10
# File 'lib/stall/checkout/step_form.rb', line 8

def clear_cart_errors
  @clear_cart_errors
end

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/stall/checkout/step_form.rb', line 8

def object
  @object
end

#stepObject (readonly)

Returns the value of attribute step.



8
9
10
# File 'lib/stall/checkout/step_form.rb', line 8

def step
  @step
end

Class Method Details

.build(&block) ⇒ Object

Build an dynamic StepForm subclass with the given block as the body of the class



40
41
42
# File 'lib/stall/checkout/step_form.rb', line 40

def self.build(&block)
  Class.new(StepForm, &block)
end

.nested(type, &block) ⇒ Object



32
33
34
35
# File 'lib/stall/checkout/step_form.rb', line 32

def self.nested(type, &block)
  self.nested_forms ||= {}
  nested_forms[type] = build(&block)
end

Instance Method Details

#model_nameObject

Override model name instanciation to add a name, since the form classes are anonymous, and ActiveModel::Name does not support unnamed classes



56
57
58
# File 'lib/stall/checkout/step_form.rb', line 56

def model_name
  @model_name ||= ActiveModel::Name.new(self, nil, self.class.name)
end

#validateObject

Runs form and nested forms validations and returns wether they all passed or not

Only clear validation errors on the cart if needed, allowing to run cart validations before the step ones, passing clear: false in the form constructor, aggregating both validation sources’ errors



25
26
27
28
29
30
# File 'lib/stall/checkout/step_form.rb', line 25

def validate
  errors.clear if clear_cart_errors
  run_validations!
  validate_nested_forms
  !errors.any?
end