From b41caedfa0cef91d36ab50b9a541fb206bd0440a Mon Sep 17 00:00:00 2001 From: Albert Viscasillas Date: Fri, 21 Nov 2014 18:17:54 +0100 Subject: [PATCH 1/4] Provide validate helper with `:if` option The `:if` options allows to define conditions for validations to be executed just in concrete contexts. --- motion/validatable.rb | 2 ++ spec/validation_spec.rb | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/motion/validatable.rb b/motion/validatable.rb index c545a06..ff1a36f 100644 --- a/motion/validatable.rb +++ b/motion/validatable.rb @@ -131,6 +131,8 @@ def validate_for(field, value) end def validate_one(field, validation) #nodoc + return true unless validation[:if] ? send(validation[:if]) : true + result = true validation.each_pair do |validation_type, setting| if self.respond_to? validation_method(validation_type) diff --git a/spec/validation_spec.rb b/spec/validation_spec.rb index c9994d4..d67df4d 100644 --- a/spec/validation_spec.rb +++ b/spec/validation_spec.rb @@ -6,7 +6,8 @@ class ValidatableTask :email => :string, :some_day => :string, :some_float => :float, - :some_int => :int + :some_int => :int, + :not_validate => :string validate :name, :presence => true validate :name, :length => 2..10 @@ -15,6 +16,11 @@ class ValidatableTask validate :some_day, :length => 8..10 validate :some_float, :presence => true validate :some_int, :presence => true + validate :not_validate, :presence => true, :if => :false_condition_method + + def false_condition_method + false + end end describe "validations" do @@ -24,10 +30,18 @@ class ValidatableTask :email => 'bob@domain.com', :some_day => '12-12-12', :some_float => 1.080, - :some_int => 99 + :some_int => 99, + :not_validate => 'Never validated column' } end + describe 'conditional validation' do + it 'does not validate if condition method returns false' do + task = ValidatableTask.new(@valid_tasks.except(:not_validate)) + task.valid?.should == true + end + end + describe "presence" do it "is initially false if name is blank" do task = ValidatableTask.new(@valid_tasks.except(:name)) From 8e56b16da0487b4811cd402314d3953aaabee0d3 Mon Sep 17 00:00:00 2001 From: Albert Viscasillas Date: Fri, 21 Nov 2014 18:29:01 +0100 Subject: [PATCH 2/4] Update README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 14c2920..d4973ce 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,15 @@ Here are some sample validations: validate :field_name, :email => true validate :field_name, :format => /\A\d?\d-\d?\d-\d\d\Z/ # expected string format would be like '12-12-12' +You can also define conditions to execute validations just in some contexts: +```ruby +validate :field_name, :presence => true, :if => :some_context? + +def some_context? + # Your conditional stuff here +end +``` + The framework is sufficiently flexible that you can add in custom validators like so: ```ruby From 5f9d47b244eed74c6ab4f6fdf794051d18999d30 Mon Sep 17 00:00:00 2001 From: Albert Viscasillas Date: Sat, 22 Nov 2014 14:27:27 +0100 Subject: [PATCH 3/4] Allow proc in validatation conditionals --- README.md | 4 ++++ motion/validatable.rb | 15 ++++++++++++++- spec/validation_spec.rb | 24 ++++++++++++++++++------ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d4973ce..062f349 100644 --- a/README.md +++ b/README.md @@ -364,8 +364,12 @@ Here are some sample validations: You can also define conditions to execute validations just in some contexts: ```ruby +# using a method passed as symbol validate :field_name, :presence => true, :if => :some_context? +# or directly with proc/lambda +validate :filed_name, :presence => true, :if => -> { # stuff here } + def some_context? # Your conditional stuff here end diff --git a/motion/validatable.rb b/motion/validatable.rb index ff1a36f..d43556b 100644 --- a/motion/validatable.rb +++ b/motion/validatable.rb @@ -130,8 +130,21 @@ def validate_for(field, value) result end + def must_validate?(validation) + stuff = validation[:if] + return true unless stuff + + case stuff + when Symbol then send(stuff) + when Proc then instance_exec(&stuff) + else + fail ArgumentError, + ':if requires a Symbol denoting a method or a Proc/lambda' + end + end + def validate_one(field, validation) #nodoc - return true unless validation[:if] ? send(validation[:if]) : true + return true unless must_validate?(validation) result = true validation.each_pair do |validation_type, setting| diff --git a/spec/validation_spec.rb b/spec/validation_spec.rb index d67df4d..b0c0933 100644 --- a/spec/validation_spec.rb +++ b/spec/validation_spec.rb @@ -7,7 +7,8 @@ class ValidatableTask :some_day => :string, :some_float => :float, :some_int => :int, - :not_validate => :string + :if_with_proc => :string, + :if_with_sym => :string validate :name, :presence => true validate :name, :length => 2..10 @@ -16,7 +17,8 @@ class ValidatableTask validate :some_day, :length => 8..10 validate :some_float, :presence => true validate :some_int, :presence => true - validate :not_validate, :presence => true, :if => :false_condition_method + validate :if_with_proc, :presence => true, :if => -> { if_with_proc == 'unexpected' } + validate :if_with_sym, :presence => true, :if => :false_condition_method def false_condition_method false @@ -31,14 +33,24 @@ def false_condition_method :some_day => '12-12-12', :some_float => 1.080, :some_int => 99, - :not_validate => 'Never validated column' + :if_with_proc => 'Never validated column', + :if_with_sym => 'Never validated column' } end describe 'conditional validation' do - it 'does not validate if condition method returns false' do - task = ValidatableTask.new(@valid_tasks.except(:not_validate)) - task.valid?.should == true + context 'with conditional by method' do + it 'does not validate if condition returns false' do + task = ValidatableTask.new(@valid_tasks.except(:if_with_sym)) + task.valid?.should == true + end + end + + context 'with conditional by proc' do + it 'does not validate if condition returns false' do + task = ValidatableTask.new(@valid_tasks.except(:if_with_proc)) + task.valid?.should == true + end end end From 20b82cb5e552ca553810844d0e76ab34553df0a8 Mon Sep 17 00:00:00 2001 From: Albert Viscasillas Date: Sat, 22 Nov 2014 18:53:28 +0100 Subject: [PATCH 4/4] Fix travis dependencies failure --- .travis.yml | 9 ++++++--- Gemfile | 2 +- Gemfile.lock | 18 +++++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1d8c67f..a5df186 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,9 @@ language: objective-c before_install: - ruby --version - sudo chown -R travis ~/Library/RubyMotion -rvm: - - "1.9.3" -script: bundle exec rake spec + - mkdir -p ~/Library/RubyMotion/build + - sudo motion update +gemfile: + - Gemfile +script: + - bundle exec rake spec diff --git a/Gemfile b/Gemfile index f486d81..af881e0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source "https://rubygems.org" gem "rake" -gem "bubble-wrap" gem "motion-stump", '~>0.2' +gemspec diff --git a/Gemfile.lock b/Gemfile.lock index 15b3e33..ea60af2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,26 @@ +PATH + remote: . + specs: + motion_model (0.6.0) + bubble-wrap (>= 1.3.0) + motion-support (>= 0.1.0) + GEM remote: https://rubygems.org/ specs: - bubble-wrap (1.4.0) + bubble-wrap (1.7.1) + bubble-wrap-http (= 1.7.1) + bubble-wrap-http (1.7.1) + motion-require (0.2.0) motion-stump (0.3.0) - rake (10.1.0) + motion-support (0.2.6) + motion-require (>= 0.0.6) + rake (10.3.2) PLATFORMS ruby DEPENDENCIES - bubble-wrap motion-stump (~> 0.2) + motion_model! rake