Skip to content
Browse files

add the first pieces in

  • Loading branch information...
1 parent a69c43b commit 5d69228322224676e34e25a84ea860642ded783c @jackdempsey committed
Showing with 32 additions and 4 deletions.
  1. +2 −2 Rakefile
  2. +1 −1 lib/sequel_polymorphic.rb
  3. +29 −1 lib/sequel_polymorphic/sequel_polymorphic.rb
View
4 Rakefile
@@ -16,7 +16,7 @@ spec = Gem::Specification.new do |s|
s.version = GEM_VERSION
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
- s.extra_rdoc_files = ["README.markdown", "LICENSE", 'TODO']
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
s.summary = SUMMARY
s.description = s.summary
s.author = AUTHOR
@@ -28,7 +28,7 @@ spec = Gem::Specification.new do |s|
s.require_path = 'lib'
s.autorequire = GEM
- s.files = %w(LICENSE README.markdown Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
end
task :default => :spec
View
2 lib/sequel_polymorphic.rb
@@ -1,4 +1,4 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-require 'sequel_polymorphic_associations/sequel_polymorphic_associations'
+require 'sequel_polymorphic/sequel_polymorphic'
View
30 lib/sequel_polymorphic/sequel_polymorphic.rb
@@ -3,9 +3,25 @@ module Plugins
module Polymorphic
# Apply the plugin to the model.
def self.apply(model, options = {})
+ if as_variable = (options[:belongs_to] or options[:many_to_one])
+ # defining the polymorphic model
+ model.class_eval %{associate(:many_to_one, #{as_variable}, :reciprocal=>:assets,
+ :dataset=>(proc { klass = attachable_type.constantize; klass.filter(klass.primary_key=>attachable_id) }),
+ :eager_loader=>(proc do |key_hash, assets, associations|
+ id_map = {}
+ assets.each { |asset| asset.associations[:attachable] = nil; ((id_map[asset.attachable_type] ||= {})[asset.attachable_id] ||= []) << asset }
+ 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 { |asset| asset.associations[:attachable] = attach }
+ end
+ end
+ end))}
+ end
end
module InstanceMethods
+
end
module ClassMethods
@@ -13,4 +29,16 @@ module ClassMethods
end # Polymorphic
end # Plugins
end # Sequel
-
+
+# class Asset < Sequel::Model
+# is :polymorphic, :belongs_to => :attachable
+# end
+#
+# class Post < Sequel::Model
+# is :polymorphic, :has_many => :assets, :as => :attachable
+# end
+#
+# class Note < Sequel::Model
+# is :polymorphic, :has_many => :assets, :as => :attachable
+# end
+

0 comments on commit 5d69228

Please sign in to comment.
Something went wrong with that request. Please try again.