DEV: Refactor draft attributes for CategoryList and TopicList.

Avoid repeating the same logic in a bunch of places which will allow us
to make changes to the draft attributes easier in the future.
This commit is contained in:
Guo Xiang Tan
2020-07-24 10:11:30 +08:00
parent 1dd3af20c6
commit b979579c1b
9 changed files with 88 additions and 40 deletions
+33
View File
@@ -0,0 +1,33 @@
# frozen_string_literal: true
class DraftableList
include ActiveModel::Serialization
def initialize(user)
@current_user = user
end
def draft_key
@draft_key || Draft::NEW_TOPIC
end
def draft_sequence
@draft_sequence || DraftSequence.current(@current_user, draft_key)
end
def draft
@draft || Draft.get(@current_user, draft_key, draft_sequence) if @current_user
end
def draft_key=(key)
@draft_key = key
end
def draft_sequence=(sequence)
@draft_sequence = sequence
end
def draft=(draft)
@draft = draft
end
end