valle 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
4
+ - 2.0.0
5
5
  branches:
6
6
  only:
7
7
  - master
data/CHANGELOG.md CHANGED
@@ -1,7 +1,11 @@
1
+ ## 0.2.0 / 2013-11-16
2
+
3
+ * added attributes option [Anton Kalyaev]
4
+
1
5
  ## 0.1.0 / 2013-11-08
2
6
 
3
7
  * fixed Stack too deep error [Anton Kalyaev]
4
- * fixed enabled option [Anton Kalyaev]
8
+ * enabled option now works [Anton Kalyaev]
5
9
  * fixed db:migrate error [Anton Kalyaev]
6
10
  * dot not override AR#valid? method [Anton Kalyaev]
7
11
 
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Anton Kalyaev
1
+ Copyright (c) 2012 - 2013 Anton Kalyaev
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -17,6 +17,11 @@ Example:
17
17
  PG::Error: ERROR: value "2147483648" is out of range for type integer
18
18
  : SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
19
19
 
20
+ ### Supported Rails
21
+
22
+ - 3.x
23
+ - 4.x
24
+
20
25
  ### Supported ActiveRecord field types
21
26
 
22
27
  - `:primary_key`
@@ -55,6 +60,20 @@ Also, you should be able to turn it off temporary by setting `enabled` option to
55
60
  config.enabled = false
56
61
  end
57
62
 
63
+ ### Disabling gem for some attributes
64
+
65
+ There are cases where you need to skip validation for a particular
66
+ attribute (see [#4](https://github.com/kaize/valle/issues/4)). For
67
+ example, CarrierWave stores his images temporarily in attributes, so calling
68
+ `save` on them will fail because of LengthValidator (255 chars maximum).
69
+ You can disable this gem for such fields using `attributes` option.
70
+
71
+ Valle.configure do |config|
72
+ config.attributes = {
73
+ 'User' => %w(id name) # Model => Array of attributes to validate
74
+ }
75
+ end
76
+
58
77
  ## Alternatives
59
78
 
60
79
  There is a similar gem, called [validates_lengths_from_database](http://github.com/rubiety/validates_lengths_from_database). It solves only one part -
@@ -77,3 +77,30 @@ Feature:
77
77
  """
78
78
  When I successfully run `bundle exec rake test --trace`
79
79
  Then the output should contain "2 tests, 3 assertions, 0 failures, 0 errors"
80
+
81
+ Scenario: Using attributes option should allow us to skip some attributes
82
+ When I write to "config/initializers/valle.rb" with:
83
+ """
84
+ Valle.configure do |config|
85
+ config.attributes = {
86
+ 'User' => %w(id)
87
+ }
88
+ end
89
+ """
90
+ And I write to "test/unit/user_test.rb" with:
91
+ """
92
+ require 'test_helper'
93
+
94
+ class UserTest < ActiveSupport::TestCase
95
+ test "should raise ActiveRecord::StatementInvalid error when name is too long" do
96
+ user = FactoryGirl.create(:user)
97
+ user.name = 'a' * 256
98
+
99
+ # SQLLite3 do not throw an ActiveRecord::StatementInvalid error
100
+ # so just check that record gets saved
101
+ assert user.save
102
+ end
103
+ end
104
+ """
105
+ When I successfully run `bundle exec rake test --trace`
106
+ Then the output should contain "1 tests, 1 assertions, 0 failures, 0 errors"
data/lib/valle.rb CHANGED
@@ -43,8 +43,23 @@ module Valle
43
43
  options[:models].nil? ||
44
44
  options[:models].is_a?(Array) && options[:models].include?(model_name)
45
45
  end
46
+
47
+ ##
48
+ # Can we process this attribute
49
+ #
50
+ # If the user wants to skip some attributes, we need to check
51
+ # whether we should add validators to column or not.
52
+ #
53
+ # @param [String] model_name model name
54
+ # @param [String] attribute attribute name
55
+ # @see Valle::Configuration
56
+ #
57
+ def can_process_column?(model_name, attribute)
58
+ !options[:attributes].has_key?(model_name) ||
59
+ options[:attributes][model_name].is_a?(Array) && options[:attributes][model_name].include?(attribute)
60
+ end
46
61
  end
47
62
  end
48
63
 
49
- ## if not using Railtie, call `Valle::Hooks.init` directly
64
+ # if not using Railtie, call `Valle::Hooks.init` directly
50
65
  require 'valle/railtie' if defined? Rails
@@ -1,5 +1,5 @@
1
- # TODO [AK 09/12/12] maybe we should use ActiveSupport::Configurable ?
2
1
  require 'active_support/core_ext/module/attribute_accessors'
2
+ require 'active_support/hash_with_indifferent_access'
3
3
 
4
4
  module Valle
5
5
  module Configuration
@@ -8,7 +8,8 @@ module Valle
8
8
 
9
9
  self.options = {
10
10
  enabled: true, # gem is enabled by default
11
- models: nil # nil means to select all the AR models
11
+ models: nil, # selects all AR models by default
12
+ attributes: ActiveSupport::HashWithIndifferentAccess.new, # adds validators to all attributes by default
12
13
  }
13
14
 
14
15
  def configure
@@ -22,5 +23,9 @@ module Valle
22
23
  def models=(collection)
23
24
  self.options[:models] = collection
24
25
  end
26
+
27
+ def attributes=(mapping)
28
+ self.options[:attributes] = mapping
29
+ end
25
30
  end
26
31
  end
data/lib/valle/manager.rb CHANGED
@@ -11,10 +11,13 @@ module Valle
11
11
  def add_validators(klass)
12
12
  columns = klass.columns
13
13
  columns.each do |original_column|
14
+ next unless Valle.can_process_column?(klass.model_name.to_s, original_column.name)
15
+
14
16
  column = AbstractAdapter::ColumnWrapper.wrap(original_column)
15
17
  ValidationSetter.add_validator(column, klass)
16
18
  end
17
19
  end
20
+
18
21
  end
19
22
  end
20
23
  end
data/lib/valle/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Valle
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/valle.gemspec CHANGED
@@ -7,6 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.description = %q{Valle automatically sets minimum and maximum values for the fields of your ActiveRecord model(s), so you shouldn't worry, that string length or ID value will exceed the permissible limit.}
8
8
  gem.summary = %q{Built-in limit validations for your ActiveRecord model.}
9
9
  gem.homepage = "http://github.com/kaize/valle"
10
+ gem.license = "MIT"
10
11
 
11
12
  gem.files = `git ls-files`.split($\)
12
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-08 00:00:00.000000000 Z
12
+ date: 2013-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -80,7 +80,8 @@ files:
80
80
  - test/test_helper.rb
81
81
  - valle.gemspec
82
82
  homepage: http://github.com/kaize/valle
83
- licenses: []
83
+ licenses:
84
+ - MIT
84
85
  post_install_message:
85
86
  rdoc_options: []
86
87
  require_paths:
@@ -93,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
94
  version: '0'
94
95
  segments:
95
96
  - 0
96
- hash: -1785766048281463164
97
+ hash: 1784568943326759879
97
98
  required_rubygems_version: !ruby/object:Gem::Requirement
98
99
  none: false
99
100
  requirements:
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  version: '0'
103
104
  segments:
104
105
  - 0
105
- hash: -1785766048281463164
106
+ hash: 1784568943326759879
106
107
  requirements: []
107
108
  rubyforge_project:
108
109
  rubygems_version: 1.8.24