public_activity 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
6
+ gem 'yard'
7
+ gem 'i18n'
8
+
9
+ group :development, :test do
10
+ gem 'rspec'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.5)
6
+ actionpack (= 3.0.5)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.5)
9
+ activemodel (= 3.0.5)
10
+ activesupport (= 3.0.5)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.5)
19
+ activesupport (= 3.0.5)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.5)
23
+ activemodel (= 3.0.5)
24
+ activesupport (= 3.0.5)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.5)
28
+ activemodel (= 3.0.5)
29
+ activesupport (= 3.0.5)
30
+ activesupport (3.0.5)
31
+ arel (2.0.9)
32
+ builder (2.1.2)
33
+ diff-lcs (1.1.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ i18n (0.5.0)
37
+ mail (2.2.15)
38
+ activesupport (>= 2.3.6)
39
+ i18n (>= 0.4.0)
40
+ mime-types (~> 1.16)
41
+ treetop (~> 1.4.8)
42
+ mime-types (1.16)
43
+ mocha (0.9.12)
44
+ polyglot (0.3.1)
45
+ rack (1.2.1)
46
+ rack-mount (0.6.13)
47
+ rack (>= 1.0.0)
48
+ rack-test (0.5.7)
49
+ rack (>= 1.0)
50
+ rails (3.0.5)
51
+ actionmailer (= 3.0.5)
52
+ actionpack (= 3.0.5)
53
+ activerecord (= 3.0.5)
54
+ activeresource (= 3.0.5)
55
+ activesupport (= 3.0.5)
56
+ bundler (~> 1.0)
57
+ railties (= 3.0.5)
58
+ railties (3.0.5)
59
+ actionpack (= 3.0.5)
60
+ activesupport (= 3.0.5)
61
+ rake (>= 0.8.7)
62
+ thor (~> 0.14.4)
63
+ rake (0.8.7)
64
+ rspec (2.5.0)
65
+ rspec-core (~> 2.5.0)
66
+ rspec-expectations (~> 2.5.0)
67
+ rspec-mocks (~> 2.5.0)
68
+ rspec-core (2.5.1)
69
+ rspec-expectations (2.5.0)
70
+ diff-lcs (~> 1.1.2)
71
+ rspec-mocks (2.5.0)
72
+ thor (0.14.6)
73
+ treetop (1.4.9)
74
+ polyglot (>= 0.3.1)
75
+ tzinfo (0.3.24)
76
+ yard (0.6.4)
77
+
78
+ PLATFORMS
79
+ ruby
80
+
81
+ DEPENDENCIES
82
+ mocha
83
+ rails
84
+ rspec
85
+ yard
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # PublicActivity
2
+
3
+ public_activity provides smooth acitivity tracking for your ActiveRecord models in Rails 3.
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
+
6
+
7
+ ## Installation
8
+
9
+ You can install this gem as you would any other gem:
10
+ gem install public_activity
11
+ or in your Gemfile:
12
+ gem 'public_activity'
13
+
14
+ ## Usage
15
+
16
+ Create migration for activities (in your Rails project):
17
+ rails g public_activity:migration
18
+ rake db:migrate
19
+
20
+ Add 'tracked' to the model you want to keep track of:
21
+ class Article < ActiveRecord::Base
22
+ tracked
23
+ end
24
+ And now, by default create/update activites are recorded in activities table.
25
+ To display them you can do a simple query:
26
+ # some_controller.rb
27
+ def index
28
+ @activities = PublicActivity::Activity.all
29
+ end
30
+ And in your views:
31
+ <% for activity in @activities %>
32
+ <%= activity.text %><br/>
33
+ <% end %>
34
+ The only thing left is to add translations to your locale files, for example:
35
+ en:
36
+ activity:
37
+ article:
38
+ create: 'Article has been created'
39
+ update: 'Someone has edited the article'
40
+
41
+ This is only a basic example, refer to documentation for more options and customization!
42
+
43
+ ## License
44
+ Copyright (c) 2011 Piotrek Okoński, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake'
2
+ require 'yard'
3
+ require 'yard/rake/yardoc_task'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.pattern = "./spec/*_spec.rb"
8
+ end
9
+
10
+ desc 'Generate documentation for the public_activity plugin.'
11
+ YARD::Rake::YardocTask.new do |doc|
12
+ doc.files = ['lib/**/*.rb']
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module PublicActivity
4
+ module Generators
5
+ module Base
6
+ def source_root
7
+ @_public_activity_source_root ||= File.expand_path(File.join('../public_activity', generator_name, 'templates'), __FILE__)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'generators/public_activity'
2
+ require 'rails/generators/active_record'
3
+
4
+ module PublicActivity
5
+ module Generators
6
+ class MigrationGenerator < ActiveRecord::Generators::Base
7
+ extend Base
8
+
9
+ argument :name, :type => :string, :default => 'create_activities'
10
+
11
+ def generate_files
12
+ migration_template 'migration.rb', "db/migrate/#{name}"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ class CreateActivities < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :activities do |t|
4
+ t.belongs_to :trackable, :polymorphic => true
5
+ t.belongs_to :owner, :polymorphic => true
6
+ t.string :key
7
+ t.text :parameters
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :activities
15
+ end
16
+ end
@@ -0,0 +1,101 @@
1
+ require 'rails'
2
+ require 'active_support/dependencies'
3
+ require 'active_record'
4
+ # +public_activity+ keeps track of changes made to models
5
+ # and allows for easy displaying of them.
6
+ #
7
+ # Basic usage requires adding one line to your models:
8
+ #
9
+ # class Article < ActiveRecord::Base
10
+ # tracked
11
+ # end
12
+ #
13
+ # And creating a table for activities, by doing this:
14
+ # rails generate public_activity:migration
15
+ # rake db:migrate
16
+ #
17
+ # Now when saved, public_activity will create
18
+ # an Activity record containing information about that changed/created
19
+ # model.
20
+ # == Displaying Activities:
21
+ #
22
+ # Minimal example would be:
23
+ #
24
+ # <% for activity in PublicActivity::Activity.all %>
25
+ # <%= activity.text %><br/>
26
+ # <% end %>
27
+ # Now you will need to add translations in your locale .yml, for the example
28
+ # provided above that would be:
29
+ # en:
30
+ # activity:
31
+ # create: 'New article has been created'
32
+ # update: 'Someone modified the article'
33
+ #
34
+ # Check {PublicActivity::ClassMethods#tracked} for more details about customizing and specifing
35
+ # ownership to users.
36
+ module PublicActivity
37
+ extend ActiveSupport::Concern
38
+ extend ActiveSupport::Autoload
39
+ autoload :Activity
40
+ autoload :Tracked
41
+ autoload :Creation
42
+ autoload :VERSION
43
+ autoload :Common
44
+
45
+ included do
46
+ class_attribute :activity_owner_global, :activity_params_global
47
+ self.activity_owner_global = nil
48
+ self.activity_params_global = {}
49
+ include Tracked
50
+ end
51
+
52
+ module ClassMethods
53
+ # Adds required callbacks for creating and updating
54
+ # tracked models and adds +activities+ relation for listing
55
+ # associated activities.
56
+ #
57
+ # == Parameters:
58
+ # :owner::
59
+ # You can pass a Symbol or a String with an attribute from
60
+ # which public_activity should take +user id+ responsible for
61
+ # this activity.
62
+ #
63
+ # For example:
64
+ # tracked :owner => :author
65
+ # will take +owner+ from tracked model's +author+ attribute.
66
+ #
67
+ # If you need more complex logic, you can pass a Proc:
68
+ # tracked :owner => Proc.new{ User.first }
69
+ # :params::
70
+ # Accepts a Hash containing parameters you wish
71
+ # to pass to every {Activity} created from this model.
72
+ #
73
+ # For example, if you want to pass a parameter that
74
+ # should be in every {Activity}, you can do this:
75
+ # tracked :params => {:user_name => "Piotrek"}
76
+ # These params are passed to i18n.translate
77
+ # when using {PublicActivity::Activity#text}, which returns
78
+ # already translated {Activity} message.
79
+ # For more dynamic settings refer to {Activity} model
80
+ # documentation.
81
+ def tracked(options = {})
82
+ return if tracked?
83
+ include Creation
84
+ include Common
85
+
86
+ if options[:owner]
87
+ self.activity_owner_global = options[:owner]
88
+ end
89
+ if options[:params]
90
+ self.activity_params_global = options[:params]
91
+ end
92
+ has_many :activities, :class_name => "PublicActivity::Activity", :as => :trackable
93
+
94
+
95
+
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ ActiveRecord::Base.send :include, PublicActivity
@@ -0,0 +1,42 @@
1
+ require 'active_record'
2
+ require 'i18n'
3
+ module PublicActivity
4
+ # The ActiveRecord model containing
5
+ # details about recorded activity.
6
+ class Activity < ActiveRecord::Base
7
+ # Define polymorphic association to the parent
8
+ belongs_to :trackable, :polymorphic => true
9
+ # Define ownership to a resource responsible for this activity
10
+ belongs_to :owner, :polymorphic => true
11
+ # Serialize parameters Hash
12
+ serialize :parameters, Hash
13
+
14
+ # Virtual attribute returning already
15
+ # translated key with params passed
16
+ # to i18n.translate function
17
+ # == Example:
18
+ #
19
+ # Let's say you want to show article's title inside Activity message.
20
+ #
21
+ # #config/locales/en.yml
22
+ # en:
23
+ # activity:
24
+ # article:
25
+ # create: "Someone has created an article '%{title}'"
26
+ # update: "Article '%{title}' has been modified"
27
+ # And in controller:
28
+ #
29
+ # def create
30
+ # @article = Article.new
31
+ # @article.title = "Rails 3.0.5 released!"
32
+ # @article.activity_params = {:title => @article.title}
33
+ # @article.save
34
+ # end
35
+ #
36
+ # Now when you list articles, you should see:
37
+ # @article.activities.last.text #=> "Someone has created an article 'Rails 3.0.5 released!'"
38
+ def text
39
+ I18n.t(key, parameters || {})
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ module PublicActivity
2
+ module Common
3
+ extend ActiveSupport::Concern
4
+
5
+ module InstanceMethods
6
+ # Prepare settings used during creation of Activity record.
7
+ # params passed directly to tracked model have priority over
8
+ # settings specified in tracked() method
9
+ def prepare_settings
10
+ # user responsible for the activity
11
+ if self.activity_owner
12
+ owner = self.activity_owner
13
+ else
14
+ case self.activity_owner_global
15
+ when Symbol, String
16
+ owner = self[self.class.activity_owner_global]
17
+ when Proc
18
+ owner = self.class.activity_owner_global.call
19
+ end
20
+ end
21
+ #customizable parameters
22
+ parameters = self.class.activity_params_global
23
+ parameters.merge! self.activity_params if self.activity_params
24
+ return {:owner => owner, :parameters => parameters}
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ module PublicActivity
2
+ module Creation
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ after_create :activity_on_create
7
+ after_update :activity_on_update
8
+ end
9
+
10
+ module InstanceMethods
11
+ private
12
+ # Creates activity based on supplied arguments
13
+ def create_activity(key, owner, params)
14
+ self.activities.create(:key => key, :owner => owner, :parameters => params)
15
+ end
16
+
17
+ # Creates activity upon creation of the tracked model
18
+ def activity_on_create
19
+ settings = prepare_settings
20
+ create_activity("activity."+self.class.name.downcase+".create", settings[:owner], settings[:parameters])
21
+ end
22
+
23
+ # Creates activity upon modification of the tracked model
24
+ def activity_on_update
25
+ settings = prepare_settings
26
+ create_activity("activity."+self.class.name.downcase+".update", settings[:owner], settings[:parameters])
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,84 @@
1
+ module PublicActivity
2
+ # Add a flag to determine whether a model class is being tracked
3
+ module Tracked
4
+ extend ActiveSupport::Concern
5
+
6
+ # Set or get parameters that will be passed to {Activity} when saving
7
+ #
8
+ # == Usage:
9
+ # In model:
10
+ #
11
+ # class Article < ActiveRecord::Base
12
+ # tracked
13
+ # end
14
+ #
15
+ # In controller
16
+ # @article = Article.new
17
+ # @article.activity_params = {:article_title => @article.title}
18
+ # @article.save
19
+ # This way you can pass strings that should remain constant, even when {Article}
20
+ # changes after creating this {Activity}.
21
+ attr_accessor :activity_params
22
+ @activity_params = {}
23
+ # Set or get user id responsible for the {Activity}.
24
+ # Same rules apply like with the other attributes described.
25
+ #
26
+ # == Usage:
27
+ # In model:
28
+ #
29
+ # class Article < ActiveRecord::Base
30
+ # tracked
31
+ # end
32
+ # Controller:
33
+ #
34
+ # @article = Article.new
35
+ # @article.activity_owner = current_user #where current_user is an object of logged in user
36
+ # @article.save
37
+ # @article.activities.last.user #=> Returns User object
38
+ attr_accessor :activity_owner
39
+ @activity_owner = nil
40
+ # Set or get custom i18n key passed to {Activity}
41
+ #
42
+ # == Usage:
43
+ # In model:
44
+ #
45
+ # class Article < ActiveRecord::Base
46
+ # tracked
47
+ # end
48
+ #
49
+ # In controller:
50
+ #
51
+ # @article = Article.new
52
+ # @article.save
53
+ # @article.activities.last.key #=> "activity.article.create"
54
+ # By default, key looks like "activity.{class_name}.{create|update}"
55
+ #
56
+ # You can customize it, by setting your own key:
57
+ # @article = Article.new
58
+ # @article.activity_key = "my.custom.article.key"
59
+ # @article.save
60
+ # @article.activities.last.key #=> "my.custom.article.key"
61
+ attr_accessor :activity_key
62
+ @activity_key = nil
63
+ # Overrides the +tracked+ method to first define the +tracked?+ class method before
64
+ # deferring to the original +tracked+.
65
+ module ClassMethods
66
+ def tracked(*args)
67
+ super(*args)
68
+
69
+ class << self
70
+ def tracked?
71
+ true
72
+ end
73
+ end
74
+ end
75
+
76
+ # For ActiveRecord::Base models that do not call the +tracked+ method, the +tracked?+
77
+ # will return false
78
+ def tracked?
79
+ false
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module PublicActivity
2
+ VERSION = '0.1'
3
+ end
@@ -0,0 +1,9 @@
1
+ Bundler.setup(:default, :test)
2
+ require File.expand_path('lib/public_activity.rb')
3
+
4
+ Dir[File.expand_path('support/*')].each {|f| require f}
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rspec
8
+ end
9
+
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe PublicActivity, 'VERSION' do
4
+ it 'is present' do
5
+ PublicActivity::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'is frozen' do
9
+ PublicActivity::VERSION.frozen?.should be(true)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: public_activity
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors:
8
+ - "Piotrek Oko\xC5\x84ski"
9
+ - "Kuba Oko\xC5\x84ski"
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-03-07 00:00:00 +01:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: activerecord
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 3.0.0
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.0.0
37
+ type: :runtime
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: i18n
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.0
48
+ type: :runtime
49
+ version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ description: Smooth acitivity tracking for your ActiveRecord models. Provides Activity model with details about actions performed by your users, like adding comments, responding etc.
62
+ email: piotrek@okonski.org
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - lib/generators/public_activity.rb
71
+ - lib/generators/public_activity/migration/migration_generator.rb
72
+ - lib/generators/public_activity/migration/templates/migration.rb
73
+ - lib/public_activity.rb
74
+ - lib/public_activity/activity.rb
75
+ - lib/public_activity/common.rb
76
+ - lib/public_activity/creation.rb
77
+ - lib/public_activity/tracked.rb
78
+ - lib/public_activity/version.rb
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - Rakefile
82
+ - README.md
83
+ - MIT-LICENSE
84
+ - spec/spec_helper.rb
85
+ - spec/version_spec.rb
86
+ has_rdoc: true
87
+ homepage: https://github.com/okonski/public_activity
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.6.1
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Smooth acitivity tracking for ActiveRecord models
114
+ test_files:
115
+ - spec/spec_helper.rb
116
+ - spec/version_spec.rb