Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added activity items

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

0 comments on commit a0913e3

Please sign in to comment.
Something went wrong with that request. Please try again.