Permalink
Browse files

Fully converted over to a gem with README

  • Loading branch information...
1 parent 722f17a commit 9275818be46c960fad402ff12b5b502cd244b1c6 @jamierumbelow committed Dec 14, 2011
View
@@ -0,0 +1,88 @@
+# acts_as_subscribable
+
+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`.
+
+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.
+
+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.
+
+**[acts_as_subscribable was kindly funded by the marvellous Mission Control task management.](http://missioncontrolhq.com)**
+
+## Synopsis
+
+```ruby
+class Todo < ActiveRecord::Base
+ acts_as_subscribable
+end
+```
+
+## Setup
+
+Add it to your Gemfile:
+
+```ruby
+gem 'acts_as_subscribable'
+```
+
+..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:
+
+```bash
+$ rails g acts_as_subscribable Subscription
+```
+
+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:
+
+```ruby
+class Todo < ActiveRecord::Base
+ acts_as_subscribable
+end
+```
+
+## Usage
+
+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.
+
+```ruby
+ > t = Todo.first
+ > u = User.first
+
+ > t.subscribe u
+
+ > t.subscribed? u
+=> true
+
+ > t.subscribers
+=> [ #<User id: 1 ...> ]
+
+ > t.unsubscribe u
+
+ > t.subscribed? u
+=> false
+```
+
+## Where are the specs?
+
+I started writing a test suite, I promise! Just got too busy, and the metaprogramming stuff was too fiddly to test.
+
+## License
+
+Copyright (c) 2011 Jamie Rumbelow, http://jamierumbelow.net
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
Binary file not shown.
@@ -3,7 +3,7 @@
module ActsAsSubscribable
module SubscribableMethods
def acts_as_subscribable(options = {})
- options.reverse_merge! :subscriber => "User", :subscription => "Subscription"
+ options.reverse_merge! :subscriber => "User", :subscription => "Subscription", :subscriber_id => :subscriber_id, :subscribed_id => :subscribed_id
options[:subscription] = options[:subscription].to_s
options[:subscriber] = options[:subscriber].to_s
@@ -13,10 +13,10 @@ def acts_as_subscribable(options = {})
subscription_const = options[:subscription].constantize
has_many :subscriptions, :class_name => options[:subscription], :dependent => :destroy
- has_many subscriber_table, :class_name => options[:subscriber], :through => :subscriptions
+ has_many subscriber_table, :class_name => options[:subscriber], :through => :subscriptions, :foreign_key => options[:subscriber_id]
subscriber_const.has_many :subscriptions, :class_name => options[:subscription], :dependent => :destroy
- subscriber_const.has_many self.name.tableize.to_sym, :class_name => self.name, :through => :subscriptions
+ # subscriber_const.has_many self.name.tableize.to_sym, :class_name => self.name, :through => :subscriptions, :foreign_key => options[:subscribed_id]
subscription_const.belongs_to options[:subscriber].downcase.to_sym
subscription_const.belongs_to self.name.downcase.to_sym
@@ -10,11 +10,11 @@ def subscribe(subscriber)
end
def unsubscribe(subscriber)
- subscriber.subscriptions.where(:"#{self.name.downcase}_id" => id).delete_all
+ subscriber.subscriptions.where(:"#{acts_as_subscribable_options[:subscribed_id]}" => id).delete_all
end
def subscribed?(subscriber)
- !subscriptions.where(:"#{acts_as_subscribable_options[:subscriber].downcase}_id" => subscriber.id).empty?
+ !subscriptions.where(:"#{acts_as_subscribable_options[:subscriber_id]}" => subscriber.id).empty?
end
end
end
View
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,9 @@
+Description:
+ Generate a model and migration to store subscriptions
+
+Example:
+ rails generate acts_as_subscribable Subscription
+
+ This will create:
+ app/models/subscription.rb
+ db/migrations/00000000_create_subscriptions.rb
@@ -0,0 +1,9 @@
+class ActsAsSubscribableGenerator < Rails::Generators::Base
+ argument :subscription_name, :type => :string, :default => "Subscription"
+ argument :subscriber_id, :type => :string, :default => "subscriber_id"
+ argument :subscribed_id, :type => :string, :default => "subscribed_id"
+
+ def generate_subscriptions
+ generate "model #{subscription_name} #{subscriber_id}:integer #{subscribed_id}:integer"
+ end
+end

0 comments on commit 9275818

Please sign in to comment.