Module: ConcernsOnRails::Models::Publishable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/publishable.rb
Constant Summary collapse
- SCOPE_BASES =
%i[published unpublished scheduled draft].freeze
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
198 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 198 def after_publish; end |
#after_unpublish ⇒ Object
200 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 200 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).
197 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 197 def before_publish; end |
#before_unpublish ⇒ Object
199 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 199 def before_unpublish; end |
#draft? ⇒ Boolean
Never set — a true draft.
239 240 241 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 239 def draft? self[self.class.publishable_field].blank? end |
#publish! ⇒ Object
202 203 204 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 202 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). Raises on a boolean publishable column:
the Time would cast to true and silently publish NOW instead of
scheduling — a boolean column cannot represent a future publish.
Example:
record.publish_at!(1.day.from_now)
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 250 def publish_at!(time) if self.class.publishable_boolean_column? raise ArgumentError, "ConcernsOnRails::Models::Publishable: publish_at! needs a timestamp column, but " \ "'#{self.class.publishable_field}' is a boolean — a Time casts to true and would " \ "publish immediately. Use publish!/unpublish!, or a datetime column to schedule." end publishable_write_with_hooks(time, :publish) end |
#published? ⇒ Boolean
Check if the record is published Example:
record.published?
216 217 218 219 220 221 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 216 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.
231 232 233 234 235 236 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 231 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!
209 210 211 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 209 def unpublish! publishable_write_with_hooks(nil, :unpublish) end |
#unpublished? ⇒ Boolean
Check if the record is unpublished Example:
record.unpublished?
226 227 228 |
# File 'lib/concerns_on_rails/models/publishable.rb', line 226 def unpublished? !published? end |