Sha256: f3b264ea6c47eb4e7e191b4caf2b28f9096fea6420aceffce967527bf8da3f73

Contents?: true

Size: 1.09 KB

Versions: 46

Compression:

Stored size: 1.09 KB

Contents

module PushType
  module Publishable
    extend ActiveSupport::Concern

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

      scope :published, -> {
        not_trash
          .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

46 entries across 46 versions & 1 rubygems

Version Path
push_type_core-0.12.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.12.0 app/models/concerns/push_type/publishable.rb
push_type_core-0.12.0.beta.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.11.2 app/models/concerns/push_type/publishable.rb
push_type_core-0.11.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.11.0.beta.2 app/models/concerns/push_type/publishable.rb
push_type_core-0.11.0.beta.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.4 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.3 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.2 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.0 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.0.beta.5 app/models/concerns/push_type/publishable.rb
push_type_core-0.10.0.beta.3 app/models/concerns/push_type/publishable.rb
push_type_core-0.9.5 app/models/concerns/push_type/publishable.rb
push_type_core-0.9.3 app/models/concerns/push_type/publishable.rb
push_type_core-0.9.2 app/models/concerns/push_type/publishable.rb
push_type_core-0.9.1 app/models/concerns/push_type/publishable.rb
push_type_core-0.9.0 app/models/concerns/push_type/publishable.rb
push_type_core-0.9.0.beta.4 app/models/concerns/push_type/publishable.rb