diff --git a/lib/active_interaction/extras.rb b/lib/active_interaction/extras.rb index 6b59725..68a9723 100644 --- a/lib/active_interaction/extras.rb +++ b/lib/active_interaction/extras.rb @@ -1,6 +1,9 @@ require "active_interaction/extras/version" require 'active_support' +require 'active_support/core_ext/class/attribute' +require 'active_support/core_ext/module/concerning' + require 'active_interaction' module ActiveInteraction @@ -29,6 +32,49 @@ module Jobs autoload(:ActiveJob, "active_interaction/extras/active_job") autoload(:Sidekiq, "active_interaction/extras/sidekiq") + + concern :FormFor do + class_methods do + def form_for(field_name) + delegate :persisted?, :id, :to_param, to: field_name + end + end + end + + concern :AfterInitialize do + include ActiveSupport::Callbacks + + included do + define_callbacks :initialize + end + + class_methods do + def after_initialize(&block) + set_callback :initialize, :after, &block + end + end + + def initialize(...) + super + run_callbacks :initialize, :after + end + end + + concern :InitializeWith do + include ActiveInteraction::Extras::AfterInitialize + + class_methods do + def initialize_with(&block) + after_initialize do + hash = instance_exec(&block) + hash.each do |filter_name, value| + public_send "#{filter_name}=", value if !inputs.given?(filter_name) + end + end + end + end + end + end end diff --git a/lib/active_interaction/extras/all.rb b/lib/active_interaction/extras/all.rb index c1528ef..da5ae22 100644 --- a/lib/active_interaction/extras/all.rb +++ b/lib/active_interaction/extras/all.rb @@ -9,4 +9,9 @@ module ActiveInteraction::Extras::All include ActiveInteraction::Extras::ModelFields include ActiveInteraction::Extras::RunCallback include ActiveInteraction::Extras::StrongParams + + include ActiveInteraction::Extras::FormFor + include ActiveInteraction::Extras::AfterInitialize + include ActiveInteraction::Extras::InitializeWith + end