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

Instance Method Details

#after_publishObject



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

def after_publish; end

#after_unpublishObject



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

def after_unpublish; end

#before_publishObject

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_unpublishObject



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

def before_unpublish; end

#draft?Boolean

Never set — a true draft.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


226
227
228
# File 'lib/concerns_on_rails/models/publishable.rb', line 226

def unpublished?
  !published?
end