Spree Module Designer

I use Spree Commerce for my online store. I want to change some behavior during the verification process, which is defined in app/models/spree/order/checkout.rb inside the spree gem. So I did checkout_decorator.rb at the same point in the application.

The problem is that my changes are not uploaded. And another problem is that everything inside the module is inside one method, def self.included(klass) method. Therefore, I think I should overwrite the entire file, not just one method. Here's what my decorator looks like:

checkout_decorator.rb

 Spree::Order::Checkout.module_eval do def self.included(klass) klass.class_eval do class_attribute :next_event_transitions class_attribute :previous_states class_attribute :checkout_flow class_attribute :checkout_steps def self.define_state_machine! # here i want to make some changes end # and the other methods are also include here # for readability, i don't show them here end end end 

The original spree gemstone checkout.rb as follows:

 module Spree class Order < ActiveRecord::Base module Checkout def self.included(klass) klass.class_eval do class_attribute :next_event_transitions class_attribute :previous_states class_attribute :checkout_flow class_attribute :checkout_steps def self.checkout_flow(&block) if block_given? @checkout_flow = block define_state_machine! else @checkout_flow end end def self.define_state_machine! # some code end # and other methods that are not shown here end end end end end 

So my questions are: why doesn't this work? Is module_eval right way to do this? I tried class_eval but it doesn't work either. How can i solve this?

+6
source share
3 answers

The module_eval method will not work for you.

You should look at the Spree Checkout Flow Documentation for some good examples of how to set up a check flow. This is the recommended way to configure the verification flow, since you will not need to copy / paste a whole bunch of code.

+1
source

Incorrect location of names.

Try Spree::Order::Checkout.class_eval do

+1
source

tl; dr: rewrite the method you want in the Spree :: Order class instead of the Spree :: Order :: Checkout module.

You mentioned that in the source file (spree_core-3.2.0.rc3 / app / models / spree / order / checkout.rb) there is a way to wrap the entire module.

 def self.included(klass) klass.class_eval do 

This method is called when the module is included in the class and has its own class_eval to add module methods to class instances, including it.

So, since (spree_core-3.2.0.rc3 / app / models / spree / order.rb) has the following line:

 include Spree::Order::Checkout 

We can add a decorator to the order class itself (app / models / spree / order_decorator.rb)

0
source

Source: https://habr.com/ru/post/950080/


All Articles