如何在我的应用程序中实现装饰器?

时间:2023-01-13 16:33:19

I am trying to implement this in my app.

我想在我的应用程序中实现这一点。

The article says that I must create a decorator - but it doesn't go into much detail about exactly how to do that. This is the code:

文章说我必须创建一个装饰器 - 但它没有详细说明如何做到这一点。这是代码:

module CartDecorator
  extend ActiveSupport::Concern

  module InstanceMethods
    def is_downloadable?
      items = self.items.collect { |li| li[:variant].item }
      items.all? { |i| i.is_downloadable }
    end

    def has_downloadable?
      items = self.items.collect { |li| li[:variant].item }
      items.any? { |i| i.is_downloadable }
    end
  end
end

Piggybak::Cart.send(:include, CartDecorator)

I am not sure if I should add that code to some model.rb (for what it's worth, I don't have a piggybak_cart.rb in my app/models/ folder).

我不确定是否应该将该代码添加到某个model.rb(为了它的价值,我的app / models /文件夹中没有piggybak_cart.rb)。

I tried running rails g decorator Cart and that didn't work.

我试过运行rails g decorator Cart,但是没有用。

What I have done is put the above code in app/helpers/cart_helper.rb.

我所做的是将上面的代码放在app / helpers / cart_helper.rb中。

Then when I tried to run a rails g command (for something else), I am now getting this error:

然后,当我尝试运行rails g命令(其他东西)时,我现在收到此错误:

/.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant CartHelper (NameError)
    from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
    from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
    from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
    from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:136:in `block in modules_for_helpers'
    from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:131:in `map!'

What's the best way to approach this?

什么是最好的方法来解决这个问题?

1 个解决方案

#1


3  

You should add the above code into app/decorators/cart_decorator.rb

您应该将以上代码添加到app / decorators / cart_decorator.rb中

the decorators folder will be new and be autoloaded when you start your rails app. when this is run Piggybak::Cart.send(:include, CartDecorator) it will decorate your Piggybag::Cart with the methods you declared above.

decorators文件夹将是新的,并在您启动rails应用程序时自动加载。当运行Piggybak :: Cart.send(:include,CartDecorator)时,它将使用您在上面声明的方法装饰您的Piggybag :: Cart。

#1


3  

You should add the above code into app/decorators/cart_decorator.rb

您应该将以上代码添加到app / decorators / cart_decorator.rb中

the decorators folder will be new and be autoloaded when you start your rails app. when this is run Piggybak::Cart.send(:include, CartDecorator) it will decorate your Piggybag::Cart with the methods you declared above.

decorators文件夹将是新的,并在您启动rails应用程序时自动加载。当运行Piggybak :: Cart.send(:include,CartDecorator)时,它将使用您在上面声明的方法装饰您的Piggybag :: Cart。