Sha256: 5a0c9f323ecb2e213e5e448ff189b7c741db41b0347a8ef799fc5c7ddfd3e5b2

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

class Blog
  include LuckySneaks::StringExtensions
  include Mongoid::Document
  include Mongoid::Timestamps

  # Scopes =========================================================================================
  scope :by_slug, lambda {|slug| {:where => {:slug => "#{slug}".gsub('//','/')} } }

  # Mongoid ========================================================================================
  field :title
  field :slug
  field :description
  field :default_author
  field :posts_per_page, :type => Integer
  field :rss_enabled, :type => Boolean
  field :has_topics, :type => Boolean, :default => false
  index :slug
  has_many :blog_categories
  has_many :posts, :dependent => :destroy

  # Behavior =======================================================================================
  before_save :set_slug
  validates_presence_of :description, :title
  validates_uniqueness_of :slug

  # Instance methods ===============================================================================
  def copy_posts
    self.posts.each do |post|
      p = Post2.create :slug => post.slug, :content => post.content, :tags => post.tags, :author => post.author, :published_at => post.published_at, :state => post.state, :publication_date => post.publication_date, :summary => post.summary
      p.title = post.title
      p.blog_category_ids = post.blog_category_ids
      p.save
      self.post2s << p
    end
  end

  def feed_address
    "/#{self.slug}/feed.rss"
  end

  def humanize_path
    self.path
  end

  def path
    "/#{self.slug}"
  end

  def posts_by_month
    dates = {}
    self.posts.published.each do |p|
      date = p.publication_date.to_s :year_month
      dates[date] ||= {}
      dates[date][:full_date] ||= p.publication_date.to_s(:month_year)
      dates[date][:posts] ||= []
      dates[date][:posts] << p
    end
    dates
  end

  def search(keyword)
    self.posts.published.where :content => /#{keyword}/i
  end

  private

  def set_slug
    self.slug = self.title.to_s.to_url if self.slug.blank?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
blog_logic-1.3.0 app/models/blog.rb
blog_logic-1.2.4 app/models/blog.rb
blog_logic-1.2.3 app/models/blog.rb
blog_logic-1.2.2 app/models/blog.rb