class Stall::Checkout::Step
Attributes
Public Class Methods
# File lib/stall/checkout/step.rb, line 10 def initialize(cart) @cart = cart end
Private Class Methods
Handles conversion from an identifier to a checkout step class, allowing us to specify a list of symbols in our wizard's .step macro
# File lib/stall/checkout/step.rb, line 108 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
# File lib/stall/checkout/step.rb, line 127 def self.validations(&block) return _validations unless block self._validations = Stall::Checkout::StepForm.build(&block) end
Public Instance Methods
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
# File lib/stall/checkout/step.rb, line 56 def allow_inactive_carts? false end
# 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
Allow injecting dependencies on step initialization and accessing them as instance method in subclasses
# File lib/stall/checkout/step.rb, line 16 def inject(method, content) define_singleton_method(method, -> { content }) end
# File lib/stall/checkout/step.rb, line 36 def is?(other) step_identifier = (Step === other ? other.identifier : other.to_sym) identifier == step_identifier end
Allows for preparing to the cart for the current step before rendering the step's view
Note : Meant to be overriden by subclasses
# File lib/stall/checkout/step.rb, line 25 def prepare end
# File lib/stall/checkout/step.rb, line 28 def process save end
Abstracts the simple case of assigning the submitted parameters to the cart object, running the step validations and saving the cart
# File lib/stall/checkout/step.rb, line 72 def save cart.assign_attributes(cart_params) cart.save if valid? end
# File lib/stall/checkout/step.rb, line 32 def skip? false end
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
# File lib/stall/checkout/step.rb, line 63 def valid? cart.validate run_step_validations!(clear: false) cart.errors.empty? end
Private Instance Methods
# File lib/stall/checkout/step.rb, line 79 def cart_params(*attributes) @cart_params ||= if params.key?(:cart) params.require(:cart).permit(attributes) else {} end end
Allows overriding the nested array of permitted parameters to add other params at a given path. Rails default behavior doesn't allow merging nested arrays, neither top-level arrays.
Note : We use the `deep_merge` gem for the nested arrays merging.
# File lib/stall/checkout/step.rb, line 93 def merge_arrays(defaults, overrides) defaults_hash = defaults.extract_options! overrides_hash = overrides.extract_options! (defaults + overrides) << defaults_hash.deeper_merge!(overrides_hash) end
# File lib/stall/checkout/step.rb, line 99 def run_step_validations!(clear: true) if (validations = self.class.validations) validations.new(cart, self, clear: clear).validate end end