public_activity 0.2.5 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
|
|
1
|
+
module PublicActivity
|
2
|
+
# Module extending classes that serve as owners
|
3
|
+
module Activist
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
# Module extending classes that serve as owners
|
6
|
+
module ClassMethods
|
7
|
+
# Adds has_many :activities association to model
|
8
|
+
# so you can list activities performed by the owner.
|
9
|
+
# It is completely optional, but simplifies your work.
|
10
|
+
#
|
11
|
+
# == Usage:
|
12
|
+
# In model:
|
13
|
+
#
|
14
|
+
# class User < ActiveRecord::Base
|
15
|
+
# activist
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# In controller:
|
19
|
+
# User.first.activities
|
20
|
+
#
|
21
|
+
def activist
|
22
|
+
has_many :activities, :class_name => "PublicActivity::Activity", :as => :owner
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -13,7 +13,9 @@ module PublicActivity
|
|
13
13
|
|
14
14
|
# Virtual attribute returning already
|
15
15
|
# translated key with params passed
|
16
|
-
# to i18n.translate function
|
16
|
+
# to i18n.translate function. You can pass additional Hash
|
17
|
+
# you want to be passed to translation method. It will be merged with the default ones.
|
18
|
+
#
|
17
19
|
# == Example:
|
18
20
|
#
|
19
21
|
# Let's say you want to show article's title inside Activity message.
|
@@ -37,7 +39,8 @@ module PublicActivity
|
|
37
39
|
#
|
38
40
|
# Now when you list articles, you should see:
|
39
41
|
# @article.activities.last.text #=> "Someone has created an article 'Rails 3.0.5 released!'"
|
40
|
-
def text
|
42
|
+
def text(params = {})
|
43
|
+
parameters.merge! params
|
41
44
|
I18n.t(key, parameters || {})
|
42
45
|
end
|
43
46
|
end
|
@@ -17,14 +17,15 @@ module PublicActivity
|
|
17
17
|
if self.activity_owner
|
18
18
|
owner = self.activity_owner
|
19
19
|
else
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
owner = self.class.activity_owner_global
|
21
|
+
end
|
22
|
+
|
23
|
+
case owner
|
24
|
+
when Symbol
|
25
|
+
owner = self.try(owner)
|
26
|
+
when Proc
|
27
|
+
owner = owner.call(self)
|
26
28
|
end
|
27
|
-
|
28
29
|
#customizable parameters
|
29
30
|
parameters = self.class.activity_params_global
|
30
31
|
parameters.merge! self.activity_params if self.activity_params
|
@@ -2,7 +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
|
+
included do
|
7
|
+
class_attribute :activity_owner_global, :activity_params_global
|
8
|
+
self.activity_owner_global = nil
|
9
|
+
self.activity_params_global = {}
|
10
|
+
end
|
6
11
|
# Set or get parameters that will be passed to {Activity} when saving
|
7
12
|
#
|
8
13
|
# == Usage:
|
@@ -20,8 +25,7 @@ module PublicActivity
|
|
20
25
|
# changes after creating this {Activity}.
|
21
26
|
attr_accessor :activity_params
|
22
27
|
@activity_params = {}
|
23
|
-
# Set or get
|
24
|
-
# Same rules apply like with the other attributes described.
|
28
|
+
# Set or get owner object responsible for the {Activity}.
|
25
29
|
#
|
26
30
|
# == Usage:
|
27
31
|
# In model:
|
@@ -32,12 +36,14 @@ module PublicActivity
|
|
32
36
|
# Controller:
|
33
37
|
#
|
34
38
|
# @article = Article.new
|
35
|
-
# @article.activity_owner = current_user #where current_user is an object of logged in user
|
39
|
+
# @article.activity_owner = current_user # where current_user is an object of logged in user
|
40
|
+
# @article.activity_owner = :author # OR: take @article.author attribute
|
41
|
+
# @article.activity_owner = proc {|o| o.author } # OR: provide a Proc with custom code
|
36
42
|
# @article.save
|
37
|
-
# @article.activities.last.
|
43
|
+
# @article.activities.last.owner #=> Returns owner object
|
38
44
|
attr_accessor :activity_owner
|
39
45
|
@activity_owner = nil
|
40
|
-
# Set or get custom i18n key passed to {Activity}
|
46
|
+
# Set or get custom i18n key passed to {Activity}, later used in {Activity#text}
|
41
47
|
#
|
42
48
|
# == Usage:
|
43
49
|
# In model:
|
@@ -60,24 +66,43 @@ module PublicActivity
|
|
60
66
|
# @article.activities.last.key #=> "my.custom.article.key"
|
61
67
|
attr_accessor :activity_key
|
62
68
|
@activity_key = nil
|
63
|
-
|
69
|
+
|
70
|
+
# Module with basic +tracked+ method that enables tracking models.
|
64
71
|
module ClassMethods
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
# Adds required callbacks for creating and updating
|
73
|
+
# tracked models and adds +activities+ relation for listing
|
74
|
+
# associated activities.
|
75
|
+
#
|
76
|
+
# == Parameters:
|
77
|
+
# [:owner]
|
78
|
+
# Specify the owner of the {Activity} (person responsible for the action).
|
79
|
+
# It can be a Proc, Symbol or an ActiveRecord object:
|
80
|
+
# == Examples:
|
81
|
+
# @article.activity :owner => :author
|
82
|
+
# @article.activity :owner => {|o| o.author}
|
83
|
+
# @article.activity :owner => User.where(:login => 'piotrek').first
|
84
|
+
# Keep in mind that owner relation is polymorphic, so you can't just provide id number of the owner object.
|
85
|
+
# [:params]
|
86
|
+
# Accepts a Hash with custom parameters you want to pass to i18n.translate
|
87
|
+
# method. It is later used in {Activity#text} method.
|
88
|
+
# == Example:
|
89
|
+
# @article.activity :parameters => {:title => @article.title, :short => truncate(@article.text, :length => 50)}
|
90
|
+
# Everything specified here has a lower priority than parameters specified directly in {Tracked#activity} method.
|
91
|
+
# So treat it as a place where you provide 'default' values.
|
92
|
+
# For more dynamic settings refer to {Activity} model
|
93
|
+
# documentation.
|
94
|
+
def tracked(options = {})
|
95
|
+
include Common
|
96
|
+
include Creation
|
97
|
+
include Destruction
|
98
|
+
|
99
|
+
if options[:owner]
|
100
|
+
self.activity_owner_global = options[:owner]
|
74
101
|
end
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
def tracked?
|
80
|
-
false
|
102
|
+
if options[:params]
|
103
|
+
self.activity_params_global = options[:params]
|
104
|
+
end
|
105
|
+
has_many :activities, :class_name => "PublicActivity::Activity", :as => :trackable
|
81
106
|
end
|
82
107
|
end
|
83
108
|
|
@@ -87,6 +112,24 @@ module PublicActivity
|
|
87
112
|
# in one line. Accepts a hash with 3 keys:
|
88
113
|
# :key, :owner, :params. You can specify all of them or just the ones you want to overwrite.
|
89
114
|
#
|
115
|
+
# === Options
|
116
|
+
#
|
117
|
+
# [:key]
|
118
|
+
# Accepts a string that will be used as a i18n key for {Activity#text} method.
|
119
|
+
# [:owner]
|
120
|
+
# Specify the owner of the {Activity} (person responsible for the action).
|
121
|
+
# It can be a Proc, Symbol or an ActiveRecord object:
|
122
|
+
# == Examples:
|
123
|
+
# @article.activity :owner => :author
|
124
|
+
# @article.activity :owner => {|o| o.author}
|
125
|
+
# @article.activity :owner => User.where(:login => 'piotrek').first
|
126
|
+
# Keep in mind that owner relation is polymorphic, so you can't just provide id number of the owner object.
|
127
|
+
# [:params]
|
128
|
+
# Accepts a Hash with custom parameters you want to pass to i18n.translate
|
129
|
+
# method. It is later used in {Activity#text} method.
|
130
|
+
# == Example:
|
131
|
+
# @article.activity :parameters => {:title => @article.title, :short => truncate(@article.text, :length => 50)}
|
132
|
+
#
|
90
133
|
# == Usage:
|
91
134
|
# In model:
|
92
135
|
#
|
data/lib/public_activity.rb
CHANGED
@@ -37,6 +37,7 @@ require 'active_record'
|
|
37
37
|
module PublicActivity
|
38
38
|
extend ActiveSupport::Concern
|
39
39
|
extend ActiveSupport::Autoload
|
40
|
+
autoload :Activist
|
40
41
|
autoload :Activity
|
41
42
|
autoload :Tracked
|
42
43
|
autoload :Creation
|
@@ -45,58 +46,9 @@ module PublicActivity
|
|
45
46
|
autoload :Common
|
46
47
|
|
47
48
|
included do
|
48
|
-
class_attribute :activity_owner_global, :activity_params_global
|
49
|
-
self.activity_owner_global = nil
|
50
|
-
self.activity_params_global = {}
|
51
49
|
include Tracked
|
50
|
+
include Activist
|
52
51
|
end
|
53
|
-
|
54
|
-
# Module with basic +tracked+ method that enables tracking models.
|
55
|
-
module ClassMethods
|
56
|
-
# Adds required callbacks for creating and updating
|
57
|
-
# tracked models and adds +activities+ relation for listing
|
58
|
-
# associated activities.
|
59
|
-
#
|
60
|
-
# == Parameters:
|
61
|
-
# :owner::
|
62
|
-
# You can pass a Symbol or a String with an attribute from
|
63
|
-
# which public_activity should take +user id+ responsible for
|
64
|
-
# this activity.
|
65
|
-
#
|
66
|
-
# For example:
|
67
|
-
# tracked :owner => :author
|
68
|
-
# will take +owner+ from tracked model's +author+ attribute.
|
69
|
-
#
|
70
|
-
# If you need more complex logic, you can pass a Proc:
|
71
|
-
# tracked :owner => Proc.new{ User.first }
|
72
|
-
# :params::
|
73
|
-
# Accepts a Hash containing parameters you wish
|
74
|
-
# to pass to every {Activity} created from this model.
|
75
|
-
#
|
76
|
-
# For example, if you want to pass a parameter that
|
77
|
-
# should be in every {Activity}, you can do this:
|
78
|
-
# tracked :params => {:user_name => "Piotrek"}
|
79
|
-
# These params are passed to i18n.translate
|
80
|
-
# when using {PublicActivity::Activity#text}, which returns
|
81
|
-
# already translated {Activity} message.
|
82
|
-
# For more dynamic settings refer to {Activity} model
|
83
|
-
# documentation.
|
84
|
-
def tracked(options = {})
|
85
|
-
return if tracked?
|
86
|
-
include Common
|
87
|
-
include Creation
|
88
|
-
include Destruction
|
89
|
-
|
90
|
-
if options[:owner]
|
91
|
-
self.activity_owner_global = options[:owner]
|
92
|
-
end
|
93
|
-
if options[:params]
|
94
|
-
self.activity_params_global = options[:params]
|
95
|
-
end
|
96
|
-
has_many :activities, :class_name => "PublicActivity::Activity", :as => :trackable
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
52
|
end
|
101
53
|
|
102
54
|
ActiveRecord::Base.send :include, PublicActivity
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: public_activity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: "0.3"
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Piotrek Oko\xC5\x84ski"
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-04-
|
14
|
+
date: 2011-04-10 00:00:00 +02:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/generators/public_activity/migration/migration_generator.rb
|
72
72
|
- lib/generators/public_activity/migration/templates/migration.rb
|
73
73
|
- lib/public_activity.rb
|
74
|
+
- lib/public_activity/activist.rb
|
74
75
|
- lib/public_activity/activity.rb
|
75
76
|
- lib/public_activity/common.rb
|
76
77
|
- lib/public_activity/creation.rb
|