Module: Storytime::Concerns::HasVersions
- Extended by:
- ActiveSupport::Concern
- Included in:
- Post
- Defined in:
- lib/storytime/concerns/has_versions.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #activate_version ⇒ Object
- #create_version ⇒ Object
-
#draft_content ⇒ Object
TO USE IN MODEL: include Storytime::Concerns::HasVersions self.draft_content_column = :html (defaults to :content) in controller, add @post.draft_user_id = current_user.id to #create and #update.
- #latest_version ⇒ Object
- #publish! ⇒ Object
- #published ⇒ Object
- #published=(val) ⇒ Object
- #published? ⇒ Boolean
Instance Method Details
#activate_version ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/storytime/concerns/has_versions.rb', line 32 def activate_version if @draft_version_id version = Storytime::Version.find(@draft_version_id) if self.update_columns(self.class.draft_content_column => version.content) version.touch end end end |
#create_version ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/storytime/concerns/has_versions.rb', line 20 def create_version unless self.draft_content.blank? if self.latest_version.nil? || self.draft_content != self.latest_version.content version = self.versions.new version.content = self.draft_content version.user_id = self.draft_user_id version.save end end self.publish! if self.published? end |
#draft_content ⇒ Object
TO USE IN MODEL: include Storytime::Concerns::HasVersions self.draft_content_column = :html (defaults to :content) in controller, add @post.draft_user_id = current_user.id to #create and #update
11 12 13 |
# File 'lib/storytime/concerns/has_versions.rb', line 11 def draft_content @draft_content || (self.latest_version.present? ? self.latest_version.content : "") end |
#latest_version ⇒ Object
15 16 17 18 |
# File 'lib/storytime/concerns/has_versions.rb', line 15 def latest_version # @latest_version ||= self.versions.order(updated_at: :desc, id: :desc).first self.versions.order(updated_at: :desc).first end |
#publish! ⇒ Object
41 42 43 44 45 |
# File 'lib/storytime/concerns/has_versions.rb', line 41 def publish! self.published = "1" attrs = {self.class.draft_content_column => (self.latest_version.present? ? self.latest_version.content : "") } self.update_columns(attrs) end |
#published ⇒ Object
66 67 68 |
# File 'lib/storytime/concerns/has_versions.rb', line 66 def published !published_at.nil? end |
#published=(val) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/storytime/concerns/has_versions.rb', line 47 def published=(val) if val == "1" || val == "true" || val == true if self.published_at_date || self.published_at_time self.published_at = if self.published_at_time.nil? DateTime.parse "#{self.published_at_date}" elsif self.published_at_date.nil? DateTime.parse "#{Date.today} #{self.published_at_time}" else DateTime.parse "#{self.published_at_date} #{self.published_at_time}" end else self.published_at = Time.now unless self.published_at end else self.published_at_date = nil self.published_at_time = nil end end |
#published? ⇒ Boolean
70 71 72 |
# File 'lib/storytime/concerns/has_versions.rb', line 70 def published? published end |