Module: ActsAsPublished

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/acts_as_published.rb

Overview

ActsAsPublished

Adds published and draft scopes. Adds published? and draft? methods

add_column :things, :published_start_at, :datetime add_column :things, :published_end_at, :datetime

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#draft!Object



65
66
67
# File 'app/models/concerns/acts_as_published.rb', line 65

def draft!
  update!(published_start_at: nil, published_end_at: nil)
end

#draft?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'app/models/concerns/acts_as_published.rb', line 77

def draft?
  return true if published_start_at.blank? || published_start_at > Time.zone.now
  return true if published_end_at.present? && published_end_at <= Time.zone.now
  false
end

#publish!Object

Instance Methods



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/concerns/acts_as_published.rb', line 51

def publish!
  now = Time.zone.now

  if published_start_at.blank? || published_start_at > now
    assign_attributes(published_start_at: now.beginning_of_day) 
  end

  if published_end_at.present? && (published_end_at <= now || published_end_at <= published_start_at)
    assign_attributes(published_end_at: nil)
  end

  save!
end

#published?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'app/models/concerns/acts_as_published.rb', line 69

def published?
  return false if published_start_at.blank? || published_start_at > Time.zone.now
  return false if published_end_at.present? && published_end_at <= Time.zone.now
  return false if try(:archived?)

  true
end

#save_as_draftObject

For the form



84
85
86
# File 'app/models/concerns/acts_as_published.rb', line 84

def save_as_draft
  persisted? && published_start_at.blank? && published_end_at.blank?
end