Sha256: 90b8686b2590b21992c29e047cfe213b4ed5db014db359d1c1f64837f4a71f8a

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module PiccoBlog
  class Post < ActiveRecord::Base
    extend FriendlyId
    friendly_id :title, use: [:slugged, :history]
    acts_as_taggable_on :tags
    dragonfly_accessor :featured_image

    paginates_per PiccoBlog.posts_per_page

    attr_accessor :author_id

    belongs_to :author, class_name: PiccoBlog.author_class.to_s
    has_many :comments

    validates :title, :text, :state, presence: true
    validates_property :format, of: :featured_image, in: [:jpeg, :jpg, :png], case_sensitive: false,
                        message: "should be either .jpeg, .jpg, .png", if: :featured_image_changed?

    before_validation :set_author

    enum state: [:visible, :hidden]

    private

      def self.search(term)
        posts = self.arel_table
        query_string = "%#{term}%"
        param_matches_string =  ->(param){ 
          posts[param].matches(query_string) 
        } 

        self.where(param_matches_string.(:title).or(param_matches_string.(:text)))
      end

      def set_author
        self.author = PiccoBlog.author_class.constantize.find(author_id)
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
picco_blog-1.4.3 app/models/picco_blog/post.rb
picco_blog-1.4.2 app/models/picco_blog/post.rb
picco_blog-1.4.1 app/models/picco_blog/post.rb