ruby-on-rails - Spree 模块装饰器

标签 ruby-on-rails ruby ruby-on-rails-3 decorator spree

我正在为我的在线商店使用 Spree Commerce。我想在结帐过程中更改一些行为,这些行为在 spree gem 内的 app/models/spree/order/checkout.rb 中定义。所以我在我的应用程序中的同一点创建了一个 checkout_decorator.rb

问题是,我的更改没有加载。另一个问题是,模块内的所有内容都在一个方法内,即 def self.included(klass) 方法。所以我想我必须覆盖整个文件,而不是只覆盖一种方法。这是我的装饰器的样子:

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

来自 spree gem 的原始文件 checkout.rb 如下所示:

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

所以我的问题是:为什么这不起作用? module_eval 是执行此操作的正确方法吗?我尝试了 class_eval 但它也不起作用。我该如何解决这个问题?

最佳答案

module_eval 方法对您不起作用。

你应该看看 Spree Checkout Flow Documentation有关如何自定义结帐流程的一些很好的示例。这是自定义结帐流程的推荐方法,因为您不需要复制/粘贴一大堆代码。

关于ruby-on-rails - Spree 模块装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810506/

相关文章:

ruby-on-rails - 自引用关联的 Rails 数据库约束

ruby-on-rails - 从 rails-geocoder gem 中的 Controller 获取纬度和经度值

mysql - 使用 Ruby/Chef Recipe for Vagrant 导入 Mysql 数据库

ruby-on-rails - Heroku 数据库 :pull cant connect to my DB in ubuntu

json - 使用 Rails 3 应用程序的 ios json 身份验证

ruby-on-rails - 无法在我的表单中添加第二个下拉菜单

ruby-on-rails - 呈现带有错误和文件输入的表单

ruby-on-rails - 我怎样才能弄清楚为什么我的 JRuby Rails 应用程序需要很长时间才能提供页面?

ruby - 使用 sudo 进行 gem 安装 cocoapods

ruby - rails 3 : Do i need to give return true in a before_save callback for an object. 保存工作?