Sha256: cd52827f5ca298ed2b10163710fb5a2bd55225d8c2c8c934d9d2ff2fa8a78699

Contents?: true

Size: 1.58 KB

Versions: 18

Compression:

Stored size: 1.58 KB

Contents

module Publishable
  
  extend ActiveSupport::Concern

  # Constants
  PUBLISHED = "published"
  UNPUBLISHED = "unpublished"
  REMOVED = "removed"
  ARCHIVED = "archived"
  
  STATUS = {"Published" => PUBLISHED, "Unpublished" => UNPUBLISHED, "Archived" => ARCHIVED, "Removed" => REMOVED}
  STATUS_REVERSE = {PUBLISHED => "Published", UNPUBLISHED => "Unpublished", ARCHIVED => "Archived", REMOVED => "Removed"}
  STATUS_UI_CLASS = {PUBLISHED => "success", UNPUBLISHED => "default", ARCHIVED => "default", REMOVED => "danger"}
  STATUS_UI_ICON = {PUBLISHED => "fa-check", UNPUBLISHED => "fa-square-o", ARCHIVED => "fa-archive", REMOVED => "fa-remove"}

  included do

	  validates :status, :presence=> true, :inclusion => {:in => STATUS_REVERSE.keys, :presence_of => :status, :message => "%{value} is not a valid status" }
	  
    state_machine :status, initial: UNPUBLISHED do
      event :publish do
	      transition [UNPUBLISHED, ARCHIVED] => PUBLISHED
	    end
	    event :unpublish do
	      transition [PUBLISHED, REMOVED, ARCHIVED] => UNPUBLISHED
	    end
	    event :remove do
	      transition [PUBLISHED, UNPUBLISHED, ARCHIVED] => REMOVED
	    end
	    event :archive do
	      transition [PUBLISHED, UNPUBLISHED, REMOVED] => ARCHIVED
	    end
    end

    scope :published, -> { where(status: PUBLISHED) }
	  scope :unpublished, -> { where(status: UNPUBLISHED) }
	  scope :removed, -> { where(status: REMOVED) }
	  scope :archived, -> { where(status: ARCHIVED) }

	  scope :status, lambda { |status| where("LOWER(status)='#{status}'") }
  end

  def display_status
    STATUS_REVERSE[self.status]
  end

end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
kuppayam-0.2.12.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.11.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.10.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.9.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.8.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.7.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.6.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.5.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.4.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.3.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.2.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.1.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.2.0.pre.materialize app/models/concerns/publishable.rb
kuppayam-0.1.39 app/models/concerns/publishable.rb
kuppayam-0.1.38 app/models/concerns/publishable.rb
kuppayam-0.1.37 app/models/concerns/publishable.rb
kuppayam-0.1.36 app/models/concerns/publishable.rb
kuppayam-0.1.35 app/models/concerns/publishable.rb