public
Rubygem
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-more.git
Validations#included now generates create and create! methods, to mirror 
those of save and save!
Wed Jul 23 09:14:25 -0700 2008
commit  156777f1be1c8ddf832dd73f1a82bc1537b782e9
tree    bb27c938936167e7301bbe5057c730605d0ba224
parent  3e735915b0809189b34348f14c1a3e262af896a9
...
34
35
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
38
39
...
187
188
189
 
190
191
192
 
 
 
 
193
194
195
196
 
 
 
 
197
198
199
...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
...
204
205
206
207
208
 
 
209
210
211
212
213
214
 
 
215
216
217
218
219
220
221
0
@@ -34,6 +34,23 @@ module DataMapper
0
         model.send(:alias_method, :save!, :save)
0
         model.send(:alias_method, :save, :save_with_validations)
0
       end
0
+ model.class_eval <<-EOS
0
+ class << self
0
+ method_defined?(:create) && !method_defined?(:create!)
0
+ def create(attributes = {}, context = :default)
0
+ resource = new(attributes)
0
+ return resource unless resource.valid?(context)
0
+ resource.save
0
+ resource
0
+ end
0
+
0
+ def create!(attributes = {})
0
+ resource = new(attributes)
0
+ resource.save!
0
+ resource
0
+ end
0
+ end
0
+ EOS
0
     end
0
 
0
     # Validate the resource before saving. Use #save! to save
0
@@ -187,13 +204,18 @@ module DataMapper
0
       #
0
       def add_validator_to_context(opts, fields, klazz)
0
         fields.each do |field|
0
+ validator = klazz.new(field, opts)
0
           if opts[:context].is_a?(Symbol)
0
- validators.context(opts[:context]) << klazz.new(field, opts)
0
- create_context_instance_methods(opts[:context])
0
+ unless validators.context(opts[:context]).include?(validator)
0
+ validators.context(opts[:context]) << validator
0
+ create_context_instance_methods(opts[:context])
0
+ end
0
           elsif opts[:context].is_a?(Array)
0
             opts[:context].each do |c|
0
- validators.context(c) << klazz.new(field, opts)
0
- create_context_instance_methods(c)
0
+ unless validators.context(c).include?(validator)
0
+ validators.context(c) << validator
0
+ create_context_instance_methods(c)
0
+ end
0
             end
0
           end
0
         end
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
92
93
...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
0
@@ -88,6 +88,26 @@ describe DataMapper::Validate do
0
     end
0
   end
0
 
0
+ describe '#create!' do
0
+ before do
0
+ Yacht.auto_migrate!
0
+ end
0
+
0
+ it "should save object without running validations" do
0
+ Yacht.create!.should be_a_kind_of(Yacht)
0
+ end
0
+ end
0
+
0
+ describe "#create" do
0
+ before do
0
+ Yacht.auto_migrate!
0
+ end
0
+
0
+ it "should run validations" do
0
+ Yacht.create.new_record?.should be_true
0
+ end
0
+ end
0
+
0
   it "should respond to validatable? (for recursing assocations)" do
0
     Yacht.new.should be_validatable
0
     Class.new.new.should_not be_validatable

Comments

    No one has commented yet.