Sha256: c5eb5e3c555313897784cb5b6bb3e41660512c3b0eaa8715909ae03841639113
Contents?: true
Size: 1.81 KB
Versions: 3
Compression:
Stored size: 1.81 KB
Contents
# # ActsAsPublished # # Adds published and draft scopes. Adds published? and draft? methods # # add_column :things, :published_start_at, :datetime # add_column :things, :published_end_at, :datetime # module ActsAsPublished extend ActiveSupport::Concern module Base def acts_as_published(options = nil) include ::ActsAsPublished end end module ClassMethods def acts_as_published?; true; end end included do attr_writer :save_as_draft before_validation do assign_attributes(published_start_at: nil, published_end_at: nil) if EffectiveResources.truthy?(@save_as_draft) end validate(if: -> { published_start_at.present? && published_end_at.present? }) do errors.add(:published_end_at, 'must be after the published start date') if published_end_at <= published_start_at end scope :draft, -> { where(published_start_at: nil) .or(where(arel_table[:published_start_at].gt(Time.zone.now))) .or(where(arel_table[:published_end_at].lteq(Time.zone.now))) } scope :published, -> { (try(:unarchived) || all) .where(arel_table[:published_start_at].lteq(Time.zone.now)) .where(published_end_at: nil).or(where(arel_table[:published_end_at].gteq(Time.zone.now))) } end # Instance Methods def published? return false if published_start_at.blank? || published_start_at > Time.zone.now return false if published_end_at.present? && published_end_at <= Time.zone.now return false if try(:archived?) true end def draft? return true if published_start_at.blank? || published_start_at > Time.zone.now return true if published_end_at.present? && published_end_at <= Time.zone.now false end # For the form def save_as_draft persisted? && published_start_at.blank? && published_end_at.blank? end end
Version data entries
3 entries across 3 versions & 1 rubygems