app/models/feature.rb in usman-0.3.16 vs app/models/feature.rb in usman-0.3.17

- old
+ new

@@ -1,33 +1,18 @@ class Feature < Usman::ApplicationRecord - # Constants - PUBLISHED = "published" - UNPUBLISHED = "unpublished" - DISABLED = "disabled" - - STATUS = { - PUBLISHED => "Published", - UNPUBLISHED => "Un-Published", - DISABLED => "Disabled" - } + # Including the State Machine Methods + include Publishable - STATUS_REVERSE = { - "Published" => PUBLISHED, - "Un-Published" => UNPUBLISHED, - "Disabled" => DISABLED - } - - # Associations + # Associations has_many :permissions has_many :users, through: :permissions - has_one :feature_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::FeatureImage" + # has_one :feature_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::FeatureImage" # Validations validates :name, presence: true, length: {minimum: 3, maximum: 250} - validates :status, :presence => true, :inclusion => {:in => STATUS.keys, :presence_of => :status, :message => "%{value} is not a valid status" } - + # ------------------ # Class Methods # ------------------ # return an active record relation object with the search query in its where clause @@ -36,18 +21,12 @@ # >>> feature.search(query) # => ActiveRecord::Relation object scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%')") } - scope :status, lambda { |status| where("LOWER(status)='#{status}'") } - scope :categorisable, -> { where(categorisable: true) } - scope :unpublished, -> { where(status: UNPUBLISHED) } - scope :published, -> { where(status: PUBLISHED) } - scope :disabled, -> { where(status: DISABLED) } - def self.save_row_data(hsh) # Initializing error hash for displaying all errors altogether error_object = Kuppayam::Importer::ErrorHash.new @@ -75,84 +54,18 @@ # ------------------ # Instance Methods # ------------------ - # Status Methods - # -------------- - - # * Return true if the user is not published, else false. - # == Examples - # >>> feature.published? - # => true - def published? - (status == PUBLISHED) - end - - # * Return true if the user is unpublished, else false. - # == Examples - # >>> feature.unpublished? - # => true - def unpublished? - (status == UNPUBLISHED) - end - - # * Return true if the user is disabled, else false. - # == Examples - # >>> feature.disabled? - # => true - def disabled? - (status == DISABLED) - end - - # change the status to :unpublished - # Return the status - # == Examples - # >>> feature.unpublish! - # => "unpublished" - def unpublish! - self.update_attribute(:status, UNPUBLISHED) - end - - # change the status to :published - # Return the status - # == Examples - # >>> feature.publish! - # => "published" - def publish! - self.update_attribute(:status, PUBLISHED) - end - - # change the status to :suspended - # Return the status - # == Examples - # >>> feature.disable! - # => "disabled" - def disable! - self.update_attribute(:status, DISABLED) - end - # Permission Methods # ------------------ def can_be_edited? published? or unpublished? end def can_be_deleted? true - end - - def can_be_published? - unpublished? or disabled? - end - - def can_be_unpublished? - published? or disabled? - end - - def can_be_disabled? - published? or unpublished? end # Other Methods # ------------- \ No newline at end of file