context_validations 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d11c171439aaf9f2e7e22d2dea1e148f061f0daa
4
- data.tar.gz: 4d430fc4aac414999a209f76411c10962bf1bd88
3
+ metadata.gz: 1e5d88bf3e08d906667a9d76bc454e4fb2319c17
4
+ data.tar.gz: 1700bb40d4ef8f2af86e3f74ec6ac1185093940e
5
5
  SHA512:
6
- metadata.gz: 6586b690a8e4e97cae04a5e8a77636245ee0b875b870daadb0c86263483e5f70500b7b3196fcbc2f0ae8fcd4d50c795f4680b6ad3767762208a0e9a6a5b507fa
7
- data.tar.gz: 42563e3c5c1a493f09493e837f01a4e3ffe8aa920864c1b2a0f09a249b6ef5c538c336ec49f1328791a511f649aa57e307c26cea7e4ba08255e5a09a7bb63abf
6
+ metadata.gz: 8d6e73defea0447f9dfb43f269166a1886b56b2f6aaa974a9d1f3f2371000f445b340b7f61d18e76caaa43c3ace4078fe230e434351e770c824e51b90b9c2dac
7
+ data.tar.gz: b5b93d9d2d5d0157f69f0662b48d55cea488bc64be75ba6f158fff27dc33fc43fb4cbcd38b2c6957d410d14d166d606be9a03abe43bb63ea23c8fa0378f0aad0
data/README.md CHANGED
@@ -127,6 +127,37 @@ use `ActiveRecord::Base.create` as we do not want to allow the
127
127
  validations to be set via mass-assignment. This does introduce an extra
128
128
  step in some places but it shouldn't be that big of a deal.
129
129
 
130
+ ## Testing ##
131
+
132
+ Currently only `MiniTest` is supported. We are open to pull requests for supporting additional test frameworks but we only work with `MiniTest`
133
+ so we probably won't go out of the way to support something we're not using.
134
+
135
+ We highly recommend using [ValidAttribute](https://github.com/bcardarella/valid_attribute) to test your validations. The following example is done using
136
+ `ValidAttribute`.
137
+
138
+ ### MiniTest ###
139
+
140
+ You are given access to a `#validations_for(:action_name)` method. You should pass the action in your
141
+ controller that is the context of the validations and use the `#valiadtions=` setter on the model.
142
+
143
+ This is a common example of how to test:
144
+
145
+ ```ruby
146
+ require 'test_helper'
147
+
148
+ describe UserController do
149
+ context 'create' do
150
+ subject { User.new(:password => 'password', :validations => validations_for(:create)) }
151
+ it { must have_valid(:name).when('Brian Cardarella') }
152
+ it { wont have_valid(:name).when(nil, '') }
153
+ end
154
+ end
155
+ ```
156
+
157
+ ## ClientSideValidations Support ##
158
+
159
+ The [ClientSideValidations](https://github.com/bcardarella/client_side_validations) gem is fully supported.
160
+
130
161
  ## Authors ##
131
162
 
132
163
  * [Brian Cardarella](http://twitter.com/bcardarella)
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'minitest'
24
24
  spec.add_development_dependency 'm'
25
- spec.add_development_dependency 'activemodel'
25
+ spec.add_development_dependency 'activerecord'
26
+ spec.add_development_dependency 'sqlite3'
26
27
  end
@@ -1,5 +1,8 @@
1
1
  require 'context_validations/version'
2
2
  require 'active_support'
3
+ if defined?(MiniTest::Unit::TestCase)
4
+ require 'context_validations/minitest'
5
+ end
3
6
 
4
7
  module ContextValidations
5
8
  extend ActiveSupport::Autoload
@@ -51,7 +51,8 @@ module ContextValidations::Controller
51
51
  defaults[:attributes] = [attribute]
52
52
  validations.each do |key, options|
53
53
  key = "#{key.to_s.camelize}Validator"
54
- klass = key.include?('::') ? key.constantize : "ActiveModel::Validations::#{key}".constantize
54
+ namespace = defined?(ActiveRecord) ? ActiveRecord::Base : ActiveModel::Validations
55
+ klass = key.include?('::') ? key.constantize : namespace.const_get(key)
55
56
  validator = klass.new(defaults.merge(_parse_validates_options(options)))
56
57
  validators << validator
57
58
  end
@@ -0,0 +1,27 @@
1
+ module ContextValidations
2
+ module ValidationsFor
3
+ module MiniTest
4
+ def validations_for(action)
5
+ determine_constant_from_test_name.new.validations(action)
6
+ end
7
+
8
+ def determine_constant_from_test_name
9
+ names = self.class.name.split('::')
10
+
11
+ while names.size > 0 do
12
+ names.last.sub!(/Test$/, '')
13
+ begin
14
+ constant = names.join('::').constantize
15
+ break(constant) if constant
16
+ rescue NameError
17
+ # Constant wasn't found, move on
18
+ ensure
19
+ names.pop
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ MiniTest::Unit::TestCase.send(:include, ContextValidations::ValidationsFor::MiniTest)
@@ -29,8 +29,25 @@ module ContextValidations::Model
29
29
 
30
30
  def run_validations!
31
31
  Array.wrap(validations).each do |validator|
32
+ if validator.respond_to?(:setup)
33
+ validator.setup(self.class)
34
+ end
32
35
  validator.validate(self)
33
36
  end
34
37
  errors.empty?
35
38
  end
39
+
40
+ private
41
+
42
+ def _validators
43
+ validations.inject({}) do |hash, validator|
44
+ attribute = validator.attributes.first
45
+ if hash.key?(attribute)
46
+ hash[attribute] << validator
47
+ else
48
+ hash[attribute] = [validator]
49
+ end
50
+ hash
51
+ end
52
+ end
36
53
  end
@@ -1,3 +1,3 @@
1
1
  module ContextValidations
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -60,6 +60,27 @@ class PrivateController
60
60
  end
61
61
  end
62
62
 
63
+ class CustomValidator < ActiveModel::EachValidator
64
+ def validate_each(record, attribute, value)
65
+ if value != 'awesome'
66
+ record.errors.add(attribute, 'Failed')
67
+ end
68
+ end
69
+ end
70
+
71
+ class CustomValidatorsController
72
+ include ContextValidations::Controller
73
+
74
+ def create
75
+ validations(:create)
76
+ end
77
+
78
+ private
79
+
80
+ def base_validations
81
+ validates :first_name, :custom => true
82
+ end
83
+ end
63
84
 
64
85
  describe 'Controller' do
65
86
  context 'Public validations' do
@@ -101,4 +122,14 @@ describe 'Controller' do
101
122
  @controller.create.length.must_equal 2
102
123
  end
103
124
  end
125
+
126
+ context 'Custom validations' do
127
+ before do
128
+ @controller = CustomValidatorsController.new
129
+ end
130
+
131
+ it 'combines base and create validations for other create action, context is forced' do
132
+ @controller.create.length.must_equal 1
133
+ end
134
+ end
104
135
  end
data/test/model_test.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  require 'test_helper'
2
- require 'active_model'
3
2
 
4
3
  EmailFormat = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
5
4
 
6
- class User
7
- include ActiveModel::Validations
5
+ users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, first_name TEXT, email TEXT);}
6
+ ActiveRecord::Base.connection.execute(users_table)
7
+ class User < ActiveRecord::Base
8
8
  include ContextValidations::Model
9
- attr_accessor :first_name, :email
10
9
 
11
10
  validates :first_name, :presence => true
12
11
  validates :email, :format => EmailFormat
@@ -22,7 +21,7 @@ describe 'Model' do
22
21
  end
23
22
 
24
23
  it 'accepts validations set onto the instance' do
25
- validations = [ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name]), ActiveModel::Validations::FormatValidator.new(:attributes => [:email], :with => EmailFormat)]
24
+ validations = [ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name]), ActiveModel::Validations::FormatValidator.new(:attributes => [:email], :with => EmailFormat), ActiveRecord::Validations::UniquenessValidator.new(:attributes => [:email])]
26
25
  @user.validations = validations
27
26
  @user.valid?.must_equal false
28
27
  @user.errors.count.must_equal 2
data/test/test_helper.rb CHANGED
@@ -6,6 +6,7 @@ else
6
6
  require 'minitest/autorun'
7
7
  end
8
8
 
9
+ require 'active_record'
9
10
  require 'context_validations'
10
11
 
11
12
  class MiniTest::Spec
@@ -13,3 +14,8 @@ class MiniTest::Spec
13
14
  alias :context :describe
14
15
  end
15
16
  end
17
+
18
+ ActiveRecord::Base.establish_connection(
19
+ :adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
20
+ :database => ':memory:'
21
+ )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: context_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cardarella
@@ -81,7 +81,21 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: activemodel
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - '>='
@@ -110,6 +124,7 @@ files:
110
124
  - context_validations.gemspec
111
125
  - lib/context_validations.rb
112
126
  - lib/context_validations/controller.rb
127
+ - lib/context_validations/minitest.rb
113
128
  - lib/context_validations/model.rb
114
129
  - lib/context_validations/version.rb
115
130
  - test/controller_test.rb