spree
spree copied to clipboard
Custom preferences undefined method
In my engine I'm having an undefined method error trying to access my custom preferences. For example: `allow_backorders'.
Context
I decorate the models like so:
Spree::AppConfiguration.class_eval do
preference :sss_path, :string, default: '/store'
preference :allow_backorders, :boolean, default: false
end
Spree::StockLocation.class_eval do
def allow_backorders
Spree::Config[:allow_backorders]
end
In the engine I have:
module Sss
class Engine < ::Rails::Engine
engine_name 'sss'
config.to_prepare do
Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
end
end
I override the admin view "backend/app/views/spree/admin/products/stock_html.erb" trying to access the preference
<%= render partial: 'spree/admin/shared/product_tabs', locals: {current: :stock} %>
...
<%= check_box_tag 'stock_item[backorderable]', true,
item.backorderable?,
disabled: !item.stock_location.allow_backorders,
class: 'stock_item_backorderable',
id: "stock_item_backorderable_#{item.stock_location.id}" %>
<% end if can? :update, item %>
But I'm having a: "NoMethodError in Spree::Admin::Products#stock"
I coudn't reproduce the error on heroku because I coudn't build the app having the error:
""" remote: NameError: expected file /tmp/build_583a208db85826b5bdf3804716a0424d/vendor/bundle/ruby/2.5.0/bundler/gems/sss-d5e18e14bd01/app/models/spree/product_decorator.rb to define constant Spree::ProductDecorator, but didn't """
I have a previous running version:
- ruby '2.5.1'
- rails '5.2.3'
- spree '~> 3.7.0'
- spree_auth_devise '~> 3.5'
- and my engine: 'sss', github: 'MateoLa/spree_social_sales' in: https://yovendia.herokuapp.com/
You could clone the failing new version:
- ruby '2.5.1'
- rails '6.0.0'
- spree '~> 4.0.0.rc2'
- spree_auth_devise '~> 4.0.0.rc2'
- and my NEW engine: 'sss', github: 'MateoLa/sss' from: https://github.com/MateoLa/yovendo
Another Context
You could see the problem faster if in the engine you do:
Spree::AppConfiguration.class_eval do
preference :sss_path, :string, default: '/store'
end
Spree::Core::Engine.add_routes do
resources :stock_locations, path: Spree::Config[:sss_path], only: :show
end
You will get the error: "sss_path preference not defined (NoMethodError)"
Here is the trace:
rails s
=> Booting Puma
=> Rails 6.0.0 application starting in development
=> Run `rails server --help` for more startup options
Exiting
Traceback (most recent call last):
93: from bin/rails:3:in `<main>'
92: from bin/rails:3:in `load'
91: from /home/mateo/yovendo/bin/spring:15:in `<top (required)>'
90: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
89: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
88: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
87: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
86: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
85: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
84: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
83: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
82: from /usr/local/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
81: from /home/mateo/yovendo/bin/rails:9:in `<top (required)>'
80: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
79: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
78: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
77: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
76: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
75: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
74: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
73: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
72: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/commands.rb:18:in `<top (required)>'
71: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/command.rb:46:in `invoke'
70: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/command/base.rb:65:in `perform'
69: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
68: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
67: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
66: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/commands/server/server_command.rb:138:in `perform'
65: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/commands/server/server_command.rb:138:in `tap'
64: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/commands/server/server_command.rb:147:in `block in perform'
63: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/commands/server/server_command.rb:37:in `start'
62: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/commands/server/server_command.rb:77:in `log_to_stdout'
61: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:354:in `wrapped_app'
60: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:219:in `app'
59: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:319:in `build_app_and_options_from_config'
58: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:40:in `parse_file'
57: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:49:in `new_from_string'
56: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:49:in `eval'
55: from config.ru:in `<main>'
54: from config.ru:in `new'
53: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:55:in `initialize'
52: from /usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/builder.rb:55:in `instance_eval'
51: from config.ru:3:in `block in <main>'
50: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `require_relative'
49: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
48: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
47: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
46: from /usr/local/lib/ruby/gems/2.5.0/gems/zeitwerk-2.2.0/lib/zeitwerk/kernel.rb:23:in `require'
45: from /usr/local/lib/ruby/gems/2.5.0/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
44: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
43: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
42: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
41: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
40: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
39: from /home/mateo/yovendo/config/environment.rb:5:in `<top (required)>'
38: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application.rb:363:in `initialize!'
37: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/initializable.rb:60:in `run_initializers'
36: from /usr/local/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
35: from /usr/local/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
34: from /usr/local/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
33: from /usr/local/lib/ruby/2.5.0/tsort.rb:347:in `call'
32: from /usr/local/lib/ruby/2.5.0/tsort.rb:347:in `each'
31: from /usr/local/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
30: from /usr/local/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
29: from /usr/local/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
28: from /usr/local/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
27: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/initializable.rb:61:in `block in run_initializers'
26: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `run'
25: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `instance_exec'
24: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/finisher.rb:184:in `block in <module:Finisher>'
23: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/routes_reloader.rb:10:in `execute'
22: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-6.0.0/lib/active_support/file_update_checker.rb:83:in `execute'
21: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/routes_reloader.rb:30:in `block in updater'
20: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/routes_reloader.rb:20:in `reload!'
19: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/routes_reloader.rb:41:in `load_paths'
18: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/routes_reloader.rb:41:in `each'
17: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/application/routes_reloader.rb:41:in `block in load_paths'
16: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
15: from /usr/local/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
14: from /usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/config/routes.rb:5:in `<top (required)>'
13: from /usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/lib/spree/core/routes.rb:30:in `draw_routes'
12: from /usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/lib/spree/core/routes.rb:30:in `each'
11: from /usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/lib/spree/core/routes.rb:30:in `block in draw_routes'
10: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/railtie.rb:190:in `method_missing'
9: from /usr/local/lib/ruby/gems/2.5.0/gems/railties-6.0.0/lib/rails/railtie.rb:190:in `public_send'
8: from /usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/lib/spree/core/routes.rb:38:in `eval_block'
7: from /usr/local/lib/ruby/gems/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:427:in `eval_block'
6: from /usr/local/lib/ruby/gems/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/mapper.rb:648:in `with_default_scope'
5: from /usr/local/lib/ruby/gems/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/mapper.rb:887:in `scope'
4: from /usr/local/lib/ruby/gems/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/mapper.rb:649:in `block in with_default_scope'
3: from /usr/local/lib/ruby/gems/2.5.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/mapper.rb:649:in `instance_exec'
2: from /home/mateo/sss/config/routes.rb:71:in `block in <top (required)>'
1: from /usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/app/models/spree/preferences/preferable.rb:42:in `get_preference'
/usr/local/lib/ruby/gems/2.5.0/gems/spree_core-4.0.0/app/models/spree/preferences/preferable.rb:62:in `has_preference!': sss_path preference not defined (NoMethodError)
Same here. I tried the actual example from the spree developer guides:
# app/models/spree/app_configuration_decorator.rb
Spree::AppConfiguration.class_eval do
preference :hot_salsa, :boolean
end
And then when I open rails c
:
Spree::Config.hot_salsa
# => NoMethodError (undefined method `hot_salsa' for #<Spree::AppConfiguration:0x00007fba3a18bb30>)
Spree::Config[:hot_salsa]
# => NoMethodError (hot_salsa preference not defined)
Ruby: 2.7.1 Spree: 4.1.7
Using a clean Spree install
EDIT
Deleted the comment about adding preferences to store, upon adding the preferences column it works as expected:
strore.preferences[:hot_salsa]
=> true
store.prefers_hot_salsa?
and store.prefers?
still don't work, but that's ok
@MatthewKennedy In which folder would you suggest to add the engine.rb file on a clean spree instalation? Spee has an engine on core, frontend, and backend :)
Sorry, I thought you were speaking from an extension. Not sure.
We can add new preferences with below snippet for spree 4.4:
`module Spree module Core module AppConfigurationDecorator def self.prepended(base) base.preference :blog_alias, :string, default: 'blog' end end end end
Spree::Core::Configuration.prepend Spree::Core::AppConfigurationDecorator `