Permalink
Browse files

added activity items

  • Loading branch information...
1 parent 3f765ab commit a0913e339d1fb3c2cb55ea8c3e803c2055e16b22 Mark Yoon committed Jul 17, 2009
View
@@ -10,15 +10,29 @@ Works with Rails 2.3.
class FooController < ActionController::Base
has_view_trail :except => :index
+
+ def index
+ end
+
+ def search
+ @results = Model.search(params[:query])
+
+ audit({:results => @results.map(&:id), :flag => "blue"})
+ end
end
-Takes :except and :only conditions like other controller filters
+ Takes :except and :only conditions like other controller filters
- >> Activities.last.whodunnit
- >> Activities.last.created_at
- >> Activities.last.controller
- >> Activities.last.action
- >> Activities.last.params
+ Also allows you to audit an arbitrary key/value hash
+
+ For example: when you visit /foo/search?query=pie
+
+ >> Activities.last.whodunnit #=> (@controller.current_user)
+ >> Activities.last.created_at #=> (date/time of activity)
+ >> Activities.last.controller #=> FooController
+ >> Activities.last.action #=> "search"
+ >> Activities.last.params #=> {:query => "pie"}
+ >> Activities.last.activity_items #=> [{:key => :results, :value => [2, 12, 14]}], [{:key => :flag, :value => "blue"}]
## Installation
@@ -23,7 +23,15 @@
end
Then /^the last activity should have params$/ do
- Activity.last.params.should == HashWithIndifferentAccess.new({:check => "yourself"})
+ HashWithIndifferentAccess.new(Activity.last.params).should == HashWithIndifferentAccess.new({:check => "yourself"})
+end
+
+Then /^the last activity should have data$/ do
+ Activity.last.activity_items.should have(2).activity_items
+ Activity.last.activity_items.first.key.should == "pi"
+ Activity.last.activity_items.first.value.should == [3,1,4,1,5,9]
+ Activity.last.activity_items.last.key.should == "pie"
+ Activity.last.activity_items.last.value.should == ["apple", "blueberry", "pecan", "strawberry"]
end
View
@@ -37,10 +37,15 @@ def foo
def bar
head :ok
end
+ def baz
+ audit({:pi => [3,1,4,1,5,9], :pie => ["apple", "blueberry", "pecan", "strawberry"]})
+ head :ok
+ end
end
# Sets up routes
ActionController::Routing::Routes.draw do |map|
map.foo "/foo", :controller => "foo", :action => "foo"
- map.foo "/bar", :controller => "foo", :action => "bar"
+ map.bar "/bar", :controller => "foo", :action => "bar"
+ map.baz "/baz", :controller => "foo", :action => "baz"
end
View
@@ -10,24 +10,6 @@ def path_to(page_name)
when /the homepage/
default_path
-
- when /the login page/
- authentication_index_path
-
- when /the dashboard/
- dashboard_path
-
- when /the all studies page/
- studies_path
-
- when /my subjects/
- subjects_path
-
- when /the study page for id "([^\"]*)"/
- "/studies/#{$1}"
-
- when /the search page/
- search_path
# Add more mappings here.
# Here is a more fancy example:
@@ -32,11 +32,17 @@ Feature: ViewTrail
And the last activity should have "312" whodunnit
And the last activity should have "foo" action
- Scenario: GET request
+ Scenario: GET request with params
Given I get the foo page with params
Then I should have 5 activities
And the last activity should have params
+ Scenario: GET request with params and arbitrary data
+ Given I get the baz page with params
+ Then I should have 6 activities
+ And the last activity should have data
+ And the last activity should have params
+
Scenario: GET request on excepted path
Given I have 0 activities
When I get the bar page
@@ -1,17 +1,21 @@
-class CreateActivities < ActiveRecord::Migration
+class CreateActivitiesAndItems < ActiveRecord::Migration
def self.up
create_table :activities do |t|
t.string :controller
t.string :action
t.text :params
t.string :whodunnit
t.datetime :created_at
+ end
+ create_table :activity_items do |t|
+ t.integer :activity_id
+ t.string :key
+ t.text :value
end
- # add_index :activities, [:item_type, :item_id]
end
def self.down
- # remove_index :activities, [:item_type, :item_id]
drop_table :activities
+ drop_table :activity_items
end
-end
+end
@@ -2,8 +2,9 @@ class ViewTrailGenerator < Rails::Generator::Base
def manifest
record do |m|
- m.migration_template 'create_activities.rb', 'db/migrate', :migration_file_name => 'create_activities'
+ m.migration_template 'create_activities_and_items.rb', 'db/migrate', :migration_file_name => 'create_activities_and_items'
end
+
end
end
View
@@ -1,4 +1,5 @@
require 'view_trail/activity'
+require 'view_trail/activity_item'
module ViewTrail
@@whodunnit = nil
@@ -42,9 +43,16 @@ def view_trail_on
end
module InstanceMethods
+ def audit(items)
+ @activity_items = items
+ end
def record_activity
if self.class.view_trail_active
- Activity.create(:whodunnit => ViewTrail.whodunnit, :controller => params.delete(:controller), :action => params.delete(:action), :params => params)
+ a = Activity.create(:whodunnit => ViewTrail.whodunnit, :controller => params.delete(:controller), :action => params.delete(:action), :params => params)
+ (@activity_items || {}).each do |k,v|
+ a.activity_items.create(:key => k.to_s, :value => v)
+ end
+ @activity_items = nil
end
end
end
@@ -1,3 +1,4 @@
class Activity < ActiveRecord::Base
serialize :params
+ has_many :activity_items
end
@@ -0,0 +1,3 @@
+class ActivityItem < ActiveRecord::Base
+ serialize :value
+end

0 comments on commit a0913e3

Please sign in to comment.