Class: Blog

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/blog.rb,
app/models/blog.posts_finder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_orderObject



79
80
81
# File 'app/models/blog.rb', line 79

def self.default_order
  "name"
end

.default_templateObject



19
20
21
22
23
24
25
# File 'app/models/blog.rb', line 19

def self.default_template
  template_file = ActionController::Base.view_paths.map do |vp|
    path = vp.to_s.first == "/" ? vp.to_s : File.join(Rails.root, vp.to_s)
    File.join(path, "cms/blogs/render.html.erb")
  end.detect{|f| File.exists? f }
  template_file ? open(template_file){|f| f.read } : ""
end

.posts_finder(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/blog.rb', line 27

def self.posts_finder(finder, options)
  if options[:tags]
    finder = finder.tagged_with(options[:tags])
  end
  if options[:exclude_tags]
    finder = finder.not_tagged_with(options[:exclude_tags])
  end
  if options[:category] || options[:category_id]
    category_type = CategoryType.named("Blog Post").first
    category = category_type.categories.named(options[:category]).first if options[:category]
    category = category_type.categories. find(options[:category_id])    if options[:category_id]
    finder = finder.in_category(category)
  end
  finder
end

Instance Method Details

#editable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/blog.rb', line 83

def editable_by?(user)
  user.able_to?(:administrate) || !(group_ids & user.group_ids).empty?
end

#inline_optionsObject



75
76
77
# File 'app/models/blog.rb', line 75

def inline_options
  {:inline => self.template}
end

#name_for_pathObject




94
95
96
# File 'app/models/blog.rb', line 94

def name_for_path
  name.to_slug.gsub('-', '_')
end

#potential_authorsObject



87
88
89
# File 'app/models/blog.rb', line 87

def potential_authors
  groups.map(&:users).flatten.uniq
end

#renderObject

Raises:

  • (ActiveRecord::RecordNotFound)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/blog.rb', line 43

def render
  @blog = self
  finder = @blog.posts.published
  finder = Blog.posts_finder(finder, params)

  if params[:year] && params[:month] && params[:day]
    @date = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
    finder = posts.published_between(@date, @date + 1.day)
  elsif params[:year] && params[:month]
    @date = Date.new(params[:year].to_i, params[:month].to_i)
    finder = posts.published_between(@date, @date + 1.month)
  elsif params[:year]
    @date = Date.new(params[:year].to_i)
    finder = posts.published_between(@date, @date + 1.year)
  end

  @blog_posts = finder.all(:limit => 25, :order => "published_at desc")
  raise ActiveRecord::RecordNotFound.new("No posts found") if @blog_posts.empty?

  if params[:category]
    @page_title = "#{params[:category]}"
  elsif params[:tag]
    @page_title = "Posts tagged with #{params[:tag]}"
  elsif params[:year] && params[:month] && params[:day]
    @page_title = "Posts from #{@date.to_s(:long)}"
  elsif params[:year] && params[:month]
    @page_title = "Posts from #{Date::MONTHNAMES[@date.month]} #{@date.year}"
  elsif params[:year]
    @page_title = "Posts from #{@date.year}"
  end
end