linkingpaths-acts_as_scribe 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Linking Paths (http://www.linkingpaths.com)
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.markdown ADDED
@@ -0,0 +1,53 @@
1
+ ActsAsScribe
2
+ ============
3
+ A simple plugin that allows to keep history records of the users activities. Common uses could be user's wall, public timeline portlets, etc...
4
+
5
+ > Scribe (From Wikipedia, the free encyclopedia):
6
+ "A scribe was traditionally a person who could read and write. This usually indicated secretarial and administrative duties such as dictation and keeping business, judicial, and history records for kings, nobles, temples, and cities."
7
+
8
+ Resources
9
+ =========
10
+
11
+ Install
12
+ -------
13
+
14
+ * Run the following command:
15
+
16
+ `script/plugin install git://github.com/linkingpaths/acts_as_scribe.git`
17
+
18
+ * Generate the tables via the given generator:
19
+
20
+ `script/generate acts_as_scribe_migration`
21
+
22
+ * And finally...
23
+
24
+ `rake db:migrate`
25
+
26
+ Usage
27
+ -----
28
+
29
+ * Make your ActiveRecord model acts as scribe.
30
+ <pre>
31
+ class Comment < ActiveRecord::Base
32
+ record_activity_of :user
33
+ end
34
+ </pre>
35
+
36
+ * If you want to record activities not related to any specific model just use `record_activities :activity` in your user model:
37
+ <pre>
38
+ class User < ActiveRecord::Base
39
+ record_activities [:featured_on_home, :logged_in, :logged_out]
40
+ end
41
+ </pre>
42
+
43
+
44
+
45
+ More
46
+ -------
47
+
48
+ [http://github.com/linkingpaths/acts\_as\_scribe](http://github.com/linkingpaths/acts_as_scribe)
49
+
50
+ [http://github.com/linkingpaths/acts\_as\_scribe/wikis](http://github.com/linkingpaths/acts_as_scribe/wikis)
51
+
52
+
53
+ Copyright (c) 2008 Linking Paths, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the acts_as_scribe plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the acts_as_scribe plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ActsAsScribe'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{acts_as_scribe}
3
+ s.version = "0.0.2"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Linking Paths"]
7
+ s.date = %q{2008-10-16}
8
+ s.description = %q{A simple plugin that allows to keep track of the users activity. Common uses could be user's wall, public timeline portlets, etc...}
9
+ s.email = ["aitor@linkingpaths.com"]
10
+ s.files = ["MIT-LICENSE", "README.markdown", "Rakefile", "acts_as_scribe.gemspec", "generators", "generators/acts_as_scribe_migration", "generators/acts_as_scribe_migration/acts_as_scribe_migration_generator.rb", "generators/acts_as_scribe_migration/templates", "generators/acts_as_scribe_migration/templates/migration.rb", "generators/acts_as_scribe_migration/USAGE", "init.rb", "lib", "lib/activity.rb", "lib/acts_as_scribe.rb", "lib/scribe.rb", "test", "test/.DS_Store", "test/factories.rb", "test/functional", "test/functional/acts_as_scribe_test.rb", "test/schema.rb", "test/test_helper.rb", "test/unit", "test/unit/activity_test.rb" ]
11
+ s.has_rdoc = true
12
+ s.homepage = %q{http://github.com/linkingpaths/acts_as_scribe}
13
+ s.post_install_message = %q{
14
+ For more information on acts_as_scribe, see http://github.com/linkingpaths/acts_as_scribe
15
+
16
+ }
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{acts_as_scribe}
19
+ s.rubygems_version = %q{1.2.0}
20
+ s.summary = %q{A simple plugin that allows to keep track of the users activity. Common uses could be user's wall, public timeline portlets, etc...}
21
+
22
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ The migration generator creates a migration to add the activities table to your database and takes no arguments.
3
+
4
+ Example:
5
+ ./script/generate acts_as_scribe_migration
6
+
7
+ Migration: db/migrate/add_activities_table.rb
8
+ The Activity model lives in the lib directory of this plugin. You can move that model into your models directory and modify it if you like.
@@ -0,0 +1,13 @@
1
+ class ActsAsScribeMigrationGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.migration_template 'migration.rb', 'db/migrate'
6
+ end
7
+ end
8
+
9
+ def file_name
10
+ "acts_as_scribe_migration"
11
+ end
12
+
13
+ end
@@ -0,0 +1,17 @@
1
+ class ActsAsScribeMigration < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :activities do |t|
5
+ t.integer :user_id
6
+ t.string :action
7
+ t.integer :item_id
8
+ t.string :item_type
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :activities
15
+ end
16
+
17
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_scribe'
data/lib/activity.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Activity < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :item, :polymorphic => true
4
+ validates_presence_of :user_id
5
+
6
+ def self.created_by(user)
7
+ Activity.find(:all, :conditions => { :user_id => user.id})
8
+ end
9
+
10
+ def self.without_model_created_by(user)
11
+ Activity.find(:all, :conditions => { :user_id => user.id, :item_type => nil, :item_id => nil})
12
+ end
13
+
14
+ def without_model?
15
+ item.nil?
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ require 'scribe'
2
+
3
+ ActiveRecord::Base.send :include, LinkingPaths::Acts::Scribe
data/lib/scribe.rb ADDED
@@ -0,0 +1,70 @@
1
+ module LinkingPaths
2
+ module Acts #:nodoc:
3
+ module Scribe #:nodoc:
4
+
5
+ def self.included(base) # :nodoc:
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ activity_options ||= {}
11
+
12
+ def record_activity_of(actor, options = {})
13
+ include_scribe_instance_methods {
14
+ has_many :activities, :as => :item, :dependent => :destroy
15
+ after_create do |record|
16
+ unless options[:if].kind_of?(Proc) and not options[:if].call(record)
17
+ record.create_activity_from_self
18
+ end
19
+ end
20
+ }
21
+ self.activity_options.merge! :actor => actor
22
+ end
23
+
24
+ def record_activities(actions = [])
25
+ include_scribe_instance_methods {
26
+ has_many :activities
27
+ has_many :activities_without_model, :class_name => "Activity", :conditions => { :item_type => nil, :item_id => nil }
28
+ }
29
+ self.activity_options.merge! :actions => actions
30
+ end
31
+
32
+ def include_scribe_instance_methods(&block)
33
+ unless included_modules.include? InstanceMethods
34
+ yield if block_given?
35
+ class_inheritable_accessor :activity_options
36
+ self.activity_options ||= {}
37
+ include InstanceMethods
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ module InstanceMethods
44
+
45
+ def create_activity_from_self
46
+ activity = Activity.new
47
+ activity.item = self
48
+ activity.action = ActiveSupport::Inflector::underscore(self.class)
49
+ actor_id = self.send( activity_options[:actor].to_s + "_id" )
50
+ activity.user_id = actor_id
51
+ activity.save
52
+ end
53
+
54
+ def record_activity(action)
55
+ if activity_options[:actions] && activity_options[:actions].include?(action)
56
+ activity = Activity.new
57
+ activity.action = action.to_s
58
+ activity.user_id = self.id
59
+ activity.save!
60
+ else
61
+ raise "The action #{action} can't be tracked."
62
+ end
63
+ end
64
+
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+ end
data/test/factories.rb ADDED
@@ -0,0 +1,5 @@
1
+ Factory.define :user do |u|
2
+ end
3
+
4
+ Factory.define :jaiku do |u|
5
+ end
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class ActsAsScribeTest < Test::Unit::TestCase
4
+
5
+ context "The acts_as_scribe plugin" do
6
+
7
+ setup do
8
+ @margie = Factory(:user, :login => 'm.thatcher')
9
+ @jaiku = Factory(:jaiku, :content => "To solve Britain’s economic Disease with Socialism is like treating leukaemia with leeches.", :user => @margie)
10
+ @activity = @jaiku.activities.first
11
+ end
12
+
13
+ should 'include the correct methods on user and jaiku class' do
14
+ [:activities, :create_activity_from_self].each{|method|
15
+ assert @jaiku.respond_to?(method, "Jaiku should responds_to #{method}")
16
+ }
17
+ [:activities, :activities_without_model, :record_activity].each{|method|
18
+ assert @margie.respond_to?(method, "Jaiku should responds_to #{method}")
19
+ }
20
+ end
21
+ should 'create a new activity every time a jaiku is created' do
22
+ assert_equal 1, @jaiku.activities.size, "Create a jaiku should create a new activity"
23
+ end
24
+
25
+ should 'link the activities created on a model with the user that is generating the activity' do
26
+ assert_equal @margie, @activity.user, "The activity should be registered as made by the user that create the jaiku"
27
+ end
28
+
29
+ should 'link the new activities to the correct model and id' do
30
+ assert_equal @jaiku.class.to_s, @activity.item_type, "The activity should be linked to the first jaiku"
31
+ assert_equal @jaiku.id, @activity.item_id, "The activity should be linked to the first jaiku"
32
+ end
33
+
34
+ should 'raise an exception if we try to record an activity that is not defined on the user class' do
35
+ assert_raise RuntimeError do
36
+ @margie.record_activity :not_defined_featured
37
+ end
38
+ end
39
+
40
+ should 'create an activity without model if we use the record_activity method on a user' do
41
+ @margie.record_activity :featured_on_home
42
+ assert_equal 1, @margie.activities_without_model.size, "Track an unlinked activity should create a new activity on the user model"
43
+ end
44
+
45
+ end
46
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,17 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+ create_table :jaikus do |t|
3
+ t.string :content
4
+ t.integer :user_id
5
+ t.timestamps
6
+ end
7
+ create_table :activities do |t|
8
+ t.integer :user_id
9
+ t.string :action
10
+ t.integer :item_id
11
+ t.string :item_type
12
+ t.timestamps
13
+ end
14
+ create_table :users do |t|
15
+ t.string :login
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+
4
+ gem 'activerecord'
5
+ require 'active_record'
6
+
7
+ begin require 'redgreen'; rescue LoadError; end
8
+
9
+ begin
10
+ require 'shoulda'
11
+ require 'shoulda/active_record'
12
+ rescue LoadError => load_error
13
+ $stderr.puts
14
+ $stderr.puts "You need shoulda to run acts_as_scribe's tests. `gem install thoughtbot-shoulda` and try again."
15
+ $stderr.puts
16
+ exit
17
+ end
18
+
19
+ require 'factory_girl'
20
+ require File.expand_path(File.dirname(__FILE__) + '/factories')
21
+
22
+ require File.dirname(__FILE__) + "/../init"
23
+
24
+
25
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
26
+ load(File.dirname(__FILE__) + "/schema.rb")
27
+
28
+ class Jaiku < ActiveRecord::Base
29
+ belongs_to :user
30
+ record_activity_of :user
31
+ end
32
+
33
+ class User < ActiveRecord::Base
34
+ has_many :jaikus
35
+ record_activities [:featured_on_home, :logged_in, :logged_out]
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class ActivityTest < Test::Unit::TestCase
4
+
5
+ should_belong_to :user
6
+ should_belong_to :item
7
+
8
+ context 'An Activity model' do
9
+
10
+ setup do
11
+ @margie = Factory(:user, :login => 'm.thatcher')
12
+ @britain = Factory(:jaiku, :content => "To solve Britain’s economic Disease with Socialism is like treating leukaemia with leeches.", :user => @margie)
13
+ @margie_activities = @britain.activities
14
+
15
+ @reagan = Factory(:user, :login => 'r.reagan')
16
+ @reagan.record_activity :logged_in
17
+ end
18
+
19
+ context 'at class level' do
20
+ should 'have a `created_by` method to allow us to find activities created by a given user' do
21
+ assert Activity.respond_to?:created_by
22
+ assert_same_elements @margie_activities, Activity.created_by(@margie)
23
+ end
24
+ should 'have a `without_model_created_by` method to allow us to find activities created by a given user and not related to any item' do
25
+ assert Activity.respond_to?:without_model_created_by
26
+ assert_same_elements @reagan.activities, Activity.without_model_created_by(@reagan)
27
+ end
28
+ end
29
+ context 'at instance level' do
30
+ should 'be linked to the correct item' do
31
+ assert_equal @britain, @britain.activities.first.item
32
+ end
33
+ should 'not be linked to any item is the activity has been generated without a model' do
34
+ assert_nil @reagan.activities.first.item
35
+ assert @reagan.activities.first.without_model?
36
+ end
37
+ end
38
+
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkingpaths-acts_as_scribe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Linking Paths
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple plugin that allows to keep track of the users activity. Common uses could be user's wall, public timeline portlets, etc...
17
+ email:
18
+ - aitor@linkingpaths.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - MIT-LICENSE
27
+ - README.markdown
28
+ - Rakefile
29
+ - acts_as_scribe.gemspec
30
+ - generators
31
+ - generators/acts_as_scribe_migration
32
+ - generators/acts_as_scribe_migration/acts_as_scribe_migration_generator.rb
33
+ - generators/acts_as_scribe_migration/templates
34
+ - generators/acts_as_scribe_migration/templates/migration.rb
35
+ - generators/acts_as_scribe_migration/USAGE
36
+ - init.rb
37
+ - lib
38
+ - lib/activity.rb
39
+ - lib/acts_as_scribe.rb
40
+ - lib/scribe.rb
41
+ - test
42
+ - test/.DS_Store
43
+ - test/factories.rb
44
+ - test/functional
45
+ - test/functional/acts_as_scribe_test.rb
46
+ - test/schema.rb
47
+ - test/test_helper.rb
48
+ - test/unit
49
+ - test/unit/activity_test.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/linkingpaths/acts_as_scribe
52
+ post_install_message: |+
53
+
54
+ For more information on acts_as_scribe, see http://github.com/linkingpaths/acts_as_scribe
55
+
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: acts_as_scribe
75
+ rubygems_version: 1.2.0
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: A simple plugin that allows to keep track of the users activity. Common uses could be user's wall, public timeline portlets, etc...
79
+ test_files: []
80
+