activist 0.0.3 → 0.0.4

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/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm 1.9.2
2
+ rvm gemset use activist
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activist (0.0.1)
4
+ activist (0.0.4)
5
5
  activesupport (~> 3.0)
6
6
  redis (~> 2.2)
7
7
  redis-namespace (= 0.10.0)
@@ -4,6 +4,10 @@ Activity stream in rails 3.0 applications
4
4
 
5
5
  Based on http://activitystrea.ms/spec/1.0/atom-activity-01.html
6
6
 
7
+ ## Requirements ##
8
+
9
+ * Redis
10
+
7
11
  ## Installation ##
8
12
 
9
13
  Just run the following command :
@@ -20,17 +24,17 @@ And run
20
24
 
21
25
  ## Usage ##
22
26
 
23
- First generate a friendship model :
27
+ First generate a status model :
24
28
 
25
29
  rails generate activist:install
26
30
 
27
- This command creates a new model called __Action__ in *'app/models'* :
31
+ This command creates a new model called __ActivistStatus__ in *'app/models'* :
28
32
 
29
- class Status < ActiveRecord::Base
33
+ class ActivistStatus < ActiveRecord::Base
30
34
  serialize :data, Hash
31
35
  end
32
36
 
33
- It also creates a new migration for the Status model so don't forget to migrate your database :
37
+ It also creates a new migration for the __ActivistStatus__ model so don't forget to migrate your database :
34
38
 
35
39
  rake db:migrate
36
40
 
@@ -47,3 +51,24 @@ Then add __activist__ in your user model :
47
51
  [user]
48
52
  end
49
53
  end
54
+
55
+ And finally create some activities :
56
+
57
+ class Post < ActiveRecord::Base
58
+ include Activist::Action
59
+
60
+ belongs_to :user
61
+
62
+ activities :public, :on => :after_create
63
+ activities :private, :actor => :user, :action => :posted, :on => :after_create
64
+ activities :public, :on => :after_update, :data => proc { |post| post.changes }
65
+
66
+ after_destroy
67
+ activity :private, :action => :deleted
68
+ end
69
+
70
+ def promote
71
+ self.title = "[PROMOTED] #{self.title}"
72
+ activity :private, :action => :promoted, :data => proc { |post| { :title => post.title } }
73
+ end
74
+ end
@@ -1,33 +1,33 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "activist/version"
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'activist/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "activist"
6
+ s.name = 'activist'
7
7
  s.version = Activist::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Adrian Dulić"]
10
- s.email = ["adulic@gmail.com"]
11
- s.homepage = "http://github.com/adriandulic/activist"
12
- s.summary = %q{Activity stream in rails 3.0 application}
13
- s.description = %q{Activity stream in rails 3.0 application}
14
-
15
- s.extra_rdoc_files = ["README.markdown"]
16
-
17
- s.required_rubygems_version = ">= 1.3.6"
9
+ s.authors = ['Adrian Dulić']
10
+ s.email = ['adulic@gmail.com']
11
+ s.homepage = 'http://github.com/adriandulic/activist'
12
+ s.summary = %q{Redis and database based activity stream in Rails 3 applications}
13
+ s.description = %q{Redis and database based activity stream in Rails 3 applications}
14
+
15
+ s.extra_rdoc_files = ['README.markdown']
16
+
17
+ s.required_rubygems_version = '>= 1.3.6'
18
18
  s.rubyforge_project = s.name
19
-
20
- s.add_dependency "redis", "~> 2.2"
21
- s.add_dependency "redis-namespace", "0.10.0"
22
- s.add_dependency "activesupport", "~> 3.0"
23
-
24
- s.add_development_dependency "bundler", "~> 1.0"
25
- s.add_development_dependency "rspec", "~> 2.0"
26
- s.add_development_dependency "activerecord", "~> 3.0"
27
- s.add_development_dependency "sqlite3-ruby", "~> 1.3.2"
28
-
19
+
20
+ s.add_dependency 'redis', '~> 2.2'
21
+ s.add_dependency 'redis-namespace', '0.10.0'
22
+ s.add_dependency 'activesupport', '~> 3.0'
23
+
24
+ s.add_development_dependency 'bundler', '~> 1.0'
25
+ s.add_development_dependency 'rspec', '~> 2.0'
26
+ s.add_development_dependency 'activerecord', '~> 3.0'
27
+ s.add_development_dependency 'sqlite3-ruby', '~> 1.3.2'
28
+
29
29
  s.files = `git ls-files`.split("\n")
30
30
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
31
31
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
32
- s.require_paths = ["lib"]
32
+ s.require_paths = ['lib']
33
33
  end
@@ -3,9 +3,9 @@ require 'redis/namespace'
3
3
  require 'redis'
4
4
 
5
5
  module Activist
6
+ autoload :Action, 'activist/action'
7
+ autoload :Actor, 'activist/actor'
6
8
  autoload :Redis, 'activist/redis'
7
9
  autoload :Store, 'activist/store'
8
- autoload :Actor, 'activist/actor'
9
- autoload :Action, 'activist/action'
10
10
  autoload :Stream, 'activist/stream'
11
11
  end
@@ -6,7 +6,7 @@ module Activist
6
6
  module ClassMethods
7
7
  def activities(stream, options = {})
8
8
  options.symbolize_keys!
9
- callback = options[:when]
9
+ callback = options[:on] || ''
10
10
 
11
11
  define_method "activist_#{stream}_#{callback}" do
12
12
  store_activities(stream, options)
@@ -1,8 +1,6 @@
1
1
  module Activist
2
2
  module Actor
3
- def self.included(receiver)
4
- receiver.extend(ClassMethods)
5
- end
3
+ extend ActiveSupport::Concern
6
4
 
7
5
  module ClassMethods
8
6
  def stream(stream, &block)
@@ -3,9 +3,38 @@ module Activist
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module InstanceMethods
6
+ # Options:
7
+ # :actor (optional)
8
+ # :action (optional)
9
+ # :on (required)
10
+ # :data (optional)
11
+ # :if (optional)
12
+ # :unless (optional)
6
13
  def store_activities(stream, options = {})
7
14
  options.symbolize_keys!
8
- callback = options.delete(:when)
15
+
16
+ callback = options.delete(:on)
17
+
18
+ if_condition = case options[:if]
19
+ when Symbol
20
+ send(options[:if])
21
+ when Proc
22
+ options[:if].call(self)
23
+ else
24
+ true
25
+ end
26
+ return unless if_condition
27
+
28
+ unless_condition = case options[:unless]
29
+ when Symbol
30
+ send(options[:unless])
31
+ when Proc
32
+ options[:unless].call(self)
33
+ else
34
+ false
35
+ end
36
+ return if unless_condition
37
+
9
38
  actor = case options[:actor]
10
39
  when nil
11
40
  send(:user)
@@ -14,8 +43,9 @@ module Activist
14
43
  when Proc
15
44
  options[:actor].call(self)
16
45
  else
17
- options[:actor]
46
+ nil
18
47
  end
48
+
19
49
  action = if options[:action].nil?
20
50
  case callback
21
51
  when :before_create, :after_create
@@ -27,14 +57,22 @@ module Activist
27
57
  when :before_destroy, :after_destroy
28
58
  options[:action] = :destroyed
29
59
  else
30
- options[:action] = :undefined
60
+ nil
31
61
  end
32
62
  else
33
63
  options[:action]
34
64
  end
35
- data = options[:data]
36
65
 
37
- status = ::Status.create! do |s|
66
+ data = case options[:data]
67
+ when Hash
68
+ options[:data]
69
+ when Proc
70
+ options[:data].call(self)
71
+ else
72
+ nil
73
+ end
74
+
75
+ status = ::ActivistStatus.create! do |s|
38
76
  s.actor_class = actor.class.to_s
39
77
  s.actor_id = actor.id
40
78
  s.action = action
@@ -15,8 +15,10 @@ module Activist
15
15
  end
16
16
 
17
17
  def all(options={})
18
- activities = Redis.execute.lrange(to_key, options[:offset] || 0, options[:limit] || size)
19
- ::Status.where(:id => activities)
18
+ options[:offset] ||= 0
19
+ options[:limit] ||= size.to_i
20
+ activities = Redis.execute.lrange(to_key, options[:offset], options[:offset] + options[:limit])
21
+ ::ActivistStatus.where(:id => activities)
20
22
  end
21
23
 
22
24
  def size
@@ -28,7 +30,7 @@ module Activist
28
30
  end
29
31
 
30
32
  def to_key
31
- "#{@actor.class}.#{@actor.id}:#{@name}"
33
+ "#{actor.class}.#{actor.id}:#{name}"
32
34
  end
33
35
  end
34
36
  end
@@ -1,3 +1,3 @@
1
1
  module Activist
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
@@ -5,19 +5,19 @@ module Activist
5
5
  module Generators
6
6
  class InstallGenerator < Rails::Generators::Base
7
7
  include Rails::Generators::Migration
8
-
8
+
9
9
  def self.source_root
10
10
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
11
11
  end
12
-
12
+
13
13
  def self.next_migration_number
14
- Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
15
15
  end
16
-
17
- desc "This generator creates a status model and its migration file"
16
+
17
+ desc 'This generator creates a status model and its migration file'
18
18
  def create_friendship_model_files
19
- template 'status.rb', 'app/models/status.rb'
20
- template 'create_statuses.rb', "db/migrate/#{self.class.next_migration_number}_create_statuses.rb"
19
+ template 'activist_status.rb', 'app/models/activist_status.rb'
20
+ template 'create_activist_statuses.rb', "db/migrate/#{self.class.next_migration_number}_create_activist_statuses.rb"
21
21
  end
22
22
  end
23
23
  end
@@ -0,0 +1,3 @@
1
+ class ActivistStatus < ActiveRecord::Base
2
+ serialize :data, Hash
3
+ end
@@ -1,6 +1,6 @@
1
- class CreateStatuses < ActiveRecord::Migration
1
+ class CreateActivistStatuses < ActiveRecord::Migration
2
2
  def self.up
3
- create_table :statuses do |t|
3
+ create_table :activist_statuses do |t|
4
4
  t.string :actor_class
5
5
  t.integer :actor_id
6
6
  t.string :action
@@ -11,8 +11,8 @@ class CreateStatuses < ActiveRecord::Migration
11
11
  t.timestamps
12
12
  end
13
13
  end
14
-
14
+
15
15
  def self.down
16
- drop_table :statuses
16
+ drop_table :activist_statuses
17
17
  end
18
18
  end
@@ -1,23 +1,44 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Activist do
4
- before do
5
- @region = Region.create!(:name => "Mazovia")
6
- @kate = User.create!(:name => "Kate", :regions => Region.all)
7
- @john = User.create!(:name => "John", :regions => Region.all)
4
+ before :all do
5
+ @region = Region.create!(:name => 'Mazovia')
6
+ @kate = User.create!(:name => 'Kate', :regions => Region.all)
7
+ @john = User.create!(:name => 'John', :regions => Region.all)
8
+ @note = Note.create!(:note => 'Note by Kate', :user => @kate)
8
9
  end
9
10
 
10
- context "actor" do
11
- it "should create streams for actor" do
11
+ context 'actor' do
12
+ it 'should create streams for actor' do
12
13
  @kate.public_stream.should be_kind_of Activist::Stream
13
14
  @kate.regional_stream.should be_kind_of Activist::Stream
14
15
  @kate.private_stream.should be_kind_of Activist::Stream
15
16
  end
16
17
  end
17
18
 
18
- context "action" do
19
+ context 'action' do
20
+ it 'should define activist methods' do
21
+ @note.public_methods.should include(:activist_regional_after_create)
22
+ @note.public_methods.should include(:activist_private_after_create)
23
+ end
24
+
25
+ it 'should define activist class methods' do
26
+ @note.class.public_methods.should include(:activities)
27
+ end
28
+
29
+ it 'should define activist instance methods' do
30
+ @note.public_methods.should include(:activity)
31
+ end
32
+
33
+ it 'should not pass' do
34
+ size = @kate.regional_stream.size
35
+ @empty = Note.create!(:note => '', :user => @kate)
36
+ @kate.regional_stream.size.should == size
37
+ @present = Note.create!(:note => 'Some note', :user => @kate)
38
+ @kate.regional_stream.size.should == size + 1
39
+ end
19
40
  end
20
41
 
21
- context "stream" do
42
+ context 'stream' do
22
43
  end
23
44
  end
@@ -1,13 +1,13 @@
1
1
  require 'active_record'
2
2
 
3
3
  ActiveRecord::Base.establish_connection(
4
- :adapter => "sqlite3",
5
- :database => ":memory:"
4
+ :adapter => 'sqlite3',
5
+ :database => ':memory:'
6
6
  )
7
7
  ActiveRecord::Migration.verbose = false
8
8
 
9
9
  ActiveRecord::Schema.define do
10
- create_table :statuses, :force => true do |t|
10
+ create_table :activist_statuses, :force => true do |t|
11
11
  t.string :actor_class
12
12
  t.integer :actor_id
13
13
  t.string :action
@@ -22,9 +22,11 @@ ActiveRecord::Schema.define do
22
22
  t.string :name
23
23
  end
24
24
 
25
- create_table :regions_users, :id => false, :force => true do |t|
25
+ create_table :user_regions, :force => true do |t|
26
26
  t.integer :user_id
27
27
  t.integer :region_id
28
+ t.datetime :created_at
29
+ t.datetime :updated_at
28
30
  end
29
31
 
30
32
  create_table :users, :force => true do |t|
@@ -40,18 +42,26 @@ end
40
42
  REDIS = Redis.new(:host => '127.0.0.1', :port => 6379)
41
43
  Activist::Redis.execute.flushall
42
44
 
43
- class Status < ActiveRecord::Base
45
+ class ActivistStatus < ActiveRecord::Base
44
46
  serialize :data, Hash
45
47
  end
46
48
 
47
49
  class Region < ActiveRecord::Base
48
- has_and_belongs_to_many :users
50
+ has_many :user_regions
51
+ has_many :users, :through => :user_regions
52
+ end
53
+
54
+ class UserRegion < ActiveRecord::Base
55
+ belongs_to :user
56
+ belongs_to :region
49
57
  end
50
58
 
51
59
  class User < ActiveRecord::Base
52
60
  include Activist::Actor
61
+
53
62
  has_many :notes
54
- has_and_belongs_to_many :regions
63
+ has_many :user_regions
64
+ has_many :regions, :through => :user_regions
55
65
 
56
66
  stream :public do
57
67
  all
@@ -68,10 +78,11 @@ end
68
78
 
69
79
  class Note < ActiveRecord::Base
70
80
  include Activist::Action
81
+
71
82
  belongs_to :user
72
83
 
73
- activities :regional, :when => :after_create
74
- activities :private, :when => :after_create
84
+ activities :private, :on => :after_create
85
+ activities :regional, :on => :after_create, :if => proc { |note| note.note.present? }
75
86
 
76
87
  after_destroy do |note|
77
88
  activity :private, :action => :deleted
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: activist
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Adrian Duli\xC4\x87"
@@ -10,7 +10,8 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-29 00:00:00 Z
13
+ date: 2011-06-27 00:00:00 +02:00
14
+ default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: redis
@@ -89,7 +90,7 @@ dependencies:
89
90
  version: 1.3.2
90
91
  type: :development
91
92
  version_requirements: *id007
92
- description: Activity stream in rails 3.0 application
93
+ description: Redis and database based activity stream in Rails 3 applications
93
94
  email:
94
95
  - adulic@gmail.com
95
96
  executables: []
@@ -100,6 +101,7 @@ extra_rdoc_files:
100
101
  - README.markdown
101
102
  files:
102
103
  - .gitignore
104
+ - .rvmrc
103
105
  - Gemfile
104
106
  - Gemfile.lock
105
107
  - LICENCE
@@ -114,11 +116,12 @@ files:
114
116
  - lib/activist/stream.rb
115
117
  - lib/activist/version.rb
116
118
  - lib/generators/activist/install/install_generator.rb
117
- - lib/generators/activist/install/templates/create_statuses.rb
118
- - lib/generators/activist/install/templates/status.rb
119
+ - lib/generators/activist/install/templates/activist_status.rb
120
+ - lib/generators/activist/install/templates/create_activist_statuses.rb
119
121
  - spec/activist_spec.rb
120
122
  - spec/setup.rb
121
123
  - spec/spec_helper.rb
124
+ has_rdoc: true
122
125
  homepage: http://github.com/adriandulic/activist
123
126
  licenses: []
124
127
 
@@ -142,10 +145,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
145
  requirements: []
143
146
 
144
147
  rubyforge_project: activist
145
- rubygems_version: 1.7.2
148
+ rubygems_version: 1.6.2
146
149
  signing_key:
147
150
  specification_version: 3
148
- summary: Activity stream in rails 3.0 application
151
+ summary: Redis and database based activity stream in Rails 3 applications
149
152
  test_files:
150
153
  - spec/activist_spec.rb
151
154
  - spec/setup.rb
@@ -1,3 +0,0 @@
1
- class Status < ActiveRecord::Base
2
- serialize :data, Hash
3
- end