public
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/halorgium/mephisto.git
commit  429ecf2948716ebb2905174653643eba4299a97e
tree    2979eb47fed9cd8cca2404211066956fc0eaaf9f
parent  5460e71c367d46d55c290804d2c2981052977a04
mephisto / app / controllers / mephisto_controller.rb
100644 141 lines (123 sloc) 5.976 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class MephistoController < ApplicationController
  layout nil
  session :off
  caches_page_with_references :dispatch
  cache_sweeper :comment_sweeper
 
  def dispatch
    @dispatch_path = Mephisto::Dispatcher.run(site, params[:path].dup)
    @dispatch_action = @dispatch_path.shift
    @section = @dispatch_path.shift
    @dispatch_action == :error ? show_404 : send("dispatch_#{@dispatch_action}")
  end
 
  protected
    def dispatch_redirect
      @skip_caching = true
      # @section is the http status
      # @dispatch_path.first has the headers
      if @dispatch_path.first.is_a?(Hash)
        response.headers['Status'] = interpret_status @section
        redirect_to @dispatch_path.first[:location], :status=>301
      else
        head @section
      end
    end
 
    def dispatch_list
      @articles = @section.articles.find_by_date(:include => :user, :limit => @section.articles_per_page)
      render_liquid_template_for(@section.show_paged_articles? ? :page : :section,
        'section' => @section.to_liquid(true), 'articles' => @articles)
    end
 
    def dispatch_page
      Article.with_published do
        @article = @dispatch_path.empty? ? @section.articles.find_by_position : @section.articles.find_by_permalink(@dispatch_path.first)
      end
      show_404 and return unless @article
      Mephisto::Liquid::CommentForm.article = @article
      render_liquid_template_for(:page, 'section' => @section.to_liquid(true),
                                        'article' => @article.to_liquid(:mode => :single, :page => @dispatch_path.empty?))
    end
    
    def dispatch_comments
      @skip_caching = true
      show_404 and return unless find_article
      if !request.post? || params[:comment].blank?
        redirect_to site.permalink_for(@article) and return
      end
 
      @comment = @article.comments.build(params[:comment].merge(:user_id => session[:user], :author_ip => request.remote_ip, :user_agent => request.user_agent, :referrer => request.referer))
      @comment.check_approval(site, request, :authenticated => admin?) if @comment.valid?
      @comment.save!
      redirect_to dispatch_path(:path => (site.permalink_for(@article)[1..-1].split('/') << 'comments' << @comment.id.to_s), :anchor => @comment.dom_id)
    rescue ActiveRecord::RecordInvalid
      show_article_with 'errors' => @comment.errors.full_messages, 'submitted' => params[:comment]
    rescue Article::CommentNotAllowed
      show_article_with 'errors' => ["Commenting has been disabled on this article"]
    rescue Comment::Previewing
      show_article_with 'errors' => ["Previewing your comment"], 'submitted' => params[:comment]
    end
    
    def dispatch_comment
      @skip_caching = true
      show_article_with 'message' => 'Thanks for the comment!'
    end
 
    def dispatch_archives
      year = @dispatch_path.shift
      month = @dispatch_path.shift
      if year
        month ||= '1'
      else
        year = Time.now.utc.year
        month = Time.now.utc.month
      end
      @articles = @section.articles.find_all_in_month(year, month, :include => :user)
      render_liquid_template_for(:archive, 'section' => @section, 'articles' => @articles, 'archive_date' => Time.utc(year, month))
    end
 
    def dispatch_search
      @section = params[:s].nil? ? nil : site.sections.detect { |s| s.path == params[:s] }
      joins = nil
      conditions = ['(published_at IS NOT NULL AND published_at <= :now) AND (title LIKE :q OR excerpt LIKE :q OR body LIKE :q)',
                       { :now => Time.now.utc, :q => "%#{params[:q]}%" }]
      if @section
        conditions.first << ' AND (assigned_sections.section_id = :section)'
        conditions.last[:section] = @section.id
      end
 
      @articles = site.articles.paginate(:conditions => conditions, :order => 'published_at DESC',
                                         :include => [:user, :sections],
                                         :per_page => site.articles_per_page, :page => params[:page])
      
      render_liquid_template_for(:search, 'articles' => @articles,
                                          'previous_page' => paged_search_url_for(@articles.previous_page),
                                          'next_page' => paged_search_url_for(@articles.next_page),
                                          'search_string' => CGI::escapeHTML(params[:q]),
                                          'search_count' => @articles.total_entries,
                                          'section' => @section)
      @skip_caching = true
    end
    
    def dispatch_tags
      @articles = site.articles.find_all_by_tags(@dispatch_path, site.articles_per_page)
      render_liquid_template_for(:tag, 'articles' => @articles, 'tags' => @dispatch_path)
    end
 
    def dispatch_comments_feed
      show_404 and return unless find_article
      @feed_title = "Comments"
      @comments = @article.comments
      @comments.reverse!
      render :action => 'feed', :content_type => 'application/xml'
    end
 
    def dispatch_changes_feed
      show_404 and return unless find_article
      @feed_title = "Changes"
      @articles = @article.versions.find(:all, :include => :updater, :order => 'version desc')
      render :action => 'feed', :content_type => 'application/xml'
    end
 
    def paged_search_url_for(page)
      page ? site.search_url(params[:q], page) : ''
    end
 
    def find_article
      cached_references << (@article = site.articles.find_by_permalink(@dispatch_path.first))
      @article
    end
 
    def show_article_with(assigns = {})
      find_article if @article.nil?
      show_404 and return unless @article || find_article
      Mephisto::Liquid::CommentForm.article = @article
      @article = @article.to_liquid(:mode => :single)
      render_liquid_template_for(:single, assigns.update('articles' => [@article], 'article' => @article))
    end
    alias dispatch_single show_article_with
end