Commit 5884c1513dc2390dcf80242d6d96b9cd4e8f2ce6
1 parent
1db5d68d
Exists in
master
and in
1 other branch
DRYing AppsController with InheritedResources.
Showing
4 changed files
with
24 additions
and
48 deletions
Show diff stats
Gemfile
Gemfile.lock
... | ... | @@ -61,7 +61,11 @@ GEM |
61 | 61 | haml (3.0.25) |
62 | 62 | happymapper (0.3.2) |
63 | 63 | libxml-ruby (~> 1.1.3) |
64 | + has_scope (0.5.1) | |
64 | 65 | i18n (0.5.0) |
66 | + inherited_resources (1.2.2) | |
67 | + has_scope (~> 0.5.0) | |
68 | + responders (~> 0.6.0) | |
65 | 69 | libxml-ruby (1.1.4) |
66 | 70 | lighthouse-api (2.0) |
67 | 71 | activeresource (>= 3.0.0) |
... | ... | @@ -110,6 +114,7 @@ GEM |
110 | 114 | rake (>= 0.8.7) |
111 | 115 | thor (~> 0.14.4) |
112 | 116 | rake (0.8.7) |
117 | + responders (0.6.4) | |
113 | 118 | rest-client (1.5.1) |
114 | 119 | mime-types (>= 1.16) |
115 | 120 | rspec (2.5.0) |
... | ... | @@ -157,6 +162,7 @@ DEPENDENCIES |
157 | 162 | email_spec |
158 | 163 | factory_girl_rails |
159 | 164 | haml |
165 | + inherited_resources | |
160 | 166 | lighthouse-api |
161 | 167 | mongoid (= 2.0.2) |
162 | 168 | mongoid_rails_migrations | ... | ... |
app/controllers/apps_controller.rb
1 | -class AppsController < ApplicationController | |
1 | +class AppsController < InheritedResources::Base | |
2 | 2 | |
3 | 3 | before_filter :require_admin!, :except => [:index, :show] |
4 | - before_filter :find_app, :except => [:index, :new, :create] | |
5 | 4 | before_filter :parse_email_at_notices_or_set_default, :only => [:create, :update] |
6 | 5 | |
7 | - def index | |
8 | - @apps = current_user.admin? ? App.all : current_user.apps.all | |
9 | - end | |
10 | - | |
11 | 6 | def show |
12 | 7 | where_clause = {} |
13 | 8 | respond_to do |format| |
14 | 9 | format.html do |
15 | 10 | where_clause[:environment] = params[:environment] if(params[:environment].present?) |
16 | 11 | if(params[:all_errs]) |
17 | - @errs = @app.errs.where(where_clause).ordered.paginate(:page => params[:page], :per_page => current_user.per_page) | |
12 | + @errs = resource.errs.where(where_clause).ordered.paginate(:page => params[:page], :per_page => current_user.per_page) | |
18 | 13 | @all_errs = true |
19 | 14 | else |
20 | - @errs = @app.errs.unresolved.where(where_clause).ordered.paginate(:page => params[:page], :per_page => current_user.per_page) | |
15 | + @errs = resource.errs.unresolved.where(where_clause).ordered.paginate(:page => params[:page], :per_page => current_user.per_page) | |
21 | 16 | @all_errs = false |
22 | 17 | end |
23 | 18 | @deploys = @app.deploys.order_by(:created_at.desc).limit(5) |
24 | 19 | end |
25 | 20 | format.atom do |
26 | - @errs = @app.errs.unresolved.ordered | |
21 | + @errs = resource.errs.unresolved.ordered | |
27 | 22 | end |
28 | 23 | end |
29 | 24 | end |
30 | 25 | |
31 | 26 | def new |
32 | - @app = App.new | |
33 | - @app.watchers.build | |
27 | + build_resource.watchers.build | |
34 | 28 | @app.issue_tracker = IssueTracker.new |
29 | + new! | |
35 | 30 | end |
36 | 31 | |
37 | 32 | def edit |
38 | - @app.watchers.build if @app.watchers.none? | |
39 | - @app.issue_tracker = IssueTracker.new if @app.issue_tracker.nil? | |
33 | + resource.watchers.build if resource.watchers.none? | |
34 | + resource.issue_tracker = IssueTracker.new if resource.issue_tracker.nil? | |
35 | + edit! | |
40 | 36 | end |
41 | 37 | |
42 | 38 | def create |
43 | - @app = App.new(params[:app]) | |
44 | - | |
45 | - if @app.save | |
46 | - flash[:success] = 'Great success! Configure your app with the API key below' | |
47 | - redirect_to app_path(@app) | |
48 | - else | |
49 | - render :new | |
50 | - end | |
39 | + create! :success => 'Great success! Configure your app with the API key below' | |
51 | 40 | end |
52 | 41 | |
53 | 42 | def update |
54 | - if @app.update_attributes(params[:app]) | |
55 | - flash[:success] = "Good news everyone! '#{@app.name}' was successfully updated." | |
56 | - redirect_to app_path(@app) | |
57 | - else | |
58 | - render :edit | |
59 | - end | |
43 | + update! :success => "Good news everyone! '#{resource.name}' was successfully updated." | |
60 | 44 | end |
61 | 45 | |
62 | 46 | def destroy |
63 | - @app.destroy | |
64 | - flash[:success] = "'#{@app.name}' was successfully destroyed." | |
65 | - redirect_to apps_path | |
47 | + destroy! :success => "'#{resource.name}' was successfully destroyed." | |
66 | 48 | end |
67 | 49 | |
68 | 50 | protected |
69 | - | |
70 | - def find_app | |
71 | - @app = App.find(params[:id]) | |
72 | - | |
73 | - # Mongoid Bug: could not chain: current_user.apps.find_by_id! | |
74 | - # apparently finding by 'watchers.email' and 'id' is broken | |
75 | - raise(Mongoid::Errors::DocumentNotFound.new(App,@app.id)) unless current_user.admin? || current_user.watching?(@app) | |
51 | + def begin_of_association_chain | |
52 | + current_user unless current_user.admin? | |
76 | 53 | end |
77 | 54 | |
78 | 55 | # email_at_notices is edited as a string, and stored as an array. | ... | ... |
spec/controllers/apps_controller_spec.rb
... | ... | @@ -201,15 +201,7 @@ describe AppsController do |
201 | 201 | |
202 | 202 | it "should display a message" do |
203 | 203 | post :create, :app => {} |
204 | - request.flash[:success].should match(/success/) | |
205 | - end | |
206 | - end | |
207 | - | |
208 | - context "when the create is unsuccessful" do | |
209 | - it "should render the new page" do | |
210 | - @app.should_receive(:save).and_return(false) | |
211 | - post :create, :app => {} | |
212 | - response.should render_template(:new) | |
204 | + request.flash[:notice].should match(/success/) | |
213 | 205 | end |
214 | 206 | end |
215 | 207 | end |
... | ... | @@ -227,7 +219,7 @@ describe AppsController do |
227 | 219 | |
228 | 220 | it "should display a message" do |
229 | 221 | put :update, :id => @app.id, :app => {} |
230 | - request.flash[:success].should match(/success/) | |
222 | + request.flash[:notice].should match(/success/) | |
231 | 223 | end |
232 | 224 | end |
233 | 225 | |
... | ... | @@ -432,7 +424,7 @@ describe AppsController do |
432 | 424 | |
433 | 425 | it "should display a message" do |
434 | 426 | delete :destroy, :id => @app.id |
435 | - request.flash[:success].should match(/success/) | |
427 | + request.flash[:notice].should match(/success/) | |
436 | 428 | end |
437 | 429 | |
438 | 430 | it "should redirect to the apps page" do | ... | ... |