Skip to content

Commit 02639ac

Browse files
committed
InstanceRunnable
1 parent c07fbd8 commit 02639ac

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/active_interaction/extras.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,35 @@ def form_for(field_name)
4848
model_name.singular_route_key = klass.model_name.singular_route_key
4949
end
5050

51+
# Resolve form object route to object
52+
# e.g. resolve "User::Form" { |form| form.user }
5153
Rails.application.routes.add_polymorphic_mapping(name, {}) do |form|
5254
form.send(field_name)
5355
end
5456
end
5557
end
5658
end
5759

60+
# Allows Interaction.new.run
61+
# Useful for controllers, so single object could be instantiated for new, create, edit, update
62+
concern :InstanceRunnable do
63+
def save
64+
run
65+
end
66+
67+
def save!
68+
run!
69+
end
70+
71+
def run
72+
if @_executed
73+
raise("Service already executed")
74+
end
75+
@_executed = true
76+
super
77+
end
78+
end
79+
5880
concern :ModelNames do
5981
class_methods do
6082
def inherited(subclass)

lib/active_interaction/extras/all.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module ActiveInteraction::Extras::All
1515
include ActiveInteraction::Extras::Current
1616

1717
include ActiveInteraction::Extras::FormFor
18+
include ActiveInteraction::Extras::InstanceRunnable
1819
include ActiveInteraction::Extras::InitializeWith
1920
include ActiveInteraction::Extras::IncludeErrors
2021
include ActiveInteraction::Extras::AllRunner

0 commit comments

Comments
 (0)