-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow importing filters from other interactions #104
Comments
This is sort of like #52, but not quite. It's possible to use the class Beta < ActiveInteraction::Base
Alpha.filters.each do |filter|
send(filter.class.slug, filter.name, filter.options)
end
# ...
end Of course that doesn't work when blocks are involved, but it gets you most of the way there. |
Thanks to #112, I've found what feels like an elegant solution to this. We can use module ActiveInteraction
class Base
class << self
def const_defined?(name, *)
name.to_sym == :Filters || super
end
def const_missing(name)
return super unless const_defined?(name)
source_filters = self.filters
Module.new do
extend ActiveSupport::Concern
included do
filters.merge!(source_filters)
end
end
end
end
end
end |
@justinsteffy proposed a good alternate solution. Instead of including a dynamically created module, why not add a class method to module ActiveInteraction
class Base
def self.import(other, only: nil, except: nil)
# ...
end
end
end By default it would import all of the filters from the other interaction into the current one. If you supplied Here's how it might look to use it: class Alpha < ActiveInteraction::Base
boolean :gamma
# ...
end
class Beta < ActiveInteraction::Base
import Alpha
# import Alpha, only: [:gamma]
# import Alpha, except: [:gamma]
boolean :delta
# ...
end |
Here's a basic implementation: def import(klass, only: nil, except: nil)
other_filters = klass.filters.dup
other_filters.select! { |k, _| only.include?(k) } if only
other_filters.reject! { |k, _| except.include?(k) } if except
filters.merge!(other_filters)
end |
Fix #104; allow importing filters from another interaction
Sometimes it's useful to share filters between interactions. Usually inheritance doesn't work. My current workaround looks like this:
The text was updated successfully, but these errors were encountered: