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



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

def after_publish; end

#after_unpublishObject



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

def after_unpublish; end

#before_publishObject

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_unpublishObject



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

def before_unpublish; end

#draft?Boolean

Never set — a true draft.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


113
114
115
# File 'lib/concerns_on_rails/models/publishable.rb', line 113

def unpublished?
  !published?
end