public
Rubygem
Fork of sam/dm-more
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/dysinger/dm-more.git
Search Repo:
Validation bug fixes
  * Fixed ticket 204
  * Clean up Rakefile
  * Retrieve value of field in more consistent way
guyvdb (author)
Tue Apr 15 22:30:10 -0700 2008
commit  fbf2bd1964b65dc94e38f55485f8a8e834596f6f
tree    6a3ece177992287414318bd0d69b719b4e325dde
parent  09e2bca4833702719797f68c8c5dba42f275632c
...
14
15
16
17
 
18
19
20
...
14
15
16
 
17
18
19
20
0
@@ -14,7 +14,7 @@
0
   s.version = VERSION
0
   s.platform = Gem::Platform::RUBY
0
   s.has_rdoc = true
0
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
0
+ s.extra_rdoc_files = ["README", "LICENSE", "TODO"]
0
   s.summary = SUMMARY
0
   s.description = s.summary
0
   s.author = AUTHOR
...
94
95
96
97
 
98
99
100
...
94
95
96
 
97
98
99
100
0
@@ -94,7 +94,7 @@
0
       
0
       
0
       def validation_property_value(name)
0
- return self.instance_variable_get("@#{name}") if self.class.properties(self.class.repository.name)[name]
0
+ return self.instance_variable_get("@#{name}") if self.instance_variables.include?(name)
0
         return self.send(name) if self.respond_to?(name)
0
         nil
0
       end
...
46
47
48
49
 
 
50
51
52
...
46
47
48
 
49
50
51
52
53
0
@@ -46,7 +46,8 @@
0
             if property.options.has_key?(:length) || property.options.has_key?(:size)
0
               len = property.options.has_key?(:length) ? property.options[:length] : property.options[:size]
0
               opts[:within] = len if len.is_a?(Range)
0
- opts[:maximum] = len unless len.is_a?(Range)
0
+ opts[:maximum] = len unless len.is_a?(Range)
0
+ opts[:allow_nil] = property.options[:nullable] if property.options.has_key?(:nullable)
0
               validates_length_of property.name, opts
0
             else
0
               opts[:maximum] = 50 #default string size
...
20
21
22
23
 
24
 
 
25
26
27
...
20
21
22
 
23
24
25
26
27
28
29
0
@@ -20,8 +20,10 @@
0
       end
0
       
0
       def call(target)
0
- field_value = target.instance_variable_get("@#{@field_name}").to_s
0
+ field_value = target.validation_property_value(@field_name)
0
         return true if @options[:allow_nil] && field_value.nil?
0
+
0
+ field_value = '' if field_value.nil?
0
         
0
         # HACK seems hacky to do this on every validation, probably should do this elsewhere?
0
         field = DataMapper::Inflection.humanize(@field_name)
...
8
9
10
11
12
13
 
 
 
 
 
 
14
15
16
...
8
9
10
 
 
 
11
12
13
14
15
16
17
18
19
0
@@ -8,9 +8,12 @@
0
         @field_name, @options = field_name, options
0
       end
0
       
0
- def call(target)
0
- field_value = !target.instance_variable_get("@#{@field_name}").blank?
0
- return true if field_value
0
+ def call(target)
0
+ if @field_name == 'customer'
0
+ puts @field_name
0
+ end
0
+ value = target.validation_property_value(@field_name)
0
+ return true if !value.blank?
0
         
0
         error_message = @options[:message] || "%s must not be blank".t(DataMapper::Inflection.humanize(@field_name))
0
         add_error(target, error_message , @field_name)
...
18
19
20
21
 
22
23
24
...
18
19
20
 
21
22
23
24
0
@@ -18,7 +18,7 @@
0
           scope.map do |item|
0
             if !target.class.properties(target.class.repository.name)[item].nil?
0
               opts[item] = target.validation_property_value(item)
0
- elsif target.class.relationships.include?(item)
0
+ elsif target.class.relationships.has_key?(item) #include?(item) #TODO I chaned this to has_key? check!!!!
0
               target.validation_association_keys(item).map do |key|
0
                 opts[key] = target.validation_property_value(key)
0
               end
...
276
277
278
 
279
280
281
...
347
348
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
351
352
353
354
355
356
357
...
354
355
356
357
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
360
 
 
 
 
 
 
 
 
 
361
362
 
 
 
 
 
363
364
365
366
367
368
369
 
 
 
 
370
371
372
373
 
 
374
 
 
 
 
 
 
 
 
375
376
377
...
276
277
278
279
280
281
282
...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
...
373
374
375
 
 
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
 
409
410
411
412
413
414
415
416
 
 
 
 
417
418
419
420
421
 
 
 
422
423
424
425
426
427
428
429
430
431
432
433
434
435
0
@@ -276,6 +276,7 @@
0
           property :no_validation, String, :auto_validation => false
0
           property :salesman, String, :nullable => false, :validates => [:multi_context_1, :multi_context_2]
0
           property :code, String, :format => Proc.new { |code| code =~ /A\d{4}/}, :validates => :format_test
0
+ property :allow_nil, String, :size => 5..10, :nullable => true, :validates => :nil_test
0
         end
0
       end
0
     
0
@@ -347,6 +348,24 @@
0
         end
0
         Test.new().valid?().should == true
0
       end
0
+
0
+ it 'It should auto add range checking the length of a string while still allowing null values' do
0
+ boat = SailBoat.new()
0
+ boat.allow_nil = 'ABC'
0
+ boat.should_not be_valid_for_nil_test
0
+ boat.errors.on(:allow_nil).should include('Allow nil must be between 5 and 10 characters long')
0
+
0
+ boat.allow_nil = 'ABCDEFG'
0
+ boat.should be_valid_for_nil_test
0
+
0
+ boat.allow_nil = 'ABCDEFGHIJKLMNOP'
0
+ boat.should_not be_valid_for_nil_test
0
+ boat.errors.on(:allow_nil).should include('Allow nil must be between 5 and 10 characters long')
0
+
0
+ boat.allow_nil = nil
0
+ boat.should be_valid_for_nil_test
0
+
0
+ end
0
           
0
     end
0
   end
0
0
0
0
0
0
@@ -354,24 +373,63 @@
0
   #-----------------------------------------------------------------------------
0
 
0
   describe DataMapper::Validate::RequiredFieldValidator do
0
- before(:all) do
0
- class Boat
0
+ after do
0
+ repository(:sqlite3).adapter.execute('DROP TABLE "landscapers"');
0
+ repository(:sqlite3).adapter.execute('DROP TABLE "gardens"');
0
+ end
0
+
0
+ before do
0
+ repository(:sqlite3).adapter.execute(<<-EOS.compress_lines) rescue nil
0
+ CREATE TABLE "landscapers" (
0
+ "id" INTEGER PRIMARY KEY,
0
+ "name" VARCHAR(50)
0
+ )
0
+ EOS
0
+ repository(:sqlite3).adapter.execute(<<-EOS.compress_lines) rescue nil
0
+ CREATE TABLE "gardens" (
0
+ "id" INTEGER PRIMARY KEY,
0
+ "landscaper_id" INTEGER,
0
+ "name" VARCHAR(50)
0
+ )
0
+ EOS
0
+
0
+ class Landscaper
0
         include DataMapper::Resource
0
         include DataMapper::Validate
0
+ property :id, Fixnum, :key => true
0
+ property :name, String
0
+ end
0
+
0
+ class Garden
0
+ include DataMapper::Resource
0
+ include DataMapper::Validate
0
+ property :id, Fixnum, :key => true
0
+ property :landscaper_id, Fixnum
0
         property :name, String, :auto_validation => false
0
- validates_presence_of :name
0
+
0
+ has :landscaper, 1..n
0
+
0
+ validates_presence_of :name, :when => :property_test
0
+ validates_presence_of :landscaper, :when => :association_test
0
       end
0
     end
0
 
0
- it "should validate the presence of a value on an instance of a resource" do
0
- sting_ray = Boat.new
0
- sting_ray.valid?.should_not == true
0
- sting_ray.errors.full_messages.include?('Name must not be blank').should == true
0
+ it "should validate the presence of a property value on an instance of a resource" do
0
+ garden = Garden.new
0
+ garden.should_not be_valid_for_property_test
0
+ garden.errors.on(:name).should include('Name must not be blank')
0
       
0
- sting_ray.name = 'Sting Ray'
0
- sting_ray.valid?.should == true
0
- sting_ray.errors.full_messages.length.should == 0
0
+ garden.name = 'The Wilds'
0
+ garden.should be_valid_for_property_test
0
     end
0
+
0
+ it "should validate the presence of an association value on an instance of a resource"
0
+ #do
0
+ # garden = Garden.new
0
+ # landscaper = garden.landscaper
0
+ # puts landscaper.children.length
0
+ # #puts "Gardens landscaper is #{garden.landscaper.child_key}"
0
+ #end
0
     
0
   end
0
 

Comments

    No one has commented yet.