i currently have an ugly `class_eval` that i suspe...
# support
e
i currently have an ugly
class_eval
that i suspect i can replace with a cleaner prepended situation, but i'm not sure how ... my current code is
Copy code
Spree::Base.class_eval do
  before_create :associate_tenant

  class TenantNotFound < StandardError; end

  def associate_tenant
    return logger.warn("Ingoring setting tenant") if RLS.disabled?
    raise(TenantNotFound, "RLS.current_tenant is not set") unless RLS.current_tenant

    self.tenant_id = RLS.current_tenant_id
    <http://logger.info|logger.info>("Set thread for #{self.class.name}")
  end
end
is there an obvious way to replace this evaluated callback creation with one that would prepend the callback instead?
c
What about throwing this in your
ApplicationRecord
class?
I guess that doesn’t work with Solidus. You may have to override the
Spree::Base
class like you are doing.
It’s probably as easy as defining a module and prepending it though, if you don’t want to use
class_eval
. Something like
Copy code
# 'app/models/my_app/base_decorator.rb'
module MyApp::BaseDecorator
  def self.prepended(base)
    base.before_create :associate_tenant
  end

  # all the other stuff you have ...

  Spree::Base.prepend(self)
end
Updated to fix the nesting and added the actual call to
prepend
j
If you’re on new rails you can make it a concern to further clean it up.
1
💯 1
c
@Jared Norman based on this I guess we can’t use
ApplicationRecord
right? https://github.com/solidusio/solidus/pull/3476
IIRC that has to be supplied by the app, but no way to do that and reference it from the solidus engines
e
Oh that's interesting. Is it not possible to inherit the application's application record from an engine?