Sha256: 1e4c995b169db47f8b5c078a9106bc11da989586bc0c8e33d546edaf064ef081

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

module Blogit
  class Post < ActiveRecord::Base

    require "acts-as-taggable-on"
    require "kaminari"
    
    acts_as_taggable    

    self.table_name = "blog_posts"

    self.paginates_per Blogit.configuration.posts_per_page

    # ===============
    # = Validations =
    # ===============

    validates :title, presence: true, length: { minimum: 10, maximum: 66 }
    validates :body,  presence: true, length: { minimum: 10 }
    validates :blogger_id, presence: true

    # =================
    # = Assosciations =
    # =================    

    belongs_to :blogger, :polymorphic => true

    if Blogit.configuration.include_comments 
      has_many :comments, :class_name => "Blogit::Comment"
    end

    # ==========
    # = Scopes =
    # ==========

    # Returns the blog posts paginated for the index page
    # @scope class
    scope :for_index, lambda { |page_no = 1| order("created_at DESC").page(page_no) }

    # ====================
    # = Instance Methods =
    # ====================

    def to_param
      "#{id}-#{title.parameterize}"
    end

    # If there's a current blogger and the display name method is set, returns the blogger's display name
    # Otherwise, returns an empty string
    def blogger_display_name
      if self.blogger and !self.blogger.respond_to?(Blogit.configuration.blogger_display_name_method)
        raise ConfigurationError, 
        "#{self.blogger.class}##{Blogit.configuration.blogger_display_name_method} is not defined"
      elsif self.blogger.nil?
        ""
      else
        self.blogger.send Blogit.configuration.blogger_display_name_method        
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
blogit-0.4.8 app/models/blogit/post.rb
blogit-0.4.7 app/models/blogit/post.rb
blogit-0.4.6 app/models/blogit/post.rb
blogit-0.4.5 app/models/blogit/post.rb
blogit-0.4.4 app/models/blogit/post.rb
blogit-0.4.3 app/models/blogit/post.rb