Skip to content

Commit 2d42fa7

Browse files
committed
feat(Publish State): extract & refactor active_state logic to Transaction
1 parent ab70e22 commit 2d42fa7

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

app/models/cortex/content_item.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ class ContentItem < Cortex::ApplicationRecord
3232
end
3333

3434
def publish_state
35-
# TODO: move logic to Transaction
36-
PublishStateService.new.content_item_state(self)
35+
GetPublishStateTransaction.new.call(self).value!
3736
end
3837

3938
# FieldItem and State Convenience Methods. TODO: move to concern? transactions?

app/services/cortex/application_service.rb

-7
This file was deleted.

app/services/cortex/publish_state_service.rb

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# TODO: The entire Publish system needs to be reworked to avoid hardcoded references and data integrity issues
2+
3+
module Cortex
4+
class GetPublishStateTransaction < Cortex::ApplicationTransaction
5+
step :sort_field_items
6+
step :process
7+
8+
def sort_field_items(content_item)
9+
sorted_field_items = content_item.field_items.select do |field_item|
10+
field_item.field.field_type_instance.is_a?(DateTimeFieldType) && !field_item.field.metadata["state"].nil?
11+
end.sort_by {|field_item| field_item.data["timestamp"]}.reverse
12+
13+
Success({
14+
sorted_field_items: sorted_field_items,
15+
content_item: content_item
16+
})
17+
end
18+
19+
def process(input)
20+
timestamp_field_item = input[:sorted_field_items].find do |field_item|
21+
field_item.data['timestamp'].present? && DateTime.now > DateTime.parse(field_item.data["timestamp"])
22+
end
23+
24+
active_state = timestamp_field_item ? timestamp_field_item.field.metadata["state"] : input[:content_item].state.titleize
25+
Success(active_state)
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)