Permalink
Browse files

Merge pull request #1 from pmukerji/master

Updated README.md to include some more specific instructions and Markdown style for Github
  • Loading branch information...
2 parents 697b10e + 087353f commit 96e0028506123a1a587c9da7dffc31b8a91748c1 @saimonmoore committed Mar 6, 2013
Showing with 62 additions and 120 deletions.
  1. +0 −118 README
  2. +60 −0 README.md
  3. +2 −2 sequel_polymorphic.gemspec
View
@@ -1,118 +0,0 @@
-Sequel Polymorphic
-==================
-
-A simple plugin for Sequel::Model's that lets you easily create polymorphic associations.
-
-ActiveRecord Style
-------------------
-
-class Asset < ActiveRecord::Base
- belongs_to :attachable, :polymorphic => true
-end
-
-class Post < ActiveRecord::Base
- has_many :assets, :as => :attachable
-end
-
-class Note < ActiveRecord::Base
- has_many :assets, :as => :attachable
-end
-
-@asset.attachable = @post
-@asset.attachable = @note
-
-In Sequel you would do the following:
-
-class Asset < Sequel::Model
- many_to_one :attachable, :reciprocal=>:assets, \
- :dataset=>(proc do
- klass = attachable_type.constantize
- klass.filter(klass.primary_key=>attachable_id)
- end), \
- :eager_loader=>(proc do |key_hash, assets, associations|
- id_map = {}
- assets.each do |asset|
- asset.associations[:attachable] = nil
- ((id_map[asset.attachable_type] ||= {})[asset.attachable_id] ||= []) << asset
- end
- id_map.each do |klass_name, id_map|
- klass = klass_name.constantize
- klass.filter(klass.primary_key=>id_map.keys).all do |attach|
- id_map[attach.pk].each do |asset|
- asset.associations[:attachable] = attach
- end
- end
- end
- end)
-
- private
-
- def _attachable=(attachable)
- self[:attachable_id] = (attachable.pk if attachable)
- self[:attachable_type] = (attachable.class.name if attachable)
- end
-end
-
-class Post < Sequel::Model
- one_to_many :assets, :key=>:attachable_id do |ds|
- ds.filter(:attachable_type=>'Post')
- end
-
- private
-
- def _add_asset(asset)
- asset.attachable_id = pk
- asset.attachable_type = 'Post'
- asset.save
- end
- def _remove_asset(asset)
- asset.attachable_id = nil
- asset.attachable_type = nil
- asset.save
- end
- def _remove_all_assets
- Asset.filter(:attachable_id=>pk, :attachable_type=>'Post')\
- .update(:attachable_id=>nil, :attachable_type=>nil)
- end
-end
-
-class Note < Sequel::Model
- one_to_many :assets, :key=>:attachable_id do |ds|
- ds.filter(:attachable_type=>'Note')
- end
-
- private
-
- def _add_asset(asset)
- asset.attachable_id = pk
- asset.attachable_type = 'Note'
- asset.save
- end
- def _remove_asset(asset)
- asset.attachable_id = nil
- asset.attachable_type = nil
- asset.save
- end
- def _remove_all_assets
- Asset.filter(:attachable_id=>pk, :attachable_type=>'Note')\
- .update(:attachable_id=>nil, :attachable_type=>nil)
- end
-end
-
-@asset.attachable = @post
-@asset.attachable = @note
-
-
-Thats quite a bit of code. With sequel_polymorphic you can now do:
-
-class Note < Sequel::Model
- is :polymorphic
- one_to_many :assets, :as => :attachable
-end
-
-class Asset < Sequel::Model
- is :polymorphic
- many_to_one :attachable, :polymorphic => true
-end
-
-voila!
View
@@ -0,0 +1,60 @@
+# Sequel Polymorphic
+
+A simple plugin for Sequel::Model's that lets you easily create polymorphic associations.
+
+## Usage
+
+### Models
+
+```ruby
+Sequel::Model.plugin(:polymorphic)
+
+class Asset < Sequel::Model
+ belongs_to :attachable, :polymorphic => true
+end
+
+class Note < Sequel::Model
+ has_many :assets, :as => :attachable
+end
+
+class Post < Sequel::Model
+ has_many :assets, :as => :attachable
+end
+```
+### Schema
+
+Include the polymorphic columns in your DB schema:
+
+```ruby
+Sequel.migration do
+ change do
+ create_table :assets do
+ # ...
+ Integer :attachable_id
+ String :attachable_type
+ # ...
+ index [:attachable_id, :attachable_type]
+ end
+ end
+end
+```
+
+## Similar to ActiveRecord Style
+
+```ruby
+class Asset < ActiveRecord::Base
+ belongs_to :attachable, :polymorphic => true
+end
+
+class Post < ActiveRecord::Base
+ has_many :assets, :as => :attachable
+end
+
+class Note < ActiveRecord::Base
+ has_many :assets, :as => :attachable
+end
+```
+
+## Sequel (without the polymorphic plugin)
+
+Check the [Advanced Associations](http://sequel.rubyforge.org/rdoc/files/doc/advanced_associations_rdoc.html) section of the [Sequel](http://sequel.rubyforge.org) docs (search "Polymorphic Associations")
@@ -10,8 +10,8 @@ Gem::Specification.new do |s|
s.date = %q{2010-11-26}
s.description = %q{A gem that provides Sequel::Models with polymorphic association capabilities}
s.email = %q{jack.dempsey@gmail.com saimonmoore@gmail.com}
- s.extra_rdoc_files = ["README", "LICENSE", "TODO"]
- s.files = ["LICENSE", "README", "TODO", "lib/sequel_polymorphic.rb", "lib/sequel/plugins/polymorphic.rb"]
+ s.extra_rdoc_files = ["README.md", "LICENSE", "TODO"]
+ s.files = ["LICENSE", "README.md", "TODO", "lib/sequel_polymorphic.rb", "lib/sequel/plugins/polymorphic.rb"]
s.homepage = %q{http://jackndempsey.blogspot.com}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}

0 comments on commit 96e0028

Please sign in to comment.