Skip to content

Commit

Permalink
moved Notices to a separate collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Hosiawa authored and hosiawak committed Apr 23, 2011
1 parent bd17fa7 commit 40453cc
Show file tree
Hide file tree
Showing 27 changed files with 342 additions and 167 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ config/deploy.rb
config/mongoid.yml
.rvmrc
*~
*.rbc
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -2,12 +2,13 @@ source 'http://rubygems.org'

gem 'rails', '3.0.5'
gem 'nokogiri'
gem 'mongoid', '~> 2.0.0.rc.7'
gem 'mongoid', '2.0.0.rc.8'
gem 'haml'
gem 'will_paginate'
gem 'devise', '~> 1.1.8'
gem 'lighthouse-api'
gem 'redmine_client', :git => "git://github.com/oruen/redmine_client.git"
gem 'mongoid_rails_migrations'

platform :ruby do
gem 'bson_ext', '~> 1.2'
Expand Down
30 changes: 18 additions & 12 deletions Gemfile.lock
Expand Up @@ -35,15 +35,15 @@ GEM
activemodel (= 3.0.5)
activesupport (= 3.0.5)
activesupport (3.0.5)
addressable (2.2.4)
addressable (2.2.5)
arel (2.0.9)
bcrypt-ruby (2.1.4)
bson (1.2.4)
bson_ext (1.2.4)
bson (1.3.0)
bson_ext (1.3.0)
builder (2.1.2)
crack (0.1.8)
database_cleaner (0.6.5)
devise (1.1.8)
database_cleaner (0.6.7)
devise (1.1.9)
bcrypt-ruby (~> 2.1.2)
warden (~> 1.0.2)
diff-lcs (1.1.2)
Expand All @@ -58,23 +58,28 @@ GEM
lighthouse-api (2.0)
activeresource (>= 3.0.0)
activesupport (>= 3.0.0)
mail (2.2.15)
mail (2.2.17)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mongo (1.2.4)
bson (>= 1.2.4)
mongoid (2.0.0.rc.7)
mongo (1.3.0)
bson (>= 1.3.0)
mongoid (2.0.0.rc.8)
activemodel (~> 3.0)
mongo (~> 1.2)
tzinfo (~> 0.3.22)
will_paginate (~> 3.0.pre)
mongoid_rails_migrations (0.0.10)
activesupport (~> 3.0.0)
bundler (>= 0.9.19)
rails (~> 3.0.0)
railties (~> 3.0.0)
nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.6.13)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
Expand Down Expand Up @@ -108,7 +113,7 @@ GEM
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.25)
tzinfo (0.3.26)
warden (1.0.3)
rack (>= 1.0.0)
webmock (1.6.2)
Expand All @@ -126,7 +131,8 @@ DEPENDENCIES
factory_girl_rails
haml
lighthouse-api
mongoid (~> 2.0.0.rc.7)
mongoid (= 2.0.0.rc.8)
mongoid_rails_migrations
nokogiri
rails (= 3.0.5)
redmine_client!
Expand Down
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -89,17 +89,24 @@ for you. Checkout [Hoptoad](http://hoptoadapp.com) from the guys over at

4. Enjoy!

Upgrading
---------
*Note*: If upgrading from a version of Errbit that used Notices embedded in Errs please run:

1. git pull origin master ( assuming origin is the github.com/jdpace/errbit repo )
2. rake db:migrate

Lighthouseapp integration
-------------------------

* Account is the name of your subdomain, i.e. **litcafe** for project at http://litcafe.lighthouseapp.com/projects/73466-face/overview
* Account is the name of your subdomain, i.e. **litcafe** for project at http://litcafe.lighthouseapp.com/projects/73466-face/overview
* Errbit uses token-based authentication. Get your API Token or visit [http://help.lighthouseapp.com/kb/api/how-do-i-get-an-api-token](http://help.lighthouseapp.com/kb/api/how-do-i-get-an-api-token) to learn how to get it.
* Project id is number identifier of your project, i.e. **73466** for project at http://litcafe.lighthouseapp.com/projects/73466-face/overview

Redmine integration
-------------------------

* Account is the host of your redmine installation, i.e. **http://redmine.org**
* Account is the host of your redmine installation, i.e. **http://redmine.org**
* Errbit uses token-based authentication. Get your API Key or visit [http://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication](http://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication) to learn how to get it.
* Project id is an identifier of your project, i.e. **chilliproject** for project at http://www.redmine.org/projects/chilliproject

Expand Down
26 changes: 13 additions & 13 deletions app/controllers/apps_controller.rb
@@ -1,12 +1,12 @@
class AppsController < ApplicationController

before_filter :require_admin!, :except => [:index, :show]
before_filter :find_app, :except => [:index, :new, :create]

def index
@apps = current_user.admin? ? App.all : current_user.apps.all
end

def show
respond_to do |format|
format.html do
Expand All @@ -18,49 +18,49 @@ def show
end
end
end

def new
@app = App.new
@app.watchers.build
@app.issue_tracker = IssueTracker.new
end

def edit
@app.watchers.build if @app.watchers.none?
@app.issue_tracker = IssueTracker.new if @app.issue_tracker.nil?
end

def create
@app = App.new(params[:app])

if @app.save
flash[:success] = 'Great success! Configure your app with the API key below'
redirect_to app_path(@app)
else
render :new
end
end
def update

def update
if @app.update_attributes(params[:app])
flash[:success] = "Good news everyone! '#{@app.name}' was successfully updated."
redirect_to app_path(@app)
else
render :edit
end
end

def destroy
@app.destroy
flash[:success] = "'#{@app.name}' was successfully destroyed."
redirect_to apps_path
end

protected

def find_app
@app = App.find(params[:id])

# Mongoid Bug: could not chain: current_user.apps.find_by_id!
# apparently finding by 'watchers.email' and 'id' is broken
raise(Mongoid::Errors::DocumentNotFound.new(App,@app.id)) unless current_user.admin? || current_user.watching?(@app)
Expand Down
26 changes: 13 additions & 13 deletions app/controllers/errs_controller.rb
@@ -1,8 +1,8 @@
class ErrsController < ApplicationController

before_filter :find_app, :except => [:index, :all]
before_filter :find_err, :except => [:index, :all]

def index
app_scope = current_user.admin? ? App.all : current_user.apps
respond_to do |format|
Expand All @@ -14,14 +14,14 @@ def index
end
end
end

def all
app_scope = current_user.admin? ? App.all : current_user.apps
@errs = Err.for_apps(app_scope).ordered.paginate(:page => params[:page], :per_page => current_user.per_page)
end

def show
page = (params[:notice] || @err.notices.count)
page = (params[:notice] || @err.notices_count)
page = 1 if page.to_i.zero?
@notices = @err.notices.ordered.paginate(:page => page, :per_page => 1)
@notice = @notices.first
Expand All @@ -46,25 +46,25 @@ def clear_issue
@err.update_attribute :issue_link, nil
redirect_to app_err_path(@app, @err)
end

def resolve
# Deal with bug in mogoid where find is returning an Enumberable obj
# Deal with bug in mongoid where find is returning an Enumberable obj
@err = @err.first if @err.respond_to?(:first)

@err.resolve!

flash[:success] = 'Great news everyone! The err has been resolved.'

redirect_to :back
rescue ActionController::RedirectBackError
redirect_to app_path(@app)
end

protected

def find_app
@app = App.find(params[:app_id])

# Mongoid Bug: could not chain: current_user.apps.find_by_id!
# apparently finding by 'watchers.email' and 'id' is broken
raise(Mongoid::Errors::DocumentNotFound.new(App,@app.id)) unless current_user.admin? || current_user.watching?(@app)
Expand All @@ -79,5 +79,5 @@ def set_tracker_params
IssueTracker.default_url_options[:port] = request.port
IssueTracker.default_url_options[:protocol] = request.scheme
end

end
18 changes: 9 additions & 9 deletions app/models/app.rb
@@ -1,7 +1,7 @@
class App
include Mongoid::Document
include Mongoid::Timestamps

field :name, :type => String
field :api_key
field :resolve_errs_on_deploy, :type => Boolean, :default => false
Expand All @@ -21,29 +21,29 @@ class App
embeds_many :deploys
embeds_one :issue_tracker
references_many :errs, :dependent => :destroy

before_validation :generate_api_key, :on => :create

validates_presence_of :name, :api_key
validates_uniqueness_of :name, :allow_blank => true
validates_uniqueness_of :api_key, :allow_blank => true
validates_associated :watchers
validate :check_issue_tracker

accepts_nested_attributes_for :watchers, :allow_destroy => true,
:reject_if => proc { |attrs| attrs[:user_id].blank? && attrs[:email].blank? }
accepts_nested_attributes_for :issue_tracker, :allow_destroy => true,
:reject_if => proc { |attrs| !%w( lighthouseapp redmine ).include?(attrs[:issue_tracker_type]) }

# Mongoid Bug: find(id) on association proxies returns an Enumerator
def self.find_by_id!(app_id)
where(:_id => app_id).first || raise(Mongoid::Errors::DocumentNotFound.new(self,app_id))
end

def self.find_by_api_key!(key)
where(:api_key => key).first || raise(Mongoid::Errors::DocumentNotFound.new(self,key))
end

def last_deploy_at
deploys.last && deploys.last.created_at
end
Expand All @@ -58,9 +58,9 @@ def notify_on_deploys
!(self[:notify_on_deploys] == false)
end
alias :notify_on_deploys? :notify_on_deploys

protected

def generate_api_key
self.api_key ||= ActiveSupport::SecureRandom.hex
end
Expand Down
26 changes: 14 additions & 12 deletions app/models/err.rb
@@ -1,7 +1,7 @@
class Err
include Mongoid::Document
include Mongoid::Timestamps

field :klass
field :component
field :action
Expand All @@ -10,42 +10,44 @@ class Err
field :last_notice_at, :type => DateTime
field :resolved, :type => Boolean, :default => false
field :issue_link, :type => String
field :notices_count, :type => Integer, :default => 0
field :message

index :last_notice_at
index :app_id

referenced_in :app
embeds_many :notices
references_many :notices

validates_presence_of :klass, :environment

scope :resolved, where(:resolved => true)
scope :unresolved, where(:resolved => false)
scope :ordered, order_by(:last_notice_at.desc)
scope :in_env, lambda {|env| where(:environment => env)}
scope :for_apps, lambda {|apps| where(:app_id.in => apps.all.map(&:id))}

def self.for(attrs)
app = attrs.delete(:app)
app.errs.where(attrs).first || app.errs.create!(attrs)
end

def resolve!
self.update_attributes!(:resolved => true)
end

def unresolved?
!resolved?
end

def where
where = component.dup
where << "##{action}" if action.present?
where
end

def message
notices.first.try(:message) || klass
super || klass
end
end

end

0 comments on commit 40453cc

Please sign in to comment.