Sha256: ee3d95d9e0a2c4d00c0f612cbc116f82c93c106753b47b2e07df853c8c4f2fdc

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

class Blog
  include BlogLogic::Base
  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
  has_many :blog_categories
  has_many :posts

  # Behavior =======================================================================================
  attr_accessor :desired_slug
  has_slug :desired_slug

  # Validations ====================================================================================
  validates_presence_of :title, :description, :desired_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}/".gsub(/\/\//,'/').gsub(/\/\//,'/')
  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blog_logic-1.2.1 app/models/blog.rb
blog_logic-1.2.0 app/models/blog.rb
blog_logic-1.1.9 app/models/blog.rb