Sha256: 84ffcf000afef0cccf67ab893aaef59881f5c46f627391ff8bd6b88c25ff5ce2

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module PagesCms
  class Article < ActiveRecord::Base

    belongs_to :image

    validates :title,   presence: true
    validates :content, presence: true
    validates :tags,    presence: true


    def tags=(val)
      write_attribute(:tags, val.split(',') )
    end

    def self.published
      self.where('archived != true AND draft != true')
    end

    def article_status
      if self.draft && !self.archived
        'Draft'
      elsif self.archived && !self.draft
        'Archived'
      elsif !self.draft && !self.archived
        'Published'
      elsif self.draft && self.archived
        'Archived'
      end
    end

    scope :search, -> (search)  { where('content ILIKE ? OR tags @> ? OR title ILIKE ? ',"#{search}",'{' + search + '}',"#{search}") }
    scope :status, -> (status) do
      case status
        when 'Archived'
          where(archived: true)
        when 'Drafts'
          where(draft: true)
        when 'Published'
          where('archived != true AND draft != true')
        else
          all
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_cms-1.0.8 app/models/pages_cms/article.rb
pages_cms-1.0.6 app/models/pages_cms/article.rb