Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use #create when a model doesn't know about #create!

Sequel 3.0 (and maybe earlier versions?) doesn't define a #create!
method on models, only #create.
  • Loading branch information...
commit 1d9eef798c0ee939e03b222114cde3ef518b8de2 1 parent 4785e5a
@foca authored
Showing with 23 additions and 6 deletions.
  1. +2 −1  lib/spawner.rb
  2. +21 −5 test/all_test.rb
View
3  lib/spawner.rb
@@ -8,6 +8,7 @@ def spawner &default
def spawn attrs = {}
@@spawn[self.name].call(model = OpenStruct.new)
- create!(model.send(:table).merge(attrs))
+ factory_method = respond_to?(:create!) ? :create! : :create
+ send(factory_method, model.send(:table).merge(attrs))
end
end
View
26 test/all_test.rb
@@ -2,14 +2,14 @@
require 'contest'
require File.dirname(__FILE__) + "/../lib/spawner"
-class Foo
+class Base
attr_accessor :attributes
def initialize(attrs = {})
@attributes = attrs
end
- def self.create!(attrs = {})
+ def self.create(attrs = {})
new(attrs)
end
@@ -19,9 +19,15 @@ def self.name; "Foo" end
extend Spawner
- spawner do |foo|
- foo.bar = 7
- foo.baz = 8
+ spawner do |object|
+ object.bar = 7
+ object.baz = 8
+ end
+end
+
+class Foo < Base
+ class << self
+ alias_method :create!, :create
end
end
@@ -34,6 +40,9 @@ def self.name; "Bar" end
end
end
+class Baz < Base
+end
+
class TestFoo < Test::Unit::TestCase
should "have a name class method" do
assert Foo.respond_to?(:name)
@@ -97,5 +106,12 @@ class TestFoo < Test::Unit::TestCase
end
end
end
+
+ context "and it doesn't understand #create! (only #create)" do
+ should "work anyway" do
+ baz = Baz.spawn :bar => 1
+ assert_equal 1, baz.bar
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.