0

Followed this post How to use concerns in Rails 4 to try to get concern to work. But it doesnt load the concern.

Would it be possible to add a concern like this to a model I have no control over or how should I do that? Ex if I want to add the concern to a model from a gem I am using.

I have added concern autoload in application.rb

config.autoload_paths += %W(#{config.root}/app/models/concerns)

I checked how Spree Commerce does it in the docs but that method doesn't work at all for me.

# app/models/concerns/schedule.rb
module Schedule
  extend ActiveSupport::Concern
  ...
end

Namespace::Trader.include Schedule
Community
  • 1
  • 1
Emil K
  • 95
  • 8
  • `module/class Namespace::Trader; include Schedule; end` but it's usually not a good idea to modify code you don't own. – avl Dec 04 '14 at 08:51
  • I tried to solve it with the console. Found it that it doesnt load the module by default in development mode. If I called "Schedule" in irb it loaded and applied it. development.rb has a setting eager_load which is set to false. If that is enabled it works right away. What is the downside of having eager load enabled in dev mode? – Emil K Dec 04 '14 at 09:05

1 Answers1

0

If your application doesn't own the model Namespace::Trader, i.e. if you don't have namespace/trader.rb file in your application, then what you can do is to create a file called app/models/namespace/trader_decorator.rb with the following code:

Namespace::Trader.class_eval do
  include Schedule
end

This should include Schedule module to Namespace::Trader class.

Surya
  • 14,897
  • 2
  • 44
  • 70