Class: Stall::Checkout::Step

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ Step

Returns a new instance of Step.



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

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



6
7
8
# File 'lib/stall/checkout/step.rb', line 6

def cart
  @cart
end

Class Method Details

.for(identifier) ⇒ Object

Handles conversion from an identifier to a checkout step class, allowing us to specify a list of symbols in our wizard’s .step macro



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/stall/checkout/step.rb', line 75

def self.for(identifier)
  name = identifier.to_s.camelize
  step_name = [name, 'CheckoutStep'].join
  # Try loading step from app
  step = Stall::Utils.try_load_constant(step_name)
  # Try loading step from stall core or lib if not found in app
  step = Stall::Utils.try_load_constant(
    ['Stall', 'Checkout', step_name.demodulize].join('::')
  ) unless step

  unless step
    raise StepNotFoundError,
      "No checkout step was found for #{ identifier }. You can generate " +
      "it with `rails g stall:checkout:step #{ identifier }`"
  end

  step
end

.validations(&block) ⇒ Object



94
95
96
97
# File 'lib/stall/checkout/step.rb', line 94

def self.validations(&block)
  return @validations unless block
  @validations = Stall::Checkout::StepForm.build(&block)
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)


61
62
63
# File 'lib/stall/checkout/step.rb', line 61

def allow_inactive_carts?
  false
end

#cart_paramsObject



30
31
32
33
34
35
36
# File 'lib/stall/checkout/step.rb', line 30

def cart_params
  @cart_params ||= if params[:cart]
    params.require(:cart).permit!
  else
    {}.with_indifferent_access
  end
end

#identifierObject



46
47
48
49
50
51
# File 'lib/stall/checkout/step.rb', line 46

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



14
15
16
# File 'lib/stall/checkout/step.rb', line 14

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

#is?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def is?(key)
  identifier == key
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



23
24
# File 'lib/stall/checkout/step.rb', line 23

def prepare
end

#processObject



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

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



67
68
69
70
# File 'lib/stall/checkout/step.rb', line 67

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

#skip?Boolean

Returns:

  • (Boolean)


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

def skip?
  false
end

#valid?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/stall/checkout/step.rb', line 99

def valid?
  return true unless (validations = self.class.validations)
  validations.new(cart, self).validate
end