Module: ArPublishControl::Publishable::InstanceMethods
- Defined in:
- lib/ar_publish_control_with_scopes.rb
Instance Method Summary collapse
- #after_initialize ⇒ Object
- #check_for_all_scopes(method_name) ⇒ Object
- #do_for_all_scopes(method_name) ⇒ 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
211 212 213 |
# File 'lib/ar_publish_control_with_scopes.rb', line 211 def after_initialize write_attribute(:publish_at, Time.now) if publish_at.nil? end |
#check_for_all_scopes(method_name) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/ar_publish_control_with_scopes.rb', line 236 def check_for_all_scopes(method_name) res = true self.class.publish_scopes.each do |scope| res &= send "#{scope}_#{method_name}" end res end |
#do_for_all_scopes(method_name) ⇒ Object
244 245 246 247 248 |
# File 'lib/ar_publish_control_with_scopes.rb', line 244 def do_for_all_scopes(method_name) self.class.publish_scopes.each do |scope| send "#{scope}_#{method_name}" end end |
#expired? ⇒ Boolean
270 271 272 |
# File 'lib/ar_publish_control_with_scopes.rb', line 270 def expired? (!unpublish_at.nil? && unpublish_at < Time.now) end |
#publish ⇒ Object
Indefinitely publish the current object right now
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/ar_publish_control_with_scopes.rb', line 275 def publish if self.class.publish_scopes.empty? return if published? self.is_published = true self.publish_at = Time.now self.unpublish_at = nil else do_for_all_scopes :publish end end |
#publish! ⇒ Object
Same as publish, but immediatly saves the object. Raises an error when saving fails.
288 289 290 291 |
# File 'lib/ar_publish_control_with_scopes.rb', line 288 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.
218 219 220 221 222 223 224 225 226 |
# File 'lib/ar_publish_control_with_scopes.rb', line 218 def publish_by_default if self.class.publish_scopes.empty? write_attribute(:is_published, true) if is_published.nil? else self.class.publish_scopes.each do |scope| write_attribute("is_#{scope}_published", true) if self.read_attribute("is_#{scope}_published").nil? end end end |
#published? ⇒ Boolean
Return whether the current object is published or not
252 253 254 255 256 257 258 259 |
# File 'lib/ar_publish_control_with_scopes.rb', line 252 def published? p = if self.class.publish_scopes.empty? is_published? else check_for_all_scopes('published?') end (p && (publish_at <=> Time.now) <= 0) && (unpublish_at.nil? || (unpublish_at <=> Time.now) >= 0) end |
#unpublish ⇒ Object
Immediatly unpublish the current object
294 295 296 297 298 299 300 |
# File 'lib/ar_publish_control_with_scopes.rb', line 294 def unpublish if self.class.publish_scopes.empty? self.is_published = false else do_for_all_scopes :unpublish end end |
#unpublish! ⇒ Object
Same as unpublish, but immediatly saves the object. Raises an error when saving files.
304 305 306 307 |
# File 'lib/ar_publish_control_with_scopes.rb', line 304 def unpublish! unpublish save! end |
#upcoming? ⇒ Boolean
261 262 263 264 265 266 267 268 |
# File 'lib/ar_publish_control_with_scopes.rb', line 261 def upcoming? p = if self.class.publish_scopes.empty? is_published? else check_for_all_scopes('published?') end (p && 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
230 231 232 233 234 |
# File 'lib/ar_publish_control_with_scopes.rb', line 230 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 |