/app/controllers/watches_controller.rb

https://github.com/ekcell/openmind · Ruby · 335 lines · 282 code · 40 blank · 13 comment · 9 complexity · 4c48dae22059f94c2cbbc15b549ba9de MD5 · raw file

  1. class WatchesController < ApplicationController
  2. helper :idea_action, :forums, :products
  3. before_filter :login_required, :except => [ :create_product_watch_from_check_for_update ]
  4. cache_sweeper :forums_sweeper, :only => [ :create_forum_watch,
  5. :destroy_forum_watch
  6. ]
  7. verify :method => :post, :only => [:create, :create_from_show, :create_topic_watch ],
  8. :redirect_to =>{ :controller => 'ideas', :action => :index }
  9. verify :method => :put, :only => [ :update ],
  10. :redirect_to => { :controller => 'ideas', :action => :list }
  11. verify :method => :delete, :only => [ :destroy, :destroy_topic_watch ],
  12. :redirect_to => { :controller => 'ideas', :action => :list }
  13. # collection routes apparently can't take additonal parameters other than
  14. # id... so, a bit of a kludge, if we can't pass a from parameter to indicate
  15. # whether the action was originated from the list page or the show page, I
  16. # added another action to indicate the difference
  17. def create_from_show
  18. create "show"
  19. end
  20. def create_product_watch_from_check_for_update
  21. if logged_in?
  22. create_product_watch
  23. else
  24. flash[:notice] = 'You must be logged in to OpenMind in order to put a watch on a product'
  25. session[:return_to] = params[:from]
  26. redirect_to :controller => '/account', :action => 'login'
  27. end
  28. end
  29. def create_product_watches
  30. release_ids = params[:releases]
  31. if release_ids
  32. added = false
  33. release_ids.split(",").collect{|r| r.split("|").first}.each do |release_id|
  34. release = Release.find_by_id(release_id)
  35. if release
  36. unless release.product.watchers.include? current_user
  37. release.product.watchers << current_user
  38. added = true
  39. end
  40. else
  41. flash[:error] = "Unknown release id #{release_id}."
  42. end
  43. end
  44. if flash[:error].nil?
  45. if added
  46. flash[:notice] = "All of your products are now being watched."
  47. else
  48. flash[:error] = "No watches added...all of your products are already being watched."
  49. end
  50. end
  51. end
  52. redirect_to check_for_updates_releases_path(:releases => params[:releases], :serial_number => params[:serial_number])
  53. end
  54. def create_product_watch
  55. begin
  56. @product = Product.find(params[:id])
  57. @product.watchers << current_user unless @product.watchers.include? current_user
  58. rescue ActiveRecord::RecordNotFound
  59. logger.error("Attempt to add watch to invalid product #{params[:id]}")
  60. flash[:error] = "Attempted to add watch to invalid product"
  61. # #list
  62. respond_to do |format|
  63. format.html { redirect_to product_path(@product) }
  64. format.js { do_product_action }
  65. end
  66. return false
  67. else
  68. flash[:notice] = "Product '#{@product.name}' is being watched."
  69. respond_to do |format|
  70. format.html { redirect_to product_path(@product) }
  71. format.js { do_product_action }
  72. end
  73. end
  74. end
  75. def create_forum_watch
  76. begin
  77. @forum = Forum.find(params[:id])
  78. unless @forum.can_see? current_user or prodmgr?
  79. flash[:error] = ForumsController.flash_for_forum_access_denied(current_user)
  80. redirect_to redirect_path_on_access_denied(current_user)
  81. end
  82. @forum.watchers << current_user unless @forum.watchers.include? current_user
  83. @forum.watch_all_topics current_user
  84. rescue ActiveRecord::RecordNotFound
  85. logger.error("Attempt to add watch to invalid forum #{params[:id]}")
  86. flash[:error] = "Attempted to add watch to invalid forum"
  87. # #list
  88. respond_to do |format|
  89. format.html {render forum_path(@forum) }
  90. format.js { do_forum_action }
  91. end
  92. return false
  93. else
  94. flash[:notice] = "Forum '#{@forum.name}' is being watched."
  95. respond_to do |format|
  96. format.html {redirect_to forum_path(@forum) }
  97. format.js { do_forum_action }
  98. end
  99. end
  100. end
  101. def create_topic_watch
  102. begin
  103. @topic = Topic.find(params[:id])
  104. unless @topic.forum.can_see? current_user or prodmgr?
  105. flash[:error] = ForumsController.flash_for_forum_access_denied(current_user)
  106. redirect_to redirect_path_on_access_denied(current_user)
  107. end
  108. @topic.watchers << current_user unless @topic.watchers.include? current_user
  109. rescue ActiveRecord::RecordNotFound
  110. logger.error("Attempt to add watch to invalid topic #{params[:id]}")
  111. flash[:notice] = "Attempted to add watch to invalid topic"
  112. # #list
  113. respond_to do |format|
  114. format.html {render topic_path(@topic) }
  115. format.js { do_topic_action }
  116. end
  117. return false
  118. else
  119. flash[:notice] = "Topic '#{@topic.title}' is being watched."
  120. respond_to do |format|
  121. format.html {redirect_to topic_path(@topic) }
  122. format.js { do_topic_action }
  123. end
  124. end
  125. end
  126. # Create idea watch
  127. def create from="list"
  128. begin
  129. @idea = Idea.find(params[:id])
  130. @idea.watchers << current_user unless @idea.watchers.include? current_user
  131. rescue ActiveRecord::RecordNotFound
  132. logger.error("Attempt to add watch to invalid idea #{params[:id]}")
  133. flash[:notice] = "Attempted to add watch to invalid idea"
  134. # #list
  135. respond_to do |format|
  136. format.html {render :controller => 'ideas', :action => 'list' }
  137. format.js { do_idea_action from }
  138. end
  139. return false
  140. else
  141. flash[:notice] = "Idea number #{@idea.id} is being watched."
  142. respond_to do |format|
  143. format.html {redirect_to :controller => 'ideas', :action => 'show', :id => @idea }
  144. format.js { do_idea_action from }
  145. end
  146. end
  147. end
  148. def destroy
  149. begin
  150. @idea = Idea.find(params[:id])
  151. @idea.watchers.delete(current_user)
  152. rescue ActiveRecord::RecordNotFound
  153. logger.error("Attempt to remove watch from invalid idea #{params[:id]}")
  154. flash[:error] = "Attempted to remove watch from invalid idea"
  155. # #list
  156. respond_to do |format|
  157. format.html {render :controller => 'ideas', :action => 'list' }
  158. format.js { do_idea_action }
  159. end
  160. return false
  161. else
  162. flash[:notice] = %(Watch removed from Idea number #{@idea.id}.)
  163. respond_to do |format|
  164. format.html {redirect_to :controller => 'ideas', :action => 'show', :id => @idea }
  165. format.js { do_idea_action params[:from] }
  166. end
  167. end
  168. end
  169. def destroy_forum_watch
  170. begin
  171. @forum = Forum.find(params[:id])
  172. unless @forum.can_see? current_user or prodmgr?
  173. flash[:error] = ForumsController.flash_for_forum_access_denied(current_user)
  174. redirect_to redirect_path_on_access_denied(current_user)
  175. end
  176. @forum.watchers.delete(current_user)
  177. rescue ActiveRecord::RecordNotFound
  178. logger.error("Attempt to remove watch from invalid forum #{params[:id]}")
  179. flash[:error] = "Attempted to remove watch from invalid forum"
  180. # #list
  181. respond_to do |format|
  182. format.html {redirect_to forum_path(@forum) }
  183. format.js { do_forum_action }
  184. end
  185. return false
  186. else
  187. flash[:notice] = %(Watch removed from forum '#{@forum.name}')
  188. respond_to do |format|
  189. format.html {redirect_to forum_path(@forum) }
  190. format.js { do_forum_action }
  191. end
  192. end
  193. end
  194. def destroy_product_watch
  195. begin
  196. @product = Product.find(params[:id])
  197. @product.watchers.delete(current_user)
  198. rescue ActiveRecord::RecordNotFound
  199. logger.error("Attempt to remove watch from invalid product #{params[:id]}")
  200. flash[:error] = "Attempted to remove watch from invalid product"
  201. # list
  202. respond_to do |format|
  203. format.html { redirect_to product_path(@product) }
  204. format.js { do_product_action }
  205. end
  206. return false
  207. else
  208. flash[:notice] = %(Watch removed from product '#{@product.name}')
  209. respond_to do |format|
  210. format.html { redirect_to product_path(@product) }
  211. format.js { do_product_action }
  212. end
  213. end
  214. end
  215. def destroy_topic_watch
  216. begin
  217. @topic = Topic.find(params[:id])
  218. unless @topic.forum.can_see? current_user or prodmgr?
  219. flash[:error] = ForumsController.flash_for_forum_access_denied(current_user)
  220. redirect_to redirect_path_on_access_denied(current_user)
  221. end
  222. @topic.watchers.delete(current_user)
  223. rescue ActiveRecord::RecordNotFound
  224. logger.error("Attempt to remove watch from invalid topic #{params[:id]}")
  225. flash[:notice] = "Attempted to remove watch from invalid topic"
  226. # #list
  227. respond_to do |format|
  228. format.html {render forums_path }
  229. format.js { do_topic_action }
  230. end
  231. return false
  232. else
  233. flash[:notice] = %(Watch removed from topic '#{@topic.title}')
  234. respond_to do |format|
  235. format.html {redirect_to topic_path(@topic) }
  236. format.js { do_topic_action }
  237. end
  238. end
  239. end
  240. private
  241. def redirect_path_on_access_denied user
  242. return forums_path unless user == :false
  243. return url_for(:controller => 'account', :action => 'login', :only_path => true) if user == :false
  244. end
  245. def do_idea_action from="list"
  246. render :update do |page|
  247. page.replace_html :flash_notice, flash_notice_string(flash[:notice])
  248. page.replace_html :flash_error, flash_error_string(flash[:error])
  249. flash[:notice].nil? ? (page.hide :flash_notice) : (page.show :flash_notice)
  250. flash[:error].nil? ? (page.hide :flash_error) : (page.show :flash_error)
  251. flash.discard
  252. page.replace "action_buttons#{@idea.id.to_s}",
  253. :partial => "ideas/list_actions",
  254. :object => @idea,
  255. :locals => { :from => from}
  256. end
  257. end
  258. def do_topic_action
  259. render :update do |page|
  260. page.replace_html :flash_notice, flash_notice_string(flash[:notice])
  261. page.replace_html :flash_error, flash_error_string(flash[:error])
  262. page.replace_html "topic_watchers#{@topic.id.to_s}", @topic.watchers.size
  263. flash[:notice].nil? ? (page.hide :flash_notice) : (page.show :flash_notice)
  264. flash[:error].nil? ? (page.hide :flash_error) : (page.show :flash_error)
  265. flash.discard
  266. page.replace "action_buttons#{@topic.id.to_s}",
  267. :partial => "forums/topic_action",
  268. :object => @topic
  269. end
  270. end
  271. def do_forum_action
  272. render :update do |page|
  273. page.replace_html :flash_notice, flash_notice_string(flash[:notice])
  274. page.replace_html :flash_error, flash_error_string(flash[:error])
  275. flash[:notice].nil? ? (page.hide :flash_notice) : (page.show :flash_notice)
  276. flash[:error].nil? ? (page.hide :flash_error) : (page.show :flash_error)
  277. flash.discard
  278. page.replace "action_buttons#{@forum.id.to_s}",
  279. :partial => "forums/forum_action",
  280. :object => @forum
  281. end
  282. end
  283. def do_product_action
  284. render :update do |page|
  285. page.replace_html :flash_notice, flash_notice_string(flash[:notice])
  286. page.replace_html :flash_error, flash_error_string(flash[:error])
  287. flash[:notice].nil? ? (page.hide :flash_notice) : (page.show :flash_notice)
  288. flash[:error].nil? ? (page.hide :flash_error) : (page.show :flash_error)
  289. flash.discard
  290. page.replace "action_buttons_#{@product.id.to_s}",
  291. :partial => "products/product_action",
  292. :object => @product
  293. end
  294. end
  295. end