Class: Stall::Checkout::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/stall/checkout/step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ Step

Returns a new instance of Step.



10
11
12
# File 'lib/stall/checkout/step.rb', line 10

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



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

def cart
  @cart
end

Instance Method Details

#allow_inactive_carts?Boolean

Allows subclasses to override this value with ‘true` to allow paid carts to be processed by that step too.

Returning false redirects the user telling him that his cart is empty

By default, the ‘PaymentReturn` checkout step returns true, since it’s always called after the cart is paid

Returns:

  • (Boolean)


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

def allow_inactive_carts?
  false
end

#identifierObject



41
42
43
44
45
46
# File 'lib/stall/checkout/step.rb', line 41

def identifier
  @identifier ||= begin
    class_name = self.class.name.demodulize
    class_name.gsub(/CheckoutStep$/, '').underscore.to_sym
  end
end

#inject(method, content) ⇒ Object

Allow injecting dependencies on step initialization and accessing them as instance method in subclasses



16
17
18
# File 'lib/stall/checkout/step.rb', line 16

def inject(method, content)
  define_singleton_method(method, -> { content })
end

#is?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/stall/checkout/step.rb', line 36

def is?(other)
  step_identifier = (Step === other ? other.identifier : other.to_sym)
  identifier == step_identifier
end

#prepareObject

Allows for preparing to the cart for the current step before rendering the step’s view

Note : Meant to be overriden by subclasses



25
26
# File 'lib/stall/checkout/step.rb', line 25

def prepare
end

#processObject



28
29
30
# File 'lib/stall/checkout/step.rb', line 28

def process
  save
end

#saveObject

Abstracts the simple case of assigning the submitted parameters to the cart object, running the step validations and saving the cart



72
73
74
75
# File 'lib/stall/checkout/step.rb', line 72

def save
  cart.assign_attributes(cart_params)
  cart.save if valid?
end

#skip?Boolean

Returns:

  • (Boolean)


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

def skip?
  false
end

#valid?Boolean

Run cart validations then step validations, and cart validations, returning wether they’re both valid or not, allowing to display all involved errors to the visitor in one time

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'lib/stall/checkout/step.rb', line 63

def valid?
  cart.validate
  run_step_validations!(clear: false)

  cart.errors.empty?
end