implemented activities tab
[shapado:piglops-shapado.git] / app / controllers / application_controller.rb
1 # Filters added to this controller apply to all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3
4 class ApplicationController < ActionController::Base
5   include Rack::Recaptcha::Helpers
6   include Subdomains
7   include Sweepers
8
9   include Shapado::Controllers::Access
10   include Shapado::Controllers::Routes
11   include Shapado::Controllers::Locale
12   include Shapado::Controllers::Utils
13
14   if !AppConfig.recaptcha['activate']
15     def recaptcha_valid?
16       true
17     end
18   end
19
20   protect_from_forgery
21
22   before_filter :find_group
23   before_filter :check_group_access
24   before_filter :set_locale
25   before_filter :find_languages
26   before_filter :share_variables
27   layout :set_layout
28
29   helper_method :recaptcha_tag
30
31   rescue_from Error404, :with => :render_404
32   rescue_from Mongoid::Errors::DocumentNotFound, :with => :render_404
33
34   protected
35   def find_group
36     @current_group ||= begin
37       subdomains = request.subdomains
38       subdomains.delete("www") if request.host == "www.#{AppConfig.domain}"
39       _current_group = Group.where({:state => "active", :domain => request.host}).first
40       unless _current_group
41         if subdomain = subdomains.first
42           _current_group = Group.where(:state => "active", :subdomain => subdomain).first
43           unless _current_group.nil?
44             redirect_to domain_url(:custom => _current_group.domain)
45             return
46           end
47         end
48         flash[:warn] = t("global.group_not_found", :url => request.host)
49         redirect_to domain_url(:custom => AppConfig.domain)
50         return
51       end
52       _current_group
53     end
54     @current_group
55   end
56
57   def find_questions(extra_conditions = {}, extra_scope = { })
58     if params[:language] || request.query_string =~ /tags=/
59       params.delete(:language)
60       head :moved_permanently, :location => url_for(params)
61       return
62     end
63
64     set_page_title(t("questions.index.title"))
65     conditions = scoped_conditions(:banned => false)
66
67     if params[:sort] == "hot"
68       conditions[:activity_at] = {"$gt" => 5.days.ago}
69     end
70
71     @active_tab = "questions"
72     if params[:unanswered]
73       conditions[:answered_with_id] = nil
74       @active_tab = "unanswered"
75     elsif params[:answers]
76       @active_tab = "answers"
77     end
78     @active_subtab ||= params[:sort] || "newest"
79
80     @questions = Question.minimal.where(conditions.merge(extra_conditions)).order_by(current_order)
81
82     extra_scope.keys.each do |key|
83       @questions = @questions.send(key, extra_scope[key])
84     end
85
86     @questions = @questions.paginate({:per_page => 25, :page => params[:page] || 1})
87
88     @langs_conds = scoped_conditions[:language][:$in]
89
90     if logged_in?
91       feed_params = { :feed_token => current_user.feed_token }
92     else
93       feed_params = {  :lang => I18n.locale,
94                           :mylangs => current_languages }
95     end
96     add_feeds_url(url_for({:format => "atom"}.merge(feed_params)), t("feeds.questions"))
97     if params[:tags]
98       add_feeds_url(url_for({:format => "atom", :tags => params[:tags]}.merge(feed_params)),
99                     "#{t("feeds.tag")} #{params[:tags].inspect}")
100     end
101     @tag_cloud = Question.tag_cloud(scoped_conditions, 25)
102
103     respond_to do |format|
104       format.html
105       format.mobile
106       format.json  { render :json => @questions.to_json(:except => %w[_keywords watchers slugs]) }
107       format.atom
108     end
109   end
110
111   def find_activities(conds = {})
112     #add_feeds_url(url_for({:format => "atom"}.merge(feed_params)), t("feeds.questions"))
113
114     @activities = current_group.activities.where(conds).order(:created_at.desc).
115                                 paginate(:page => params[:page],
116                                          :per_page => params[:per_page]||25)
117
118     respond_to do |format|
119       format.html
120       format.json { render :json => @activities}
121     end
122   end
123
124   def current_group
125     @current_group
126   end
127   helper_method :current_group
128
129   def scoped_conditions(conditions = {})
130     unless current_tags.empty?
131       conditions.deep_merge!({:tags => {:$all => current_tags}})
132     end
133     conditions.deep_merge!({:group_id => current_group.id})
134     conditions.deep_merge!(language_conditions)
135     conditions
136   end
137   helper_method :scoped_conditions
138
139   def set_layout
140     if devise_controller? || (action_name == "new" && controller_name == "users")
141       'sessions'
142     elsif params["format"] == "mobile"
143       'mobile'
144     else
145       'application'
146     end
147   end
148
149   def render_404
150     Rails.logger.info "ROUTE NOT FOUND (404): #{request.url}"
151
152     respond_to do |format|
153       format.html { render "public_errors/not_found", :status => '404 Not Found' }
154       format.json { render :json => {:success => false, :message => "Not Found"}, :status => '404 Not Found' }
155     end
156   end
157
158   # override from devise
159   def after_sign_out_path_for(resource)
160     params[:format] == "mobile" ? "/mobile" : root_path
161   end
162
163   def after_sign_in_path_for(resource_or_scope)
164     self.current_user = resource_or_scope
165     super(resource_or_scope)
166   end
167
168   def share_variables
169     Thread.current[:current_group] = current_group
170     Thread.current[:current_user] = current_user
171     Thread.current[:current_ip] = request.remote_ip
172   end
173 end