Module: Postablr::ArPublish

Extended by:
ActiveSupport::Concern
Included in:
Entry
Defined in:
app/models/postablr/ar_publish.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/postablr/ar_publish.rb', line 139

def expired?
  (!unpublish_at.nil? && unpublish_at < Time.now)
end

#fill_default_publishObject



111
112
113
# File 'app/models/postablr/ar_publish.rb', line 111

def fill_default_publish
  self.publish_at = Time.now if self.publish_at.nil?
end

#publishObject

Indefinitely publish the current object right now



144
145
146
147
148
149
# File 'app/models/postablr/ar_publish.rb', line 144

def publish
  return if published?
  self.is_published = true
  self.publish_at = Time.now
  self.unpublish_at = nil
end

#publish!Object

Same as publish, but immediatly saves the object. Raises an error when saving fails.



153
154
155
156
# File 'app/models/postablr/ar_publish.rb', line 153

def publish!
  publish
  save!
end

#publish_by_defaultObject

ActiveRecrod callback fired on before_create to make sure a new object always gets a publication date; if none is supplied it defaults to the creation date.



118
119
120
# File 'app/models/postablr/ar_publish.rb', line 118

def publish_by_default
  self.is_published = true if is_published.nil?
end

#published?Boolean

Return whether the current object is published or not

Returns:

  • (Boolean)


131
132
133
# File 'app/models/postablr/ar_publish.rb', line 131

def published?
  (is_published? && (publish_at <=> Time.now) <= 0) && (unpublish_at.nil? || (unpublish_at <=> Time.now) >= 0)
end

#unpublishObject

Immediatly unpublish the current object



159
160
161
162
# File 'app/models/postablr/ar_publish.rb', line 159

def unpublish
  return unless published?
  self.is_published = false
end

#unpublish!Object

Same as unpublish, but immediatly saves the object. Raises an error when saving files.



166
167
168
169
# File 'app/models/postablr/ar_publish.rb', line 166

def unpublish!
  unpublish
  save!
end

#upcoming?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/postablr/ar_publish.rb', line 135

def upcoming?
  (is_published? && publish_at > Time.now)
end

#validate_publish_date_consistencyObject

Validate that unpublish_at is greater than publish_at publish_at must not be nil



124
125
126
127
128
# File 'app/models/postablr/ar_publish.rb', line 124

def validate_publish_date_consistency
  if unpublish_at && publish_at && unpublish_at <= publish_at
    errors.add(:unpublish_at,"should be greater than publication date or empty")
  end
end