activity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,15 @@
1
+ Activity
2
+ =======
3
+
4
+ Activity is a rails engine that enables you to log actions by users on your models.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ class Comment < ActiveRecord::Base
11
+ actionable :user_id
12
+ # Your comment model here
13
+ end
14
+
15
+ Copyright (c) 2010 Joshua Wood (joshuawood.net), released under the MIT license
@@ -0,0 +1,17 @@
1
+ class Action < ActiveRecord::Base
2
+ set_table_name :activity
3
+
4
+ belongs_to :model, :polymorphic => true
5
+ belongs_to :actor, :polymorphic => true
6
+
7
+ scope :latest, order("`created_at` DESC, `id` DESC")
8
+ scope :limit, lambda {|lim| limit(lim) }
9
+ scope :for_model, lambda {|model| where(:model_id => model.id, :model_type => model.class.name.to_sym) }
10
+ scope :for_model_type, lambda {|type| where(:model_type => type) }
11
+
12
+ def to_s(l=nil)
13
+ haml = Haml::Engine.new(I18n.t('activity.'+self.model_type.to_s.downcase+'.'+(l.nil? ? '' : l.to_s+'.')+self.action.to_s.downcase))
14
+ haml.render( Object.new, {:actor => self.actor, :model => self.model} )
15
+ end
16
+
17
+ end
@@ -0,0 +1,70 @@
1
+ # Require Rails engine
2
+ module Activity
3
+ require 'activity/engine' if defined?(Rails)
4
+ require 'generators/activity/activity_generator'
5
+
6
+ module Actionable
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def actionable
14
+ has_many :actions, :as => :model, :dependent => :destroy, :order => 'created_at ASC'
15
+ # after_create :action_after_create
16
+ include Activity::Actionable::InstanceMethods
17
+ extend Activity::Actionable::SingletonMethods
18
+ end
19
+ end
20
+
21
+ # This module contains class methods
22
+ module SingletonMethods
23
+ end
24
+
25
+ # This module contains instance methods
26
+ module InstanceMethods
27
+ # Method to determine if the model is actionable
28
+ def actionable
29
+ true
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ module Actable
36
+
37
+ def self.included(base)
38
+ base.extend ClassMethods
39
+ end
40
+
41
+ module ClassMethods
42
+ def actable
43
+ has_many :acts, :class_name => 'Action', :as => :actor, :dependent => :destroy, :order => 'created_at ASC'
44
+ # after_create :action_after_create
45
+ include Activity::Actable::InstanceMethods
46
+ extend Activity::Actable::SingletonMethods
47
+ end
48
+ end
49
+
50
+ # This module contains class methods
51
+ module SingletonMethods
52
+ end
53
+
54
+ # This module contains instance methods
55
+ module InstanceMethods
56
+ # Method to determine if the model is actable
57
+ def actable
58
+ true
59
+ end
60
+ # Records the activity to the model
61
+ def act(action,model)
62
+ self.acts.create({:action=>action,:model_id=>model.id,:model_type=>model.class.name.to_sym})
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ ActiveRecord::Base.send(:include, Activity::Actionable)
70
+ ActiveRecord::Base.send(:include, Activity::Actable)
@@ -0,0 +1,9 @@
1
+ require "activity"
2
+ require "rails"
3
+ require "active_record"
4
+
5
+ module Activity
6
+ class Engine < Rails::Engine
7
+ engine_name :activity
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ # Activity
2
+ module Activity
3
+ module Actionable
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def actionable(user_key)
11
+ has_many :actions, :as => :model, :dependent => :destroy, :order => 'created_at ASC'
12
+ belongs_to :actioner, :class_name => 'User', :foreign_key => user_key
13
+ after_create :action_after_create
14
+ include Activity::Actionable::InstanceMethods
15
+ extend Activity::Actionable::SingletonMethods
16
+ end
17
+ end
18
+
19
+ # This module contains class methods
20
+ module SingletonMethods
21
+ end
22
+
23
+ # This module contains instance methods
24
+ module InstanceMethods
25
+ def action_after_create
26
+ self.actions.create({:action=>:create,:user_id=>self.actioner.id})
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ # lib/generators/activity/activity_generator.rb
2
+ # http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
3
+ require 'rails/generators'
4
+ require 'rails/generators/migration'
5
+
6
+ class ActivityGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+ # Implement the required interface for Rails::Generators::Migration.
12
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
13
+ def self.next_migration_number(dirname)
14
+ if ActiveRecord::Base.timestamped_migrations
15
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
16
+ else
17
+ "%.3d" % (current_migration_number(dirname) + 1)
18
+ end
19
+ end
20
+
21
+ def create_migration_file
22
+ migration_template 'migration.rb', 'db/migrate/create_activity.rb'
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ class CreateActivity < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :activity do |t|
4
+ t.integer :model_id
5
+ t.string :model_type
6
+ t.string :action
7
+ t.integer :actor_id
8
+ t.string :actor_type
9
+ t.timestamps
10
+ end
11
+ add_index :activity, :model_id, :name => "index_activity_on_model_id"
12
+ add_index :activity, :model_type, :name => "index_activity_on_model_type"
13
+ add_index :activity, :actor_id, :name => "index_activity_on_actor_id"
14
+ add_index :activity, :actor_type, :name => "index_activity_on_actor_type"
15
+ end
16
+
17
+ def self.down
18
+ drop_table :activity
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activity
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors: []
13
+
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-26 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ name: haml
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ description:
36
+ email:
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - app/models/action.rb
45
+ - lib/activity.rb
46
+ - lib/activity/engine.rb
47
+ - lib/generators/activity.rb
48
+ - lib/generators/activity/activity_generator.rb
49
+ - lib/generators/activity/templates/migration.rb
50
+ - README
51
+ has_rdoc: true
52
+ homepage:
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Logging engine for Ruby on Rails 3
85
+ test_files: []
86
+