Module: ConcernsOnRails::Models::Publishable

Extended by:
ActiveSupport::Concern
Defined in:
lib/concerns_on_rails/models/publishable.rb

Instance Method Summary collapse

Instance Method Details

#after_publishObject



89
# File 'lib/concerns_on_rails/models/publishable.rb', line 89

def after_publish; end

#after_unpublishObject



91
# File 'lib/concerns_on_rails/models/publishable.rb', line 91

def after_unpublish; end

#before_publishObject

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_unpublishObject



90
# File 'lib/concerns_on_rails/models/publishable.rb', line 90

def before_unpublish; end

#draft?Boolean

Never set — a true draft.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


117
118
119
# File 'lib/concerns_on_rails/models/publishable.rb', line 117

def unpublished?
  !published?
end