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
79 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 79 def after_publish; end |
#after_unpublish ⇒ Object
81 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 81 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).
78 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 78 def before_publish; end |
#before_unpublish ⇒ Object
80 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 80 def before_unpublish; end |
#draft? ⇒ Boolean
Never set — a true draft.
126 127 128 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 126 def draft? self[self.class.publishable_field].blank? end |
#publish! ⇒ Object
83 84 85 86 87 88 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 83 def publish! before_publish result = update(self.class.publishable_field => Time.zone.now) after_publish if result result end |
#publish_at!(time) ⇒ Object
Publish at an explicit time. A future time schedules the record. Example:
record.publish_at!(1.day.from_now)
133 134 135 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 133 def publish_at!(time) update(self.class.publishable_field => time) end |
#published? ⇒ Boolean
Check if the record is published Example:
record.published?
103 104 105 106 107 108 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 103 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.
118 119 120 121 122 123 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 118 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!
93 94 95 96 97 98 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 93 def unpublish! before_unpublish result = update(self.class.publishable_field => nil) after_unpublish if result result end |
#unpublished? ⇒ Boolean
Check if the record is unpublished Example:
record.unpublished?
113 114 115 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 113 def unpublished? !published? end |