Module: ConcernsOnRails::Models::Publishable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/publishable.rb
Instance Method Summary collapse
- #after_publish ⇒ Object
- #after_unpublish ⇒ Object
-
#before_publish ⇒ Object
Instance methods Publish the record Example: record.publish! Lifecycle hooks — override in the model (mirrors SoftDeletable's hooks).
- #before_unpublish ⇒ Object
-
#draft? ⇒ Boolean
Never set — a true draft.
- #publish! ⇒ Object
-
#publish_at!(time) ⇒ Object
Publish at an explicit time.
-
#published? ⇒ Boolean
Check if the record is published Example: record.published?.
-
#scheduled? ⇒ Boolean
Set, but the publish time is still in the future.
-
#unpublish! ⇒ Object
Unpublish the record Example: record.unpublish!.
-
#unpublished? ⇒ Boolean
Check if the record is unpublished Example: record.unpublished?.
Instance Method Details
#after_publish ⇒ Object
89 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 89 def after_publish; end |
#after_unpublish ⇒ Object
91 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 91 def after_unpublish; end |
#before_publish ⇒ Object
Instance methods Publish the record Example:
record.publish!
Lifecycle hooks — override in the model (mirrors SoftDeletable's hooks).
88 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 88 def before_publish; end |
#before_unpublish ⇒ Object
90 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 90 def before_unpublish; end |
#draft? ⇒ Boolean
Never set — a true draft.
130 131 132 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 130 def draft? self[self.class.publishable_field].blank? end |
#publish! ⇒ Object
93 94 95 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 93 def publish! publishable_write_with_hooks(Time.zone.now, :publish) end |
#publish_at!(time) ⇒ Object
Publish at an explicit time. A future time schedules the record. Fires the publish hooks (same state change as publish!, so since 1.22 they no longer silently skip). Example:
record.publish_at!(1.day.from_now)
139 140 141 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 139 def publish_at!(time) publishable_write_with_hooks(time, :publish) end |
#published? ⇒ Boolean
Check if the record is published Example:
record.published?
107 108 109 110 111 112 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 107 def published? value = self[self.class.publishable_field] return false unless value.present? value.respond_to?(:<=) ? value <= Time.zone.now : true end |
#scheduled? ⇒ Boolean
Set, but the publish time is still in the future.
122 123 124 125 126 127 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 122 def scheduled? value = self[self.class.publishable_field] return false if value.blank? value.respond_to?(:>) ? value > Time.zone.now : false end |
#unpublish! ⇒ Object
Unpublish the record Example:
record.unpublish!
100 101 102 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 100 def unpublish! publishable_write_with_hooks(nil, :unpublish) end |
#unpublished? ⇒ Boolean
Check if the record is unpublished Example:
record.unpublished?
117 118 119 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 117 def unpublished? !published? end |