activerecord_mysql_strict 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ module ActiveRecord
2
+ module MySQL
3
+ module Strict
4
+ module Validations
5
+ # Constants
6
+ MYSQL_STRICT_STRING_LIMIT = 255
7
+ MYSQL_STRICT_TEXT_LIMIT = 65535
8
+ MYSQL_STRICT_INTEGER_LIMIT = 2147483647
9
+
10
+ def self.define_mysql_strict_validations(klass, options = {})
11
+ except = options[:except] || []
12
+ model_columns = klass.columns.dup.reject { |c| except.include?(c.name.to_sym) }
13
+
14
+ if only = options[:only]
15
+ model_columns = model_columns.select { |c| only.include?(c.name.to_sym) }
16
+ end
17
+
18
+ model_columns.each do |field|
19
+ method = :"define_mysql_strict_#{field.type}_validation"
20
+ Validations.send(method, klass, field) if Validations.respond_to?(method)
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def self.define_mysql_strict_string_validation(klass, field)
27
+ klass.validates field.name, 'ActiveRecord::MySQL::Strict::StrictLength' => { in: 0..(field.limit || MYSQL_STRICT_STRING_LIMIT) }, allow_blank: true
28
+ end
29
+
30
+ def self.define_mysql_strict_text_validation(klass, field)
31
+ klass.validates field.name, 'ActiveRecord::MySQL::Strict::StrictLength' => { in: 0..(field.limit || MYSQL_STRICT_TEXT_LIMIT) }, allow_blank: true
32
+ end
33
+
34
+ def self.define_mysql_strict_integer_validation(klass, field)
35
+ klass.validates field.name, numericality: { greather_than_or_equal_to: -MYSQL_STRICT_INTEGER_LIMIT, less_than_or_equal_to: MYSQL_STRICT_INTEGER_LIMIT }, allow_blank: true
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module MySQL
3
3
  module Strict
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2'
5
5
  end
6
6
  end
7
7
  end
@@ -5,11 +5,10 @@ require 'active_model'
5
5
  require 'active_support'
6
6
 
7
7
  require 'active_record/mysql/strict/strict_length_validator'
8
- require 'active_record/mysql/strict/mixin'
8
+ require 'active_record/mysql/strict/validations'
9
9
 
10
10
  class ActiveRecord::Base
11
11
  def self.validates_strict_columns(options = {})
12
- @mysql_strict_options = options
13
- include ActiveRecord::MySQL::Strict::Mixin
12
+ ActiveRecord::MySQL::Strict::Validations.define_mysql_strict_validations(self, options)
14
13
  end
15
14
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ActiveRecord::MySQL::Strict::Mixin do
4
- describe :validates_strict_columns do
3
+ describe ActiveRecord::MySQL::Strict::Validations do
4
+ describe :define_mysql_strict_validations do
5
5
  context 'for model without other validations' do
6
6
  let(:model) { strict_model 'User' }
7
7
 
@@ -129,7 +129,7 @@ describe ActiveRecord::MySQL::Strict::Mixin do
129
129
  end
130
130
  end
131
131
 
132
- context 'for model with accessor that returns an invalid string' do
132
+ context 'for valid model with accessor that returns an invalid string' do
133
133
  let(:model) do
134
134
  strict_model 'User' do
135
135
  attr_reader :long_name
@@ -152,7 +152,35 @@ describe ActiveRecord::MySQL::Strict::Mixin do
152
152
  end
153
153
 
154
154
  subject { model.new(long_name: '*' * 10) }
155
+ its(:long_name) { should eql '*' * 400 }
155
156
  it { should be_valid }
156
157
  end
158
+
159
+ context 'for invalid model with accessor that returns a valid string' do
160
+ let(:model) do
161
+ strict_model 'User' do
162
+ attr_reader :short_name
163
+
164
+ # When we call `#short_name` it will return a valid string but
165
+ # we want the attribute to store the real, valid value because we
166
+ # want to actually make sure that the real attribute value does
167
+ # exceed the limit, not what the accessor returns.
168
+ define_method :short_name= do |value|
169
+ @short_name = '*' * 100
170
+ write_attribute(:short_name, value)
171
+ end
172
+ end
173
+ end
174
+
175
+ before do
176
+ run_migration do
177
+ create_table(:users, force: true) { |t| t.string :short_name }
178
+ end
179
+ end
180
+
181
+ subject { model.new(short_name: '*' * 400) }
182
+ its(:short_name) { should eql '*' * 100 }
183
+ it { should_not be_valid }
184
+ end
157
185
  end
158
186
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::MySQL::Strict do
4
+ describe :validates_strict_columns do
5
+ before do
6
+ spawn_model 'User'
7
+ run_migration do
8
+ create_table(:users, force: true) { |t| t.string :name }
9
+ end
10
+ end
11
+
12
+ context 'without options' do
13
+ before do
14
+ expect(ActiveRecord::MySQL::Strict::Validations).to receive(:define_mysql_strict_validations).with(User, {})
15
+ end
16
+
17
+ it { User.validates_strict_columns }
18
+ end
19
+
20
+ context 'with options' do
21
+ before do
22
+ expect(ActiveRecord::MySQL::Strict::Validations).to receive(:define_mysql_strict_validations).with(User, { except: [:bar], only: [:foo] })
23
+ end
24
+
25
+ it { User.validates_strict_columns except: [:bar], only: [:foo] }
26
+ end
27
+ end
28
+ end
@@ -1,7 +1,7 @@
1
1
  module ModelMacros
2
2
  # Create a new microscope model
3
3
  def strict_model(klass_name, options = {}, &block)
4
- spawn_model klass_name, ActiveRecord::Base do
4
+ spawn_model klass_name do
5
5
  validates_strict_columns options
6
6
  instance_exec(&block) if block
7
7
  end
@@ -10,7 +10,7 @@ module ModelMacros
10
10
  protected
11
11
 
12
12
  # Create a new model class
13
- def spawn_model(klass_name, parent_klass, &block)
13
+ def spawn_model(klass_name, parent_klass = ActiveRecord::Base, &block)
14
14
  Object.instance_eval { remove_const klass_name } if Object.const_defined?(klass_name)
15
15
  Object.const_set(klass_name, Class.new(parent_klass))
16
16
  Object.const_get(klass_name).class_eval(&block) if block_given?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_mysql_strict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -140,11 +140,12 @@ files:
140
140
  - activerecord_mysql_strict.gemspec
141
141
  - gemfiles/Gemfile.activerecord-3.2.x
142
142
  - gemfiles/Gemfile.activerecord-4.0
143
- - lib/active_record/mysql/strict/mixin.rb
144
143
  - lib/active_record/mysql/strict/strict_length_validator.rb
144
+ - lib/active_record/mysql/strict/validations.rb
145
145
  - lib/active_record/mysql/strict/version.rb
146
146
  - lib/activerecord_mysql_strict.rb
147
- - spec/activerecord_mysql_strict/mixin_spec.rb
147
+ - spec/activerecord_mysql_strict/validations_spec.rb
148
+ - spec/activerecord_mysql_strict_spec.rb
148
149
  - spec/spec_helper.rb
149
150
  - spec/support/macros/database_macros.rb
150
151
  - spec/support/macros/model_macros.rb
@@ -163,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
164
  version: '0'
164
165
  segments:
165
166
  - 0
166
- hash: -369873074256560002
167
+ hash: -2194545616864898002
167
168
  required_rubygems_version: !ruby/object:Gem::Requirement
168
169
  none: false
169
170
  requirements:
@@ -172,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
173
  version: '0'
173
174
  segments:
174
175
  - 0
175
- hash: -369873074256560002
176
+ hash: -2194545616864898002
176
177
  requirements: []
177
178
  rubyforge_project:
178
179
  rubygems_version: 1.8.23
@@ -181,7 +182,8 @@ specification_version: 3
181
182
  summary: ActiveRecord::MySQL::Strict adds validations to ActiveRecord models to make
182
183
  sure they do not trigger errors in MySQL strict mode.
183
184
  test_files:
184
- - spec/activerecord_mysql_strict/mixin_spec.rb
185
+ - spec/activerecord_mysql_strict/validations_spec.rb
186
+ - spec/activerecord_mysql_strict_spec.rb
185
187
  - spec/spec_helper.rb
186
188
  - spec/support/macros/database_macros.rb
187
189
  - spec/support/macros/model_macros.rb
@@ -1,46 +0,0 @@
1
- module ActiveRecord
2
- module MySQL
3
- module Strict
4
- # Constants
5
- MYSQL_STRICT_STRING_LIMIT = 255
6
- MYSQL_STRICT_TEXT_LIMIT = 65535
7
- MYSQL_STRICT_INTEGER_LIMIT = 2147483647
8
-
9
- module Mixin
10
- extend ActiveSupport::Concern
11
-
12
- included do
13
- define_mysql_strict_validations
14
- end
15
-
16
- module ClassMethods
17
- def define_mysql_strict_validations
18
- except = @mysql_strict_options[:except] || []
19
- model_columns = self.columns.dup.reject { |c| except.include?(c.name.to_sym) }
20
-
21
- if only = @mysql_strict_options[:only]
22
- model_columns = model_columns.select { |c| only.include?(c.name.to_sym) }
23
- end
24
-
25
- model_columns.each do |field|
26
- method = :"define_mysql_strict_#{field.type}_validation"
27
- send(method, field) if respond_to?(method)
28
- end
29
- end
30
-
31
- def define_mysql_strict_string_validation(field)
32
- validates field.name, 'ActiveRecord::MySQL::Strict::StrictLength' => { in: 0..(field.limit || MYSQL_STRICT_STRING_LIMIT) }, allow_blank: true
33
- end
34
-
35
- def define_mysql_strict_text_validation(field)
36
- validates field.name, 'ActiveRecord::MySQL::Strict::StrictLength' => { in: 0..(field.limit || MYSQL_STRICT_TEXT_LIMIT) }, allow_blank: true
37
- end
38
-
39
- def define_mysql_strict_integer_validation(field)
40
- validates field.name, numericality: { greather_than_or_equal_to: -MYSQL_STRICT_INTEGER_LIMIT, less_than_or_equal_to: MYSQL_STRICT_INTEGER_LIMIT }, allow_blank: true
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end