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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d18679f078afeb4b6a651297b270735f70e2fa1
4
- data.tar.gz: 7a05aca0063dc2bc09601e9d306b6a0468b93045
3
+ metadata.gz: 67be0ece0a1ae6f81acfad8c591a9594f34ab071
4
+ data.tar.gz: dbd018f0301d78b8f5f240bc57727932861fa90e
5
5
  SHA512:
6
- metadata.gz: 74743f076bc27cb88816f80172a0905228b4e92ed698b3ae6c5cda0823a1af2e6c3111d20302b481d8c19b8d2fb082c678462b9f893ce2ad8b07a09b8a80baef
7
- data.tar.gz: 4517371a447ca864f18cf125b90559699c9d07660cd617be5c840ae50a9f09b1bd346baa4e9559298d612ceeab3dd822675225aca70dc38e149590d979ee7865
6
+ metadata.gz: b419b10dff4154c1896b040dfe2bb69cb3c389727cd388693a2cb94d5076973619266b521c95cc0b4049fef4e5b40a0480449cd7a6ef13822fb14678ee3d499b
7
+ data.tar.gz: ef9321bfc502dbd8314d7ad8de12d3fab2689b99e495b74673e73a64a3f02141a9a2a05fbfd52cba3bfc8289aaa254006bb5dfaaf1e2dba8c7c19e69f645798c
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ *.gem
1
2
  *.rbc
2
3
  *.sassc
3
4
  .sass-cache
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tiny_validations (0.1)
4
+ tiny_validations (0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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( key, condition = :if, &block)
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] = -> { context_key(key) == context_key(context_validation) }
24
+ options[condition] = -> { keys.include?(context_key(validation_context)) }
23
25
 
24
26
  instance_eval do
25
- attr_accessor context_key(key)
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.send("context_validation=", key)
34
+ self.validation_context = key
36
35
  else
37
36
  raise 'Context not found'
38
37
  end
@@ -1,3 +1,3 @@
1
1
  module TinyValidations
2
- VERSION = 0.1
2
+ VERSION = 0.2
3
3
  end
@@ -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
 
@@ -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 test_validatios_when
13
- # context :password_reset
14
- @user.context = :password_reset
15
- @user.password = 'pass19822'
16
- assert @user.valid?
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
- #context :facebook_auth
19
- @user.context = :facebook_auth
20
- @user.fb_token_secret = 'fff22111fff'
21
- assert_equal false, @user.valid?
23
+ def test_validations_when_not
24
+ # context: :password_reset
25
+ @user.valid?(:password_reset)
26
+ asserts_when_not
22
27
 
23
- @user.city = 'New York'
24
- @user.state = 'NY'
25
- assert @user.valid?
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.1'
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-03 00:00:00.000000000 Z
11
+ date: 2014-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler