Sha256: 07aca13e0eaad34f84c74bc300416177badc995c98e5eff4e0a4e91995979202

Contents?: true

Size: 1.42 KB

Versions: 7

Compression:

Stored size: 1.42 KB

Contents

module Readable
  
  extend ActiveSupport::Concern

  # Constants
  NEW = "new"
  UNREAD = "unread"
  READ = "read"
  ARCHIVED = "archived"
  REMOVED = "removed"
  
  STATUS = {"New" => NEW, "Unread" => UNREAD, "Read" => READ, "Archived" => ARCHIVED, "Removed" => REMOVED}
  STATUS_REVERSE = {NEW => "New", UNREAD => "Unread", READ => "Read",ARCHIVED => "Archived", REMOVED => "Removed"}
  STATUS_UI_CLASS = {NEW => "success", UNREAD => "info", READ => "default", ARCHIVED => "default", REMOVED => "danger"}

  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: NEW do
      event :read do
	      transition [NEW, UNREAD] => READ
	    end
	    event :unread do
	      transition [READ, ARCHIVED, REMOVED] => UNREAD
	    end
	    event :remove do
	      transition [NEW, READ, UNREAD, ARCHIVED] => REMOVED
	    end
	    event :archive do
        transition [NEW, READ, UNREAD, REMOVED] => ARCHIVED
	    end
    end

    scope :new_ones, -> { where(status: NEW) }
    scope :read, -> { where(status: READ) }
	  scope :unread, -> { where(status: UNREAD) }
	  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

7 entries across 7 versions & 1 rubygems

Version Path
kuppayam-0.1.28 app/models/concerns/readable.rb
kuppayam-0.1.27 app/models/concerns/readable.rb
kuppayam-0.1.26 app/models/concerns/readable.rb
kuppayam-0.1.25 app/models/concerns/readable.rb
kuppayam-0.1.24 app/models/concerns/readable.rb
kuppayam-0.1.23 app/models/concerns/readable.rb
kuppayam-0.1.22 app/models/concerns/readable.rb