tiny_validations 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/tiny_validations.rb +11 -12
- data/lib/tiny_validations/version.rb +1 -1
- data/test/test_helper.rb +9 -2
- data/test/validations_test.rb +19 -14
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 67be0ece0a1ae6f81acfad8c591a9594f34ab071
         | 
| 4 | 
            +
              data.tar.gz: dbd018f0301d78b8f5f240bc57727932861fa90e
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: b419b10dff4154c1896b040dfe2bb69cb3c389727cd388693a2cb94d5076973619266b521c95cc0b4049fef4e5b40a0480449cd7a6ef13822fb14678ee3d499b
         | 
| 7 | 
            +
              data.tar.gz: ef9321bfc502dbd8314d7ad8de12d3fab2689b99e495b74673e73a64a3f02141a9a2a05fbfd52cba3bfc8289aaa254006bb5dfaaf1e2dba8c7c19e69f645798c
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/lib/tiny_validations.rb
    CHANGED
    
    | @@ -5,10 +5,6 @@ require 'active_record' | |
| 5 5 | 
             
            module TinyValidations
         | 
| 6 6 | 
             
              extend ActiveSupport::Concern
         | 
| 7 7 |  | 
| 8 | 
            -
              included do
         | 
| 9 | 
            -
                attr_accessor :context_validation
         | 
| 10 | 
            -
              end
         | 
| 11 | 
            -
             | 
| 12 8 | 
             
              def validations_when(key , &block)
         | 
| 13 9 | 
             
                validations_base(key, &block)
         | 
| 14 10 | 
             
              end
         | 
| @@ -17,22 +13,25 @@ module TinyValidations | |
| 17 13 | 
             
                validations_base(key, :unless, &block)
         | 
| 18 14 | 
             
              end
         | 
| 19 15 |  | 
| 20 | 
            -
              def validations_base( | 
| 16 | 
            +
              def validations_base(rule, condition = :if, &block)
         | 
| 17 | 
            +
                if rule.is_a?(Array)
         | 
| 18 | 
            +
                  keys = rule.map { |r| context_key(r) }
         | 
| 19 | 
            +
                else
         | 
| 20 | 
            +
                  keys = Array(context_key(rule))
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 21 23 | 
             
                options = {}
         | 
| 22 | 
            -
                options[condition] = -> {  | 
| 24 | 
            +
                options[condition] = -> { keys.include?(context_key(validation_context)) }
         | 
| 23 25 |  | 
| 24 26 | 
             
                instance_eval do
         | 
| 25 | 
            -
                  attr_accessor  | 
| 26 | 
            -
             | 
| 27 | 
            -
                  with_options(options) do |validations|
         | 
| 28 | 
            -
                    yield validations
         | 
| 29 | 
            -
                  end
         | 
| 27 | 
            +
                  keys.each { |k| attr_accessor k }
         | 
| 28 | 
            +
                  with_options(options) { |validations| yield validations }
         | 
| 30 29 | 
             
                end
         | 
| 31 30 | 
             
              end
         | 
| 32 31 |  | 
| 33 32 | 
             
              def context=(key)
         | 
| 34 33 | 
             
                if self.respond_to?(context_key(key))
         | 
| 35 | 
            -
                  self. | 
| 34 | 
            +
                  self.validation_context = key
         | 
| 36 35 | 
             
                else
         | 
| 37 36 | 
             
                  raise 'Context not found'
         | 
| 38 37 | 
             
                end
         | 
    
        data/test/test_helper.rb
    CHANGED
    
    | @@ -34,7 +34,7 @@ class User < ActiveRecord::Base | |
| 34 34 |  | 
| 35 35 | 
             
              validates :name, :email, presence: true
         | 
| 36 36 |  | 
| 37 | 
            -
              validations_when_not :password_reset do |m|
         | 
| 37 | 
            +
              validations_when_not [:password_reset, :user_subscription] do |m|
         | 
| 38 38 | 
             
                m.validates :city, :state, presence: true
         | 
| 39 39 | 
             
                m.validates_inclusion_of :state, in: ['NY']
         | 
| 40 40 | 
             
              end
         | 
| @@ -46,7 +46,6 @@ class User < ActiveRecord::Base | |
| 46 46 | 
             
              validations_when :password_reset do |m|
         | 
| 47 47 | 
             
                m.validates :password, presence: true
         | 
| 48 48 | 
             
              end
         | 
| 49 | 
            -
             | 
| 50 49 | 
             
            end
         | 
| 51 50 |  | 
| 52 51 |  | 
| @@ -57,5 +56,13 @@ class Minitest::Unit::TestCase | |
| 57 56 | 
             
                                 email: 'nandosousafr@gmail.com')
         | 
| 58 57 |  | 
| 59 58 | 
             
              end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              def asserts_when_not
         | 
| 61 | 
            +
                refute_includes @user.errors, :city,
         | 
| 62 | 
            +
                  'city is validating to :password_reset_context'
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                refute_includes @user.errors, :state,
         | 
| 65 | 
            +
                  'state is validating to :password_reset_context'
         | 
| 66 | 
            +
              end
         | 
| 60 67 | 
             
            end
         | 
| 61 68 |  | 
    
        data/test/validations_test.rb
    CHANGED
    
    | @@ -5,23 +5,28 @@ class ValidationsTest < Minitest::Unit::TestCase | |
| 5 5 | 
             
              def test_set_contexts
         | 
| 6 6 | 
             
                assert @user.respond_to?(:context_validations_password_reset)
         | 
| 7 7 | 
             
                assert @user.respond_to?(:context_validations_facebook_auth)
         | 
| 8 | 
            -
             | 
| 9 | 
            -
                assert @user.respond_to?(:context_validation)
         | 
| 8 | 
            +
                assert @user.respond_to?(:context_validations_user_subscription)
         | 
| 10 9 | 
             
              end
         | 
| 11 10 |  | 
| 12 | 
            -
              def  | 
| 13 | 
            -
                # context :password_reset
         | 
| 14 | 
            -
                @user. | 
| 15 | 
            -
                @user.password | 
| 16 | 
            -
             | 
| 11 | 
            +
              def test_validations_when
         | 
| 12 | 
            +
                # context: :password_reset
         | 
| 13 | 
            +
                @user.valid?(:password_reset)
         | 
| 14 | 
            +
                assert_includes @user.errors, :password,
         | 
| 15 | 
            +
                  'password is not validating to :password_reset context'
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                # context: :facebook_auth
         | 
| 18 | 
            +
                @user.valid?(:facebook_auth)
         | 
| 19 | 
            +
                assert_includes @user.errors, :fb_token_secret,
         | 
| 20 | 
            +
                  'fb_token_secredt is not validating to :facebook_auth context'
         | 
| 21 | 
            +
              end
         | 
| 17 22 |  | 
| 18 | 
            -
             | 
| 19 | 
            -
                 | 
| 20 | 
            -
                @user. | 
| 21 | 
            -
                 | 
| 23 | 
            +
              def test_validations_when_not
         | 
| 24 | 
            +
                # context: :password_reset
         | 
| 25 | 
            +
                @user.valid?(:password_reset)
         | 
| 26 | 
            +
                asserts_when_not
         | 
| 22 27 |  | 
| 23 | 
            -
                 | 
| 24 | 
            -
                @user. | 
| 25 | 
            -
                 | 
| 28 | 
            +
                # context: :user_subscription
         | 
| 29 | 
            +
                @user.valid?(:user_subscription)
         | 
| 30 | 
            +
                asserts_when_not
         | 
| 26 31 | 
             
              end
         | 
| 27 32 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: tiny_validations
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: '0. | 
| 4 | 
            +
              version: '0.2'
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Nando Sousa
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2014-03- | 
| 11 | 
            +
            date: 2014-03-09 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |