Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve handling of associations in sequel

[fixes #123]
commit e9d563cd5ab1347e664595585d404174d93297d7 1 parent b656aa9
@paulelliott authored
View
16 lib/fabrication/generator/sequel.rb
@@ -1,11 +1,25 @@
class Fabrication::Generator::Sequel < Fabrication::Generator::Base
+ def initialize(klass)
+ super
+ __klass.plugin :instance_hooks unless __klass.new.respond_to? :after_save_hook
+ end
+
def self.supports?(klass)
defined?(Sequel) && klass.ancestors.include?(Sequel::Model)
end
def set_attributes
- __instance.set_fields(__attributes, __attributes.keys)
+ __attributes.each do |key, value|
+ if (reflection = __klass.association_reflections[key]) && value.is_a?(Array)
+ __instance.associations[key] = value
+ __instance.after_save_hook do
+ value.each { |o| __instance.send(reflection.add_method, o) }
+ end
+ else
+ __instance.send("#{key}=", value)
+ end
+ end
end
def persist
View
6 spec/fabricators/sequel_fabricator.rb
@@ -8,11 +8,7 @@
end
Fabricator(:parent_sequel_model_with_children, from: :parent_sequel_model) do
- after_create do |parent|
- 2.times do
- Fabricate(:child_sequel_model, :parent_sequel_model => parent)
- end
- end
+ child_sequel_models(count: 2)
end
Fabricator(:child_sequel_model) do
Please sign in to comment.
Something went wrong with that request. Please try again.