/
pages_controller.rb
executable file
·64 lines (52 loc) · 1.98 KB
/
pages_controller.rb
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
class PagesController < ApplicationController
# GET /pages
protect_from_forgery :only => [:create, :update, :destroy]
layout 'pages'#, :except => [:auto_complete_for_search_query]
use_google_charts
caches_page :show
caches_page :csv
def auto_complete_for_search_query
# look for autosuggest results in memcached
unless read_fragment({:query => params["search"]["query"]})
@pages = Page.title_like params["search"]["query"]
end
render :partial => "search_results"
end
def index
if params[:search]
@pages = Page.title_like(params["search"]["query"]).paginate(:page => params[:page], :order => 'monthly_trend DESC', :per_page => APP_CONFIG['articles_per_page'])
else
@pages = Page.paginate(:page => params[:page], :conditions => ["pages.id NOT IN (?) and page_id NOT IN (select page_id from featured_pages)", APP_CONFIG['blacklist']], :order => 'monthly_trend DESC', :per_page => APP_CONFIG['articles_per_page'])
end
# random rising, rotates
@page = DailyTrend.find(:all, :limit => 20 , :order => 'trend DESC', :conditions => ["page_id NOT IN (?)", APP_CONFIG['blacklist']] ).rand.page
unless params[:page]
params[:page]='1'
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @pages }
format.atom { render :layout => false}
end
end
# GET /pages/1
# GET /pages/1.xml
def show
@page = Page.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @page }
end
end
#### Custom REST actions #######
# GET /pages/1/csv
def csv
@page = Page.find(params[:id])
csv_array = ["Date,Pageviews"]
@page.date_pageview_array.each do |pair|
csv_array << "#{pair[0]},#{pair[1]}"
end
send_data csv_array.join("\n"), :type => 'text/csv; charset=utf-8', :filename=>"#{@page.url}.csv",
:disposition => 'attachment'
end
end