Skip to content

Commit

Permalink
View stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Clayton committed Apr 18, 2009
1 parent 2567807 commit 5d90eaf
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/models/watched_exception.rb
@@ -1,2 +1,3 @@
class WatchedException < ActiveRecord::Base
include Watchtower::WatchedExceptionBase
end
2 changes: 1 addition & 1 deletion app/views/layouts/watchtower.erb
Expand Up @@ -7,7 +7,7 @@
<!--[if lt IE 8]>
<%= stylesheet_link_tag "watchtower/ie" %>
<![endif]-->
<%= javascript_include_tag "watchtower/jquery-1.3.2.min" %>
<%= javascript_include_tag "watchtower/jquery-1.3.2.min", "watchtower/jquery.watchtower" %>
<%= yield :head %>
</head>
<body>
Expand Down
21 changes: 21 additions & 0 deletions app/views/watchtower/_recordset.erb
@@ -0,0 +1,21 @@
<table>
<thead>
<tr>
<th>Exception</th>
<th>Message</th>
<th>Controller Action</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<% watched_exceptions.each do |exception| %>
<tr>
<td><%= link_to exception.exception_class, watchtower_path(exception) %></td>
<td><%= h exception.message %></td>
<td><%= exception.controller_action %></td>
<td><%= distance_of_time_in_words_to_now exception.created_at %> ago</td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate watched_exceptions %>
41 changes: 40 additions & 1 deletion app/views/watchtower/index.html.erb
@@ -1 +1,40 @@
welcome
<div class="content-main">
<%= render :partial => "recordset", :locals => {:watched_exceptions => @watched_exceptions} %>
</div>
<div class="content-extra">
<ul class="filter-exception_class">
<% WatchedException.exception_classes.each do |exception_class| %>
<li><%= exception_class %></li>
<% end %>
</ul>
<ul class="filter-controller_action">
<% WatchedException.controller_actions.each do |controller_action| %>
<li><%= controller_action %></li>
<% end %>
</ul>
</div>
<% content_for :footer do %>
<script type="text/javascript">
(function($) {
var watchtower = $.watchtower();

$("ul[class^=filter-] li").each(function(idx, li) {
var $li = $(li),
filter = $li.parent().attr("class").replace("filter-", ""),
name = $li.text();

$li.click(function() {
watchtower.filters(filter).clear();

if($li.hasClass("active-filter")) {
$li.removeClass("active-filter");
} else {
$li.siblings().removeClass("active-filter");
$li.addClass("active-filter");
watchtower.filters(filter).add(name);
}
});
});
})(jQuery);
</script>
<% end %>
16 changes: 16 additions & 0 deletions app/views/watchtower/show.html.erb
@@ -0,0 +1,16 @@
<h2><%= @watched_exception.exception_class %> in <%= @watched_exception.controller_action %></h2>
<h3><%= h @watched_exception.message %></h3>
<dl>
<dt>Created</dt>
<dd><%= distance_of_time_in_words_to_now @watched_exception.created_at %> ago (<%= @watched_exception.created_at.to_s(:long) %>)</dd>
<dt>Parameters</dt>
<dd><code><%= @watched_exception.parameters %></code></dd>
<dt>Backtrace</dt>
<dd><code><pre><%= h @watched_exception.backtrace.strip %></pre></code></dd>
<dt>Format</dt>
<dd><code><%= h @watched_exception.format %></code></dd>
<dt>Application Path</dt>
<dd><code><%= h @watched_exception.application_path %></code></dd>
<dt>Full Request</dt>
<dd><code><%= h @watched_exception.request %></code></dd>
</dl>
4 changes: 2 additions & 2 deletions db/migrate/20090418041923_create_watched_exceptions.rb
Expand Up @@ -2,13 +2,13 @@ class CreateWatchedExceptions < ActiveRecord::Migration
def self.up
create_table :watched_exceptions do |t|
t.string :controller_name, :action_name, :controller_action, :exception_class, :key
t.string :parameters, :format, :application_path
t.string :parameters, :format, :application_path, :message
t.text :backtrace, :request
t.datetime :created_at
end

change_table :watched_exceptions do |t|
[:controller_name, :action_name, :controller_action, :exception_class, :key, :created_at].each do |col|
[:controller_name, :action_name, :controller_action, :exception_class, :key, :created_at, :message].each do |col|
t.index col
end
end
Expand Down
3 changes: 1 addition & 2 deletions init.rb
@@ -1,3 +1,2 @@
# Include hook code here
require "will_paginate"
require "watchtower"
WatchedException.send :include, Watchtower::WatchedExceptionBase
4 changes: 3 additions & 1 deletion lib/watchtower.rb
@@ -1,3 +1,5 @@
# Watchtower
require "watchtower/controller_base"
require "watchtower/watched_exception_base"
require "watchtower/controller_base"
require "watchtower/application_controller_base"
require "watchtower/watched_exceptions_presenter"
18 changes: 18 additions & 0 deletions lib/watchtower/application_controller_base.rb
@@ -0,0 +1,18 @@
require "ipaddr"

module Watchtower
module ApplicationControllerBase

def self.included(base)
base.class_eval do
def rescue_action_in_public_with_watchtower(*args)
WatchedException.create_from_exception(self, args.first)
rescue_action_in_public_without_watchtower(*args)
end

alias_method_chain :rescue_action_in_public, :watchtower
end
end
end
end

6 changes: 2 additions & 4 deletions lib/watchtower/controller_base.rb
Expand Up @@ -10,12 +10,10 @@ def self.included(base)
module InstanceMethods

def index
@watched_exceptions = WatchedException.all
@watched_exceptions = WatchedExceptionsPresenter.new(params)
end

def show

end
def show; end

def destroy
@watched_exception.destroy
Expand Down
39 changes: 39 additions & 0 deletions lib/watchtower/watched_exception_base.rb
Expand Up @@ -4,18 +4,28 @@ def self.included(base)
base.send :include, Callbacks
base.send :include, Validations
base.send :include, Scopes
base.extend ClassMethods
end

module Callbacks
def self.included(base)
base.send :include, InstanceMethods
base.before_validation_on_create :generate_key
base.before_save :generate_controller_action, :clean_backtrace
end

module InstanceMethods
def generate_key
self.key = ActiveSupport::SecureRandom.hex(12)
end

def generate_controller_action
self.controller_action = "#{self.controller_name}/#{self.action_name}"
end

def clean_backtrace
# self.backtrace = self.
end
end
end

Expand All @@ -31,9 +41,38 @@ def self.included(base)
base.class_eval do
default_scope :order => "#{WatchedException.quoted_table_name}.created_at DESC"
named_scope :recent, lambda {|*ct| ct = ct.first || 5; { :limit => ct }}
named_scope :search, lambda { |query| {
:conditions => [
[ :controller_name, :action_name,
:controller_action, :exception_class,
:parameters, :message].map { |attribute| "#{WatchedException.quoted_table_name}.#{attribute} LIKE :query"}.join(" OR "), {:query => query}
]
}}

end
end
end

module ClassMethods
def create_from_exception(controller, exception)
create! :exception_class => exception.class.name,
:controller_name => controller.controller_name,
:action_name => controller.action_name,
:backtrace => controller.send(:clean_backtrace, exception),
:request => controller.request.inspect,
:parameters => controller.request.parameters.inspect,
:format => controller.request.format.to_s,
:message => exception.message.to_s,
:application_path => ::RAILS_ROOT
end

def exception_classes
all(:select => "DISTINCT exception_class", :order => "exception_class").collect(&:exception_class)
end

def controller_actions
all(:select => "DISTINCT controller_action", :order => "controller_action").collect(&:controller_action)
end
end
end
end
21 changes: 21 additions & 0 deletions lib/watchtower/watched_exceptions_presenter.rb
@@ -0,0 +1,21 @@
class WatchedExceptionsPresenter
include Enumerable

def initialize(params)
scope = WatchedException
scope = scope.search("%#{params[:query]}%") if params[:query]
scope = scope.scoped_by_action_name(params[:action_name]) if params[:action_name]
scope = scope.scoped_by_controller_name(params[:controller_name]) if params[:controller_name]
scope = scope.scoped_by_controller_action(params[:controller_action]) if params[:controller_action]
scope = scope.scoped_by_exception_class(params[:exception_class]) if params[:exception_class]
@watched_exceptions = scope.paginate(:page => params[:page], :per_page => 20)
end

def each(&block)
@watched_exceptions.each(&block)
end

def method_missing(call, *args)
@watched_exceptions.send call, *args
end
end
39 changes: 39 additions & 0 deletions public/javascripts/watchtower/jquery.watchtower.js
@@ -0,0 +1,39 @@
(function($) {
var $doc = $(document);

$.watchtower = function() {
var self = {},
filterCache = {};

return {
filters: function(name) {
return {
set: function(filter) {
if(filterCache[name]) { $doc.trigger("watchtower-filter-remove", {filter: filter, name: name}); }
filterCache[name] = filter;
$doc.trigger("watchtower-filter-add", {filter: filter, name: name});
},
clear: function() {
var oldFilter = filterCache[name];
filterCache[name] = null;
$doc.trigger("watchtower-filter-remove", {filter: oldFilter, name: name});
}
};
}
};
};

$.watchtower.bindings = function() {
$doc.bind("watchtower-filter-add", function(event, info) {
$doc.trigger("watchtower-filter-change", info);
});

$doc.bind("watchtower-filter-remove", function(event, info) {
$doc.trigger("watchtower-filter-change", info);
});

$doc.bind("watchtower-filter-change", function(event, info) {

});
};
})(jQuery);

0 comments on commit 5d90eaf

Please sign in to comment.