acts_as_subscribable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/README.md +88 -0
- data/Rakefile +5 -0
- data/acts_as_subscribable.gemspec +29 -0
- data/lib/.DS_Store +0 -0
- data/lib/acts_as_subscribable.rb +6 -0
- data/lib/acts_as_subscribable/railtie.rb +9 -0
- data/lib/acts_as_subscribable/subscribable_methods.rb +29 -0
- data/lib/acts_as_subscribable/subscribable_methods/instance_methods.rb +21 -0
- data/lib/acts_as_subscribable/version.rb +3 -0
- data/lib/generators/.DS_Store +0 -0
- data/lib/generators/acts_as_subscribable/.DS_Store +0 -0
- data/lib/generators/acts_as_subscribable/USAGE +9 -0
- data/lib/generators/acts_as_subscribable/acts_as_subscribable_generator.rb +9 -0
- metadata +64 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# acts_as_subscribable
|
2
|
+
|
3
|
+
acts_as_subscribable is a small Rubygem that provides a high-level wrapper around `ActiveRecord::Base` to allow a model to 'subscribe' to another given model. This could be a `User` subscribing to a `Task` for example, or a `Member` subscribing to a `Thread` or even a `Watcher` subscribing to a `Channel`.
|
4
|
+
|
5
|
+
It's essentially a glorified version of a `has_many :through` association, but it adds a few methods to make the lookups and searching a little easier and more readable.
|
6
|
+
|
7
|
+
This is my first proper Rubygem so you'll have to bear with me on this one. If you've any suggestions of ways to improve the code, please feel free to contribute.
|
8
|
+
|
9
|
+
**[acts_as_subscribable was kindly funded by the marvellous Mission Control task management.](http://missioncontrolhq.com)**
|
10
|
+
|
11
|
+
## Synopsis
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class Todo < ActiveRecord::Base
|
15
|
+
acts_as_subscribable
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
## Setup
|
20
|
+
|
21
|
+
Add it to your Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'acts_as_subscribable'
|
25
|
+
```
|
26
|
+
|
27
|
+
..and then `bundle install`. Once that's installed, we can run the built-in generator to generate our `Subscription` model, with an optional model name, subscribed foreign key and subscriber foreign key as parameters:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
$ rails g acts_as_subscribable Subscription
|
31
|
+
```
|
32
|
+
|
33
|
+
This will generate our Subscription model with the appropriate migration. Run your migrations and add the `acts_as_subscribable` call to your model that you wish to be subscribable:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class Todo < ActiveRecord::Base
|
37
|
+
acts_as_subscribable
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
We now have our wrapper in place. We use a bunch of sane defaults; the subscriber model defaults to `User`, so we'll be subscribing `User`s to `Todo`s.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
> t = Todo.first
|
47
|
+
> u = User.first
|
48
|
+
|
49
|
+
> t.subscribe u
|
50
|
+
|
51
|
+
> t.subscribed? u
|
52
|
+
=> true
|
53
|
+
|
54
|
+
> t.subscribers
|
55
|
+
=> [ #<User id: 1 ...> ]
|
56
|
+
|
57
|
+
> t.unsubscribe u
|
58
|
+
|
59
|
+
> t.subscribed? u
|
60
|
+
=> false
|
61
|
+
```
|
62
|
+
|
63
|
+
## Where are the specs?
|
64
|
+
|
65
|
+
I started writing a test suite, I promise! Just got too busy, and the metaprogramming stuff was too fiddly to test.
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
Copyright (c) 2011 Jamie Rumbelow, http://jamierumbelow.net
|
70
|
+
|
71
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
72
|
+
a copy of this software and associated documentation files (the
|
73
|
+
"Software"), to deal in the Software without restriction, including
|
74
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
75
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
76
|
+
permit persons to whom the Software is furnished to do so, subject to
|
77
|
+
the following conditions:
|
78
|
+
|
79
|
+
The above copyright notice and this permission notice shall be
|
80
|
+
included in all copies or substantial portions of the Software.
|
81
|
+
|
82
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
83
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
84
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
85
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
86
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
87
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
88
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_subscribable/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_subscribable"
|
7
|
+
s.version = ActsAsSubscribable::VERSION
|
8
|
+
s.authors = ["Jamie Rumbelow"]
|
9
|
+
s.email = ["jamie@jamierumbelow.net"]
|
10
|
+
s.homepage = "https://github.com/jamierumbelow/acts_as_subscribable"
|
11
|
+
s.summary = "Simplify adding user subscriptions to models"
|
12
|
+
s.description = <<-desc
|
13
|
+
acts_as_subscribable is a loose wrapper around ActiveRecord to allow a basic user subscription model.
|
14
|
+
acts_as_subscribable exposes a high-level set of methods that allow you to easily subscribe, unsubscribe
|
15
|
+
and view subscribers to a model.
|
16
|
+
desc
|
17
|
+
|
18
|
+
s.rubyforge_project = "acts_as_subscribable"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
|
25
|
+
# specify any dependencies here; for example:
|
26
|
+
# s.add_development_dependency "rspec"
|
27
|
+
# s.add_development_dependency "mocha"
|
28
|
+
# s.add_runtime_dependency "rest-client"
|
29
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "acts_as_subscribable/subscribable_methods/instance_methods"
|
2
|
+
|
3
|
+
module ActsAsSubscribable
|
4
|
+
module SubscribableMethods
|
5
|
+
def acts_as_subscribable(options = {})
|
6
|
+
options.reverse_merge! :subscriber => "User", :subscription => "Subscription", :subscriber_id => :subscriber_id, :subscribed_id => :subscribed_id
|
7
|
+
|
8
|
+
options[:subscription] = options[:subscription].to_s
|
9
|
+
options[:subscriber] = options[:subscriber].to_s
|
10
|
+
|
11
|
+
subscriber_const = options[:subscriber].constantize
|
12
|
+
subscriber_table = options[:subscriber].tableize
|
13
|
+
subscription_const = options[:subscription].constantize
|
14
|
+
|
15
|
+
has_many :subscriptions, :class_name => options[:subscription], :dependent => :destroy
|
16
|
+
has_many subscriber_table, :class_name => options[:subscriber], :through => :subscriptions, :foreign_key => options[:subscriber_id]
|
17
|
+
|
18
|
+
subscriber_const.has_many :subscriptions, :class_name => options[:subscription], :dependent => :destroy
|
19
|
+
# subscriber_const.has_many self.name.tableize.to_sym, :class_name => self.name, :through => :subscriptions, :foreign_key => options[:subscribed_id]
|
20
|
+
|
21
|
+
subscription_const.belongs_to options[:subscriber].downcase.to_sym
|
22
|
+
subscription_const.belongs_to self.name.downcase.to_sym
|
23
|
+
|
24
|
+
send(:include, ActsAsSubscribable::SubscribableMethods::InstanceMethods)
|
25
|
+
|
26
|
+
class_eval "def acts_as_subscribable_options; { :subscriber => '#{options[:subscriber]}', :subscription => '#{options[:subscription]}' }; end"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActsAsSubscribable
|
2
|
+
module SubscribableMethods
|
3
|
+
module InstanceMethods
|
4
|
+
def subscribers
|
5
|
+
send acts_as_subscribable_options[:subscriber].tableize
|
6
|
+
end
|
7
|
+
|
8
|
+
def subscribe(subscriber)
|
9
|
+
send(acts_as_subscribable_options[:subscriber].tableize).send(:<<, subscriber) unless subscribed?(subscriber)
|
10
|
+
end
|
11
|
+
|
12
|
+
def unsubscribe(subscriber)
|
13
|
+
subscriber.subscriptions.where(:"#{acts_as_subscribable_options[:subscribed_id]}" => id).delete_all
|
14
|
+
end
|
15
|
+
|
16
|
+
def subscribed?(subscriber)
|
17
|
+
!subscriptions.where(:"#{acts_as_subscribable_options[:subscriber_id]}" => subscriber.id).empty?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
Binary file
|
Binary file
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class ActsAsSubscribableGenerator < Rails::Generators::Base
|
2
|
+
argument :subscription_name, :type => :string, :default => "Subscription"
|
3
|
+
argument :subscriber_id, :type => :string, :default => "subscriber_id"
|
4
|
+
argument :subscribed_id, :type => :string, :default => "subscribed_id"
|
5
|
+
|
6
|
+
def generate_subscriptions
|
7
|
+
generate "model #{subscription_name} #{subscriber_id}:integer #{subscribed_id}:integer"
|
8
|
+
end
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_subscribable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jamie Rumbelow
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-14 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! " acts_as_subscribable is a loose wrapper around
|
15
|
+
ActiveRecord to allow a basic user subscription model.\n acts_as_subscribable
|
16
|
+
exposes a high-level set of methods that allow you to easily subscribe, unsubscribe\n
|
17
|
+
\ and view subscribers to a model.\n"
|
18
|
+
email:
|
19
|
+
- jamie@jamierumbelow.net
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- .rspec
|
26
|
+
- Gemfile
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- acts_as_subscribable.gemspec
|
30
|
+
- lib/.DS_Store
|
31
|
+
- lib/acts_as_subscribable.rb
|
32
|
+
- lib/acts_as_subscribable/railtie.rb
|
33
|
+
- lib/acts_as_subscribable/subscribable_methods.rb
|
34
|
+
- lib/acts_as_subscribable/subscribable_methods/instance_methods.rb
|
35
|
+
- lib/acts_as_subscribable/version.rb
|
36
|
+
- lib/generators/.DS_Store
|
37
|
+
- lib/generators/acts_as_subscribable/.DS_Store
|
38
|
+
- lib/generators/acts_as_subscribable/USAGE
|
39
|
+
- lib/generators/acts_as_subscribable/acts_as_subscribable_generator.rb
|
40
|
+
homepage: https://github.com/jamierumbelow/acts_as_subscribable
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project: acts_as_subscribable
|
60
|
+
rubygems_version: 1.8.12
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Simplify adding user subscriptions to models
|
64
|
+
test_files: []
|