Commit 5ed8ce12ff6025cd1560ad3f28661198ab03a0fd

Authored by Nathan B
2 parents abf96589 9277301c
Exists in master and in 1 other branch production

Merge pull request #69 from fxposter/refactoring

Small enhancements, more to go
app/controllers/application_controller.rb
... ... @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base
6 6 protected
7 7  
8 8 def require_admin!
9   - redirect_to(root_path) and return(false) unless user_signed_in? && current_user.admin?
  9 + redirect_to root_path unless user_signed_in? && current_user.admin?
10 10 end
11 11  
12 12 end
... ...
app/controllers/apps_controller.rb
... ... @@ -5,17 +5,14 @@ class AppsController < InheritedResources::Base
5 5 respond_to :html
6 6  
7 7 def show
8   - where_clause = {}
9 8 respond_to do |format|
10 9 format.html do
11   - where_clause[:environment] = params[:environment] if(params[:environment].present?)
12   - if(params[:all_errs])
13   - @errs = resource.errs.where(where_clause).ordered.paginate(:page => params[:page], :per_page => current_user.per_page)
14   - @all_errs = true
15   - else
16   - @errs = resource.errs.unresolved.where(where_clause).ordered.paginate(:page => params[:page], :per_page => current_user.per_page)
17   - @all_errs = false
18   - end
  10 + @all_errs = !!params[:all_errs]
  11 +
  12 + @errs = resource.errs
  13 + @errs = @errs.unresolved unless @all_errs
  14 + @errs = @errs.in_env(params[:environment]).ordered.paginate(:page => params[:page], :per_page => current_user.per_page)
  15 +
19 16 @deploys = @app.deploys.order_by(:created_at.desc).limit(5)
20 17 end
21 18 format.atom do
... ... @@ -46,7 +43,6 @@ class AppsController < InheritedResources::Base
46 43 edit!
47 44 end
48 45  
49   -
50 46 protected
51 47 def initialize_subclassed_issue_tracker
52 48 if params[:app][:issue_tracker_attributes] && tracker_type = params[:app][:issue_tracker_attributes][:type]
... ...
app/controllers/deploys_controller.rb
... ... @@ -7,34 +7,40 @@ class DeploysController < ApplicationController
7 7  
8 8 def create
9 9 @app = App.find_by_api_key!(params[:api_key])
10   - if params[:deploy]
11   - deploy = {
12   - :username => params[:deploy][:local_username],
13   - :environment => params[:deploy][:rails_env],
14   - :repository => params[:deploy][:scm_repository],
15   - :revision => params[:deploy][:scm_revision],
16   - :message => params[:deploy][:message]
17   - }
18   - end
19   -
20   - # handle Heroku's HTTP post deployhook format
21   - deploy ||= {
22   - :username => params[:user],
23   - :environment => params[:rack_env].try(:downcase) || params[:app],
24   - :repository => "git@heroku.com:#{params[:app]}.git",
25   - :revision => params[:head],
26   - }
27   -
28   - @deploy = @app.deploys.create!(deploy)
  10 + @deploy = @app.deploys.create!(default_deploy || heroku_deploy)
29 11 render :xml => @deploy
30 12 end
31 13  
32 14 def index
33 15 # See AppsController#find_app for the reasoning behind this code.
34 16 app = App.find(params[:app_id])
35   - raise(Mongoid::Errors::DocumentNotFound.new(App,app.id)) unless current_user.admin? || current_user.watching?(app)
  17 + raise Mongoid::Errors::DocumentNotFound.new(App, app.id) unless current_user.admin? || current_user.watching?(app)
36 18  
37   - @deploys = app.deploys.order_by(:created_at.desc).paginate(:page => params[:page], :per_page => 10)
  19 + @deploys = app.deploys.order_by(:created_at.desc).paginate(:page => params[:page], :per_page => 10)
38 20 end
39   -
  21 +
  22 + private
  23 +
  24 + def default_deploy
  25 + if params[:deploy]
  26 + {
  27 + :username => params[:deploy][:local_username],
  28 + :environment => params[:deploy][:rails_env],
  29 + :repository => params[:deploy][:scm_repository],
  30 + :revision => params[:deploy][:scm_revision],
  31 + :message => params[:deploy][:message]
  32 + }
  33 + end
  34 + end
  35 +
  36 + # handle Heroku's HTTP post deployhook format
  37 + def heroku_deploy
  38 + {
  39 + :username => params[:user],
  40 + :environment => params[:rack_env].try(:downcase) || params[:app],
  41 + :repository => "git@heroku.com:#{params[:app]}.git",
  42 + :revision => params[:head],
  43 + }
  44 + end
  45 +
40 46 end
... ...
app/controllers/errs_controller.rb
... ... @@ -5,15 +5,12 @@ class ErrsController < ApplicationController
5 5  
6 6 def index
7 7 app_scope = current_user.admin? ? App.all : current_user.apps
8   - where_clause = {}
9   - where_clause[:environment] = params[:environment] if(params[:environment].present?)
  8 + @errs = Err.for_apps(app_scope).in_env(params[:environment]).unresolved.ordered
10 9 respond_to do |format|
11 10 format.html do
12   - @errs = Err.for_apps(app_scope).where(where_clause).unresolved.ordered.paginate(:page => params[:page], :per_page => current_user.per_page)
13   - end
14   - format.atom do
15   - @errs = Err.for_apps(app_scope).where(where_clause).unresolved.ordered
  11 + @errs = @errs.paginate(:page => params[:page], :per_page => current_user.per_page)
16 12 end
  13 + format.atom
17 14 end
18 15 end
19 16  
... ...
app/models/err.rb
... ... @@ -25,8 +25,11 @@ class Err
25 25 scope :resolved, where(:resolved => true)
26 26 scope :unresolved, where(:resolved => false)
27 27 scope :ordered, order_by(:last_notice_at.desc)
28   - scope :in_env, lambda {|env| where(:environment => env)}
29 28 scope :for_apps, lambda {|apps| where(:app_id.in => apps.all.map(&:id))}
  29 +
  30 + def self.in_env(env)
  31 + env.present? ? where(:environment => env) : scoped
  32 + end
30 33  
31 34 def self.for(attrs)
32 35 app = attrs.delete(:app)
... ...
app/views/apps/show.html.haml
... ... @@ -13,14 +13,14 @@
13 13 - if current_user.admin?
14 14 = link_to 'edit', edit_app_path(@app), :class => 'button'
15 15 = link_to 'destroy', app_path(@app), :method => :delete, :confirm => 'Seriously?', :class => 'button'
16   - - if @all_errs == true
  16 + - if @all_errs
17 17 = link_to 'unresolved errs', app_path(@app), :class => 'button'
18 18 - else
19   - = link_to 'all errs', app_path(@app, {:all_errs => true}), :class => 'button'
  19 + = link_to 'all errs', app_path(@app, :all_errs => true), :class => 'button'
20 20  
21   -%h3{:id => 'watchers_toggle'}
  21 +%h3#watchers_toggle
22 22 Watchers
23   - %span{:class => 'click_span'} (show/hide)
  23 + %span.click_span (show/hide)
24 24 #watchers_div
25 25 - if @app.notify_all_users
26 26 %table.watchers
... ... @@ -42,9 +42,9 @@
42 42 %em Sadly, no one is watching this app
43 43  
44 44 - if @app.github_url?
45   - %h3{:id => 'repository_toggle'}
  45 + %h3#repository_toggle
46 46 Repository
47   - %span{:class => 'click_span'} (show/hide)
  47 + %span.click_span (show/hide)
48 48 #repository_div
49 49 %table.repository
50 50 %thead
... ... @@ -54,9 +54,9 @@
54 54 %tr
55 55 %td= link_to(@app.github_url, @app.github_url, :target => '_blank')
56 56  
57   -%h3{:id => 'deploys_toggle'}
  57 +%h3#deploys_toggle
58 58 Latest Deploys
59   - %span{:class => 'click_span'} (show/hide)
  59 + %span.click_span (show/hide)
60 60 #deploys_div
61 61 - if @deploys.any?
62 62 %table.deploys
... ...