Quantcast
Viewing latest article 40
Browse Latest Browse All 42

Dynamic concerns with inheritance not loading twice, but only once

We are loading code dynamically with concerns, based on some environment variables, which works pretty nice.

Something like this:

# User classclass User  include DynamicConcernend
module DynamicConcern  extend ActiveSupport::Concern  included do    if "Custom::#{ENV["CUSTOMER_NAME"].camelize}::#{self.name}Concern".safe_constantize      include "Custom::#{ENV["CUSTOMER_NAME"].camelize}::#{self.name}Concern".constantize     end  endend
# custom codemodule Custom::Custom123::UserConcern  extend ActiveSupport::Concern  included do    ...  endend

We are using this since years and it worked absolutely fine in models. Some days ago we tried to use the same approach with Controllers, but realized that this approach doesn't work fine with inheritance, where the parent class inherits the concern as well as the inherited class:

class ApplicationController < ActionController::Base  # this gets loaded and includes the right dynamic module  include DynamicConcern endclass ShopController < ApplicationController  # this is NOT getting loaded again and skipped,   # since it has been loaded already in the parent controller  include DynamicConcern end

Is there a way to tell rails that it should include/evaluade the concern a second time, since the second time it would have another class name which would include another module?

I'm not looking for other solutions, since a lot of our code is based on this approach and I think it's possible to solve this without rewriting everything.

Thanks!


Viewing latest article 40
Browse Latest Browse All 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>