Module: ArPublishControl::Publishable::InstanceMethods
- Defined in:
- lib/ar_publish_control.rb
Instance Method Summary collapse
- #after_initialize ⇒ Object
- #expired? ⇒ Boolean
-
#publish ⇒ Object
Indefinitely publish the current object right now.
-
#publish! ⇒ Object
Same as publish, but immediatly saves the object.
-
#publish_by_default ⇒ Object
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. -
#published? ⇒ Boolean
Return whether the current object is published or not.
-
#unpublish ⇒ Object
Immediatly unpublish the current object.
-
#unpublish! ⇒ Object
Same as unpublish, but immediatly saves the object.
- #upcoming? ⇒ Boolean
-
#validate_publish_date_consistency ⇒ Object
Validate that unpublish_at is greater than publish_at publish_at must not be nil.
Instance Method Details
#after_initialize ⇒ Object
158 159 160 |
# File 'lib/ar_publish_control.rb', line 158 def after_initialize write_attribute(:publish_at, Time.now) if publish_at.nil? end |
#expired? ⇒ Boolean
188 189 190 |
# File 'lib/ar_publish_control.rb', line 188 def expired? (!unpublish_at.nil? && unpublish_at < Time.now) end |
#publish ⇒ Object
Indefinitely publish the current object right now
193 194 195 196 197 198 |
# File 'lib/ar_publish_control.rb', line 193 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.
202 203 204 205 |
# File 'lib/ar_publish_control.rb', line 202 def publish! publish save! end |
#publish_by_default ⇒ Object
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.
165 166 167 |
# File 'lib/ar_publish_control.rb', line 165 def publish_by_default write_attribute(:is_published, true) if is_published.nil? end |
#published? ⇒ Boolean
Return whether the current object is published or not
180 181 182 |
# File 'lib/ar_publish_control.rb', line 180 def published? (is_published? && (publish_at <=> Time.now) <= 0) && (unpublish_at.nil? || (unpublish_at <=> Time.now) >= 0) end |
#unpublish ⇒ Object
Immediatly unpublish the current object
208 209 210 211 |
# File 'lib/ar_publish_control.rb', line 208 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.
215 216 217 218 |
# File 'lib/ar_publish_control.rb', line 215 def unpublish! unpublish save! end |
#upcoming? ⇒ Boolean
184 185 186 |
# File 'lib/ar_publish_control.rb', line 184 def upcoming? (is_published? && publish_at > Time.now) end |
#validate_publish_date_consistency ⇒ Object
Validate that unpublish_at is greater than publish_at publish_at must not be nil
171 172 173 174 175 |
# File 'lib/ar_publish_control.rb', line 171 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 |