H guys, first of all let me tell u I am new to spree, so my question might sound stupid to most of you. I want to customize for example the "index" method in the home_controller.rb, I know the right way is to use a decorators. So I have created this file app/controller/home_controller_decorator.rb. I have in there
# app/controller/home_controller_decorator.rb
HomeController.class_eval do
def index
# Empty method
end
end
The original spree index method looks like
def index
@searcher = Spree::Config.searcher_class.new(params)
@products = @searcher.retrieve_products
respond_with(@products)
end
I expect that when I restart the server with the _decorator adde开发者_Go百科d it will display me no products on the home page, or will crash. When applying this decorator and starting the server I get this message
agop@linux-as2q:~/Desktop/spp> rails server -p 3000
/home/agop/Desktop/spp/app/controllers/home_controller_decorator.rb:1:in `<top (required)>': uninitialized constant Spree::BaseController (NameError)
from /home/agop/Desktop/spp/lib/spree_site.rb:5:in `block in <class:Engine>'
from /home/agop/Desktop/spp/lib/spree_site.rb:4:in `glob'
from /home/agop/Desktop/spp/lib/spree_site.rb:4:in `<class:Engine>'
from /home/agop/Desktop/spp/lib/spree_site.rb:2:in `<module:SpreeSite>'
from /home/agop/Desktop/spp/lib/spree_site.rb:1:in `<top (required)>'
from /home/agop/Desktop/spp/config/application.rb:11:in `<class:Application>'
from /home/agop/Desktop/spp/config/application.rb:10:in `<module:Spp>'
from /home/agop/Desktop/spp/config/application.rb:9:in `<top (required)>'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:28:in `require'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:28:in `block in <top (required)>'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:27:in `tap'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I am probably not writing the decorator in the way spree expects. What is the correct way to apply this decorator on the home_controller.rb index method?
This is because HomeController
inherits from Spree::BaseController
which isn't loaded at this point in time due to some unknown reason. You should be able to fix it by putting require 'spree/base_controller'
at the top of your decorator.
Could you please also submit a GitHub issue for this on http://github.com/spree/spree? It would be helpful for anybody else who ran into this issue too.
ActiveSupport::Concern is a clean way to decorate existing classes.
But note, that caches are not cached in development mode, so you'll need to add something like the code below in config/environments/development.rb to ensure that your decorate methods persist.
config.to_prepare do
#SomeModel.send(:include, SomeDecorator)
end
Here are more details on this.
精彩评论