public_activity 0.2 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/generators/public_activity.rb +3 -0
- data/lib/generators/public_activity/migration/migration_generator.rb +2 -1
- data/lib/generators/public_activity/migration/templates/migration.rb +3 -1
- data/lib/public_activity.rb +1 -0
- data/lib/public_activity/common.rb +5 -3
- data/lib/public_activity/creation.rb +5 -4
- data/lib/public_activity/destruction.rb +3 -2
- data/lib/public_activity/tracked.rb +34 -5
- data/lib/public_activity/version.rb +2 -1
- metadata +2 -2
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'rails/generators/named_base'
|
2
2
|
|
3
3
|
module PublicActivity
|
4
|
+
# A generator module with Activity table schema.
|
4
5
|
module Generators
|
6
|
+
# A base module
|
5
7
|
module Base
|
8
|
+
# Get path for migration template
|
6
9
|
def source_root
|
7
10
|
@_public_activity_source_root ||= File.expand_path(File.join('../public_activity', generator_name, 'templates'), __FILE__)
|
8
11
|
end
|
@@ -3,11 +3,12 @@ require 'rails/generators/active_record'
|
|
3
3
|
|
4
4
|
module PublicActivity
|
5
5
|
module Generators
|
6
|
+
# Migration generator that creates migration file from template
|
6
7
|
class MigrationGenerator < ActiveRecord::Generators::Base
|
7
8
|
extend Base
|
8
9
|
|
9
10
|
argument :name, :type => :string, :default => 'create_activities'
|
10
|
-
|
11
|
+
# Create migration in project's folder
|
11
12
|
def generate_files
|
12
13
|
migration_template 'migration.rb', "db/migrate/#{name}"
|
13
14
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
+
# Migration responsible for creating a table with activities
|
1
2
|
class CreateActivities < ActiveRecord::Migration
|
3
|
+
# Create table
|
2
4
|
def self.up
|
3
5
|
create_table :activities do |t|
|
4
6
|
t.belongs_to :trackable, :polymorphic => true
|
@@ -9,7 +11,7 @@ class CreateActivities < ActiveRecord::Migration
|
|
9
11
|
t.timestamps
|
10
12
|
end
|
11
13
|
end
|
12
|
-
|
14
|
+
# Drop table
|
13
15
|
def self.down
|
14
16
|
drop_table :activities
|
15
17
|
end
|
data/lib/public_activity.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
module PublicActivity
|
2
|
+
# Common methods shared across the gem.
|
2
3
|
module Common
|
3
4
|
extend ActiveSupport::Concern
|
4
|
-
|
5
|
+
# Instance methods used by other methods in PublicActivity module.
|
5
6
|
module InstanceMethods
|
6
7
|
private
|
7
8
|
# Creates activity based on supplied arguments
|
8
9
|
def create_activity(key, owner, params)
|
9
10
|
self.activities.create(:key => key, :owner => owner, :parameters => params)
|
10
11
|
end
|
11
|
-
#
|
12
|
+
# Prepares settings used during creation of Activity record.
|
12
13
|
# params passed directly to tracked model have priority over
|
13
14
|
# settings specified in tracked() method
|
14
15
|
def prepare_settings
|
@@ -23,10 +24,11 @@ module PublicActivity
|
|
23
24
|
owner = self.class.activity_owner_global.call
|
24
25
|
end
|
25
26
|
end
|
27
|
+
|
26
28
|
#customizable parameters
|
27
29
|
parameters = self.class.activity_params_global
|
28
30
|
parameters.merge! self.activity_params if self.activity_params
|
29
|
-
return {:owner => owner, :parameters => parameters}
|
31
|
+
return {:key => self.activity_key,:owner => owner, :parameters => parameters}
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
@@ -1,24 +1,25 @@
|
|
1
1
|
module PublicActivity
|
2
|
+
# Handles creation of Activities upon destruction and update of tracked model.
|
2
3
|
module Creation
|
3
4
|
extend ActiveSupport::Concern
|
4
|
-
|
5
|
+
|
5
6
|
included do
|
6
7
|
after_create :activity_on_create
|
7
8
|
after_update :activity_on_update
|
8
9
|
end
|
9
|
-
|
10
|
+
# Handlers responsible for creating Activities.
|
10
11
|
module InstanceMethods
|
11
12
|
private
|
12
13
|
# Creates activity upon creation of the tracked model
|
13
14
|
def activity_on_create
|
14
15
|
settings = prepare_settings
|
15
|
-
create_activity("activity."+self.class.name.
|
16
|
+
create_activity(settings[:key] || "activity."+self.class.name.parameterize('_')+".create", settings[:owner], settings[:parameters])
|
16
17
|
end
|
17
18
|
|
18
19
|
# Creates activity upon modification of the tracked model
|
19
20
|
def activity_on_update
|
20
21
|
settings = prepare_settings
|
21
|
-
create_activity("activity."+self.class.name.
|
22
|
+
create_activity(settings[:key] || "activity."+self.class.name.parameterize('_')+".update", settings[:owner], settings[:parameters])
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
@@ -1,17 +1,18 @@
|
|
1
1
|
module PublicActivity
|
2
|
+
# Handles creation of Activities upon destruction of tracked model.
|
2
3
|
module Destruction
|
3
4
|
extend ActiveSupport::Concern
|
4
5
|
|
5
6
|
included do
|
6
7
|
before_destroy :activity_on_destroy
|
7
8
|
end
|
8
|
-
|
9
|
+
# Handlers responsible for adding {Activity} when object is destroyed.
|
9
10
|
module InstanceMethods
|
10
11
|
private
|
11
12
|
# Records an activity upon destruction of the tracked model
|
12
13
|
def activity_on_destroy
|
13
14
|
settings = prepare_settings
|
14
|
-
create_activity("activity."+self.class.name.
|
15
|
+
create_activity("activity."+self.class.name.parameterize('_')+".destroy", settings[:owner], settings[:parameters])
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
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
|
|
@@ -16,7 +16,7 @@ module PublicActivity
|
|
16
16
|
# @article = Article.new
|
17
17
|
# @article.activity_params = {:article_title => @article.title}
|
18
18
|
# @article.save
|
19
|
-
# This way you can pass strings that should remain constant, even when
|
19
|
+
# This way you can pass strings that should remain constant, even when Article
|
20
20
|
# changes after creating this {Activity}.
|
21
21
|
attr_accessor :activity_params
|
22
22
|
@activity_params = {}
|
@@ -51,7 +51,7 @@ module PublicActivity
|
|
51
51
|
# @article = Article.new
|
52
52
|
# @article.save
|
53
53
|
# @article.activities.last.key #=> "activity.article.create"
|
54
|
-
# By default, key looks like "activity.
|
54
|
+
# By default, key looks like "activity.[class_name].[create|update|destroy]"
|
55
55
|
#
|
56
56
|
# You can customize it, by setting your own key:
|
57
57
|
# @article = Article.new
|
@@ -60,9 +60,10 @@ module PublicActivity
|
|
60
60
|
# @article.activities.last.key #=> "my.custom.article.key"
|
61
61
|
attr_accessor :activity_key
|
62
62
|
@activity_key = nil
|
63
|
+
# Placeholder methods for classes not tracked by public_activity gem.
|
64
|
+
module ClassMethods
|
63
65
|
# Overrides the +tracked+ method to first define the +tracked?+ class method before
|
64
66
|
# deferring to the original +tracked+.
|
65
|
-
module ClassMethods
|
66
67
|
def tracked(*args)
|
67
68
|
super(*args)
|
68
69
|
|
@@ -79,6 +80,34 @@ module PublicActivity
|
|
79
80
|
false
|
80
81
|
end
|
81
82
|
end
|
82
|
-
|
83
|
+
|
84
|
+
# A module with shortcut method for setting own parameters to the {Activity} model
|
85
|
+
module InstanceMethods
|
86
|
+
# A shortcut method for setting custom key, owner and parameters of {Activity}
|
87
|
+
# in one line. Accepts a hash with 3 keys:
|
88
|
+
# :key, :owner, :params. You can specify all of them or just the ones you want to overwrite.
|
89
|
+
#
|
90
|
+
# == Usage:
|
91
|
+
# In model:
|
92
|
+
#
|
93
|
+
# class Article < ActiveRecord::Base
|
94
|
+
# tracked
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# In controller:
|
98
|
+
#
|
99
|
+
# @article = Article.new
|
100
|
+
# @article.title = "New article"
|
101
|
+
# @article.activity :key => "my.custom.article.key", :owner => @article.author, :params => {:title => @article.title}
|
102
|
+
# @article.save
|
103
|
+
# @article.activities.last.key #=> "my.custom.article.key"
|
104
|
+
# @article.activities.last.parameters #=> {:title => "New article"}
|
105
|
+
def activity(options = {})
|
106
|
+
self.activity_key = options[:key] if options[:key]
|
107
|
+
self.activity_owner = options[:owner] if options[:owner]
|
108
|
+
self.activity_params = options[:params] if options[:params]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
83
112
|
end
|
84
113
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: public_activity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version:
|
5
|
+
version: 0.2.5
|
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-
|
14
|
+
date: 2011-04-09 00:00:00 +02:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|