Skip to content

Commit ab70e22

Browse files
committed
refactor(Transaction Layer): finalize implementation of create/update ContentItem transactions, simplify concerns
1 parent 78d3c32 commit ab70e22

File tree

6 files changed

+69
-38
lines changed

6 files changed

+69
-38
lines changed

app/controllers/cortex/content_items_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def edit
3636

3737
def update
3838
begin
39-
content_item.update
39+
update_content_item
4040
rescue ActiveRecord::RecordInvalid => e
4141
flash[:warning] = validation_message(e.message)
4242
@content_item = content_item_reload(content_type.content_items.find_by_id(params[:id]))

app/helpers/cortex/content_item_helper.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ def content_type
44
@content_type ||= Cortex::ContentType.find_by_id(params[:content_type_id])
55
end
66

7-
def content_item
8-
@content_item ||= Cortex::ContentItemService.new(id: params[:id], content_item_params: content_item_params, current_user: current_user, state: params[:content_item][:state])
9-
end
10-
117
def create_content_item
128
CreateContentItemTransaction.new
139
.with_step_args(
@@ -18,6 +14,16 @@ def create_content_item
1814
.value!
1915
end
2016

17+
def update_content_item
18+
UpdateContentItemTransaction.new
19+
.with_step_args(
20+
execute_content_item_state_change: [state: params[:content_item][:state]]
21+
)
22+
.call(id: params[:id], content_type: content_type,
23+
content_item_params: content_item_params, current_user: current_user)
24+
.value!
25+
end
26+
2127
def content_item_reload(content_item)
2228
@content_item = content_item
2329
content_type.fields.each do |field|

app/operations/cortex/execute_content_item_state_change_operation.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
module Cortex
44
class ExecuteContentItemStateChangeOperation
55
include Dry::Transaction::Operation
6-
include Cortex::ContentItemable
76

87
def call(input, state:)
9-
execute_state_change(input, state)
8+
if state && input.can_transition?(state)
9+
state_method = "#{state}!"
10+
input.send(state_method)
11+
end
12+
1013
Success(input)
1114
end
1215
end

app/operations/cortex/parse_content_item_field_items_operation.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
module Cortex
44
class ParseContentItemFieldItemsOperation
55
include Dry::Transaction::Operation
6-
include Cortex::ContentItemable
6+
include Cortex::WidgetParsable
77

88
def call(input)
9-
content_item = parse_field_items!(input)
10-
Success(content_item)
9+
input.field_items.each do |field_item|
10+
if field_item.field.metadata && field_item.field.metadata['parse_widgets']
11+
parse_widgets!(field_item)
12+
end
13+
end
14+
15+
Success(input)
1116
end
1217
end
1318
end

app/transactions/concerns/cortex/content_itemable.rb

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,13 @@ module ContentItemable
33
extend ActiveSupport::Concern
44

55
included do
6-
include Cortex::WidgetParsable
7-
8-
def parse_field_items!(content_item)
9-
content_item.field_items.each do |field_item|
10-
if field_item.field.metadata && field_item.field.metadata['parse_widgets']
11-
parse_widgets!(field_item)
12-
end
13-
end
14-
content_item
15-
end
16-
176
def field_items_attributes(content_item_params)
187
content_item_params['field_items_attributes']
198
end
209

21-
def content_item_attributes(content_item_params, creator, content_type, field_items, current_user)
22-
attributes = content_item_params || { creator: creator, content_type: content_type, field_items: field_items }
23-
attributes.merge! latest_history_patch(current_user)
24-
end
25-
26-
def latest_history_patch(current_user)
27-
history_patch = {}
28-
history_patch.merge! last_updated_by(current_user)
29-
end
30-
3110
def last_updated_by(current_user)
3211
{ updated_by: current_user }
3312
end
34-
35-
def execute_state_change(content_item, state)
36-
if state && content_item.can_transition?(state)
37-
state_method = "#{state}!"
38-
content_item.send(state_method)
39-
end
40-
end
4113
end
4214
end
4315
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Cortex
2+
class UpdateContentItemTransaction < Cortex::ApplicationTransaction
3+
include Dry::Transaction(container: Cortex::TransactionContainer)
4+
include Cortex::ContentItemable
5+
6+
around :database_transact, with: "cortex.database_transact"
7+
step :init
8+
step :process
9+
step :latest_history_patch
10+
step :parse_content_item_field_items, with: "cortex.parse_content_item_field_items"
11+
step :persist_content_item, with: "cortex.persist_content_item"
12+
step :execute_content_item_state_change, with: "cortex.execute_content_item_state_change"
13+
14+
def init(input)
15+
Success(ContentItemTransactionType.new(input))
16+
end
17+
18+
def process(input)
19+
if input.content_item
20+
content_item = input.content_item
21+
else
22+
content_item = ContentItem.find(input.id)
23+
field_items_attributes(input.content_item_params).each do |_key, field_item_attributes|
24+
content_item.field_items << UpdateFieldItemTransaction.new.call(field_item_attributes).value!
25+
end
26+
27+
input.content_item_params.delete('field_items_attributes')
28+
content_item.assign_attributes(input.content_item_params)
29+
end
30+
31+
Success({
32+
content_item: content_item,
33+
current_user: input.current_user
34+
})
35+
end
36+
37+
def latest_history_patch(input)
38+
history_patch = {}
39+
history_patch.merge! last_updated_by(input[:current_user])
40+
41+
input[:content_item].assign_attributes(history_patch)
42+
Success(input[:content_item])
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)