public_activity 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +28 -28
- data/lib/public_activity/activity.rb +7 -11
- data/lib/public_activity/common.rb +2 -2
- data/lib/public_activity/tracked.rb +27 -25
- data/lib/public_activity/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PublicActivity ![Build Status](http://travis-ci.org/pokonski/public_activity.png)
|
2
2
|
|
3
|
-
public_activity provides smooth
|
3
|
+
public_activity provides smooth activity tracking for your ActiveRecord models in Rails 3.
|
4
4
|
Simply put: it records what has been changed or edited and gives you the ability to present those recorded activities to users - in a similar way Github does it.
|
5
5
|
|
6
6
|
## Example
|
@@ -26,58 +26,58 @@ Create migration for activities (in your Rails project):
|
|
26
26
|
Add 'tracked' to the model you want to keep track of:
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
class Article < ActiveRecord::Base
|
30
|
+
tracked
|
31
|
+
end
|
32
32
|
```
|
33
33
|
|
34
34
|
To default the owner to the current user (optional)
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
37
|
+
#Aplication Controller
|
38
|
+
before_filter :define_current_user
|
39
|
+
|
40
|
+
def define_current_user
|
41
|
+
User.current_user = current_user
|
42
|
+
end
|
43
|
+
|
44
|
+
#User.rb (model)
|
45
|
+
class User < ActiveRecord::Base
|
46
|
+
cattr_accessor :current_user
|
47
|
+
end
|
48
48
|
```
|
49
49
|
|
50
50
|
And now, by default create/update/destroy activities are recorded in activities table.
|
51
51
|
To display them you can do a simple query:
|
52
52
|
|
53
53
|
```ruby
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
# some_controller.rb
|
55
|
+
def index
|
56
|
+
@activities = PublicActivity::Activity.all
|
57
|
+
end
|
58
58
|
```
|
59
59
|
|
60
60
|
And in your views:
|
61
61
|
|
62
62
|
```erb
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
<% for activity in @activities %>
|
64
|
+
<%= activity.text %><br/>
|
65
|
+
<% end %>
|
66
66
|
```
|
67
67
|
|
68
68
|
The only thing left is to add templates (config/pba.yml), for example:
|
69
69
|
|
70
70
|
```yaml
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
71
|
+
activity:
|
72
|
+
article:
|
73
|
+
create: 'Article has been created'
|
74
|
+
update: 'Someone has edited the article'
|
75
|
+
destroy: 'Some user removed an article!'
|
76
76
|
```
|
77
77
|
Place this in a file and reference it in a Rails initializer.
|
78
78
|
|
79
79
|
```ruby
|
80
|
-
|
80
|
+
PublicActivity::Activity.template = YAML.load_file("#{Rails.root}/config/pba.yml")
|
81
81
|
```
|
82
82
|
|
83
83
|
This is only a basic example, refer to documentation for more options and customization!
|
@@ -42,7 +42,7 @@ module PublicActivity
|
|
42
42
|
def text(params = {})
|
43
43
|
begin
|
44
44
|
erb_template = resolveTemplate(key)
|
45
|
-
if
|
45
|
+
if erb_template
|
46
46
|
parameters.merge! params
|
47
47
|
renderer = ERB.new(erb_template)
|
48
48
|
renderer.result(binding)
|
@@ -56,17 +56,13 @@ module PublicActivity
|
|
56
56
|
|
57
57
|
private
|
58
58
|
def resolveTemplate(key)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
res = self.template[k]
|
64
|
-
else
|
65
|
-
res = res[k]
|
66
|
-
end
|
67
|
-
end
|
59
|
+
res = nil
|
60
|
+
if self.template
|
61
|
+
key.split(".").each do |k|
|
62
|
+
res = (res ? res[k] : self.template[k])
|
68
63
|
end
|
69
|
-
|
64
|
+
end
|
65
|
+
res
|
70
66
|
end
|
71
67
|
end
|
72
68
|
end
|
@@ -25,7 +25,7 @@ module PublicActivity
|
|
25
25
|
|
26
26
|
activity = self.activities.create(:key => key, :owner => owner, :parameters => params)
|
27
27
|
if !Pusher.app_id.nil? && !Pusher.key.nil? && !Pusher.secret.nil?
|
28
|
-
Pusher['
|
28
|
+
Pusher['activity-channel'].trigger('activity-create', {:key => key, :owner => owner, :parameters => params, :text => activity.text, :object => self})
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -43,7 +43,7 @@ module PublicActivity
|
|
43
43
|
|
44
44
|
case owner
|
45
45
|
when Symbol
|
46
|
-
owner = self.
|
46
|
+
owner = self.__send__(owner)
|
47
47
|
when Proc
|
48
48
|
owner = owner.call(self)
|
49
49
|
end
|
@@ -2,12 +2,12 @@ module PublicActivity
|
|
2
2
|
# Main module extending classes we want to keep track of.
|
3
3
|
module Tracked
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
included do
|
7
7
|
class_attribute :activity_owner_global, :activity_params_global
|
8
8
|
self.activity_owner_global = nil
|
9
9
|
self.activity_params_global = {}
|
10
|
-
end
|
10
|
+
end
|
11
11
|
# Set or get parameters that will be passed to {Activity} when saving
|
12
12
|
#
|
13
13
|
# == Usage:
|
@@ -15,7 +15,7 @@ module PublicActivity
|
|
15
15
|
#
|
16
16
|
# class Article < ActiveRecord::Base
|
17
17
|
# tracked
|
18
|
-
# end
|
18
|
+
# end
|
19
19
|
#
|
20
20
|
# In controller
|
21
21
|
# @article = Article.new
|
@@ -32,7 +32,7 @@ module PublicActivity
|
|
32
32
|
#
|
33
33
|
# class Article < ActiveRecord::Base
|
34
34
|
# tracked
|
35
|
-
# end
|
35
|
+
# end
|
36
36
|
# Controller:
|
37
37
|
#
|
38
38
|
# @article = Article.new
|
@@ -50,7 +50,7 @@ module PublicActivity
|
|
50
50
|
#
|
51
51
|
# class Article < ActiveRecord::Base
|
52
52
|
# tracked
|
53
|
-
# end
|
53
|
+
# end
|
54
54
|
#
|
55
55
|
# In controller:
|
56
56
|
#
|
@@ -66,19 +66,19 @@ module PublicActivity
|
|
66
66
|
# @article.activities.last.key #=> "my.custom.article.key"
|
67
67
|
attr_accessor :activity_key
|
68
68
|
@activity_key = nil
|
69
|
-
|
69
|
+
|
70
70
|
# Module with basic +tracked+ method that enables tracking models.
|
71
71
|
module ClassMethods
|
72
72
|
# Adds required callbacks for creating and updating
|
73
73
|
# tracked models and adds +activities+ relation for listing
|
74
74
|
# associated activities.
|
75
|
-
#
|
75
|
+
#
|
76
76
|
# == Parameters:
|
77
77
|
# [:owner]
|
78
78
|
# Specify the owner of the {Activity} (person responsible for the action).
|
79
79
|
# It can be a Proc, Symbol or an ActiveRecord object:
|
80
80
|
# == Examples:
|
81
|
-
# @article.activity :owner => :author
|
81
|
+
# @article.activity :owner => :author
|
82
82
|
# @article.activity :owner => {|o| o.author}
|
83
83
|
# @article.activity :owner => User.where(:login => 'piotrek').first
|
84
84
|
# Keep in mind that owner relation is polymorphic, so you can't just provide id number of the owner object.
|
@@ -89,48 +89,50 @@ module PublicActivity
|
|
89
89
|
# @article.activity :parameters => {:title => @article.title, :short => truncate(@article.text, :length => 50)}
|
90
90
|
# Everything specified here has a lower priority than parameters specified directly in {#activity} method.
|
91
91
|
# So treat it as a place where you provide 'default' values.
|
92
|
-
# For more dynamic settings refer to {Activity} model
|
92
|
+
# For more dynamic settings refer to {Activity} model
|
93
93
|
# documentation.
|
94
94
|
# [:skip_defaults]
|
95
95
|
# Disables recording of activities on create/update/destroy leaving that to programmer's choice. Check {PublicActivity::Common#create_activity}
|
96
96
|
# for a guide on how to manually record activities.
|
97
97
|
def tracked(options = {})
|
98
98
|
include Common
|
99
|
-
|
99
|
+
|
100
100
|
all_options = [:create, :update, :destroy]
|
101
|
-
|
102
|
-
if !options[:skip_defaults] && !options[:only] && !options[:except]
|
101
|
+
|
102
|
+
if !options[:skip_defaults] && !options[:only] && !options[:except]
|
103
103
|
include Creation
|
104
104
|
include Destruction
|
105
105
|
include Update
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
if options[:except].is_a? Array
|
109
109
|
options[:only] = all_options - options[:except]
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
if options[:only].is_a? Array
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
end
|
113
|
+
options[:only].each do |opt|
|
114
|
+
if opt.eql?(:create)
|
115
|
+
include Creation
|
116
|
+
elsif opt.eql?(:destroy)
|
117
|
+
include Destruction
|
118
|
+
elsif opt.eql?(:update)
|
119
|
+
include Update
|
121
120
|
end
|
121
|
+
end
|
122
122
|
end
|
123
|
-
|
123
|
+
|
124
124
|
if options[:owner]
|
125
125
|
self.activity_owner_global = options[:owner]
|
126
126
|
end
|
127
127
|
if options[:params]
|
128
128
|
self.activity_params_global = options[:params]
|
129
129
|
end
|
130
|
-
has_many :activities, :class_name => "PublicActivity::Activity", :as => :trackable
|
130
|
+
has_many :activities, :class_name => "PublicActivity::Activity", :as => :trackable
|
131
|
+
|
132
|
+
nil
|
131
133
|
end
|
132
134
|
end
|
133
|
-
|
135
|
+
|
134
136
|
# A shortcut method for setting custom key, owner and parameters of {Activity}
|
135
137
|
# in one line. Accepts a hash with 3 keys:
|
136
138
|
# :key, :owner, :params. You can specify all of them or just the ones you want to overwrite.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: public_activity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-08-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
version: '0'
|
148
148
|
requirements: []
|
149
149
|
rubyforge_project:
|
150
|
-
rubygems_version: 1.8.
|
150
|
+
rubygems_version: 1.8.24
|
151
151
|
signing_key:
|
152
152
|
specification_version: 3
|
153
153
|
summary: Smooth activity tracking for ActiveRecord models
|