Skip to content

Commit

Permalink
Add FormFor and InitializeWith
Browse files Browse the repository at this point in the history
  • Loading branch information
antulik committed Jun 29, 2024
1 parent 13a6466 commit fc564a6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/active_interaction/extras.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/active_interaction/extras/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fc564a6

Please sign in to comment.