Sha256: 06265d12de8e86b72d56d82b241e1f6c65ad61360fa797cbd4fc60d0b8061bd4

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module PushType
  module Publishable
    extend ActiveSupport::Concern

    included do
      enum status: [ :draft, :published ]

      scope :published, -> {
        not_trash.exposed.
          where(['push_type_nodes.status = ? AND push_type_nodes.published_at <= ?', self.statuses[:published], Time.zone.now]).
          where(['push_type_nodes.published_to IS NULL OR push_type_nodes.published_to > ?', Time.zone.now])
      }

      after_initialize :set_default_status
      before_save :set_published_at

      def published?
        super and !scheduled? and !expired?
      end
    end

    def scheduled?
      published_at? and published_at > Time.zone.now
    end

    def expired?
      published_to? and published_to < Time.zone.now
    end

    private

    def set_published_at
      case status
        when 'draft', self.class.statuses[:draft]         then self.published_at = nil
        when 'published', self.class.statuses[:published] then self.published_at ||= Time.zone.now
      end
    end

    def set_default_status
      self.status     ||= self.class.statuses[:draft]
    end

  end  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
push_type_core-0.3.3 app/models/concerns/push_type/publishable.rb
push_type_core-0.3.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.2.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.2.0 app/models/concerns/push_type/publishable.rb
push_type_core-0.2.0.beta2 app/models/concerns/push_type/publishable.rb