valhammer 0.3.1 → 1.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
- SHA1:
3
- metadata.gz: 758eec1c687a507398055fdb46d849004962b1b6
4
- data.tar.gz: bda97ef82eb60f40da4789514710c122897a35ab
2
+ SHA256:
3
+ metadata.gz: 5a5c5adbed9b66cb80eb994a6d9595a56cbf1de28e0f105b5440687a53805c63
4
+ data.tar.gz: b94ce0e81bfe18c2ba9f85967aa29b0facc3433357694304189d8e1c9ce43d2d
5
5
  SHA512:
6
- metadata.gz: 6a8dc0e51c7b2ebb78fc0a6536be61a49a7b66257807f6dd901e28736631b755c7fdebd4639d350780862af0f8de2fce2846b7a1e55566b435aaee018e1c83a7
7
- data.tar.gz: af035d6d7dab4931dbbabe1fa1602b30fd98610e90b2ec46f8a631749b4ae734caf1774afec8f7f57f495602a6c68a1a534ca7d7aa5466713303630c06a42c89
6
+ metadata.gz: 38d4db5e578d88ea9ad8f23a411a0a1edfa32366fdbbcc3ced400c6414750dfcb8d929b6ae29a9a46b898d2596217e1784c446eca8f1bf42c1feb818c58d2518
7
+ data.tar.gz: 83898470fcc987c4f81d71b2d302ff0f327dd19241b04bd3afd3bd45003bedcf3b22b898767a6ce28f73ae643c0fe9ad031529d58f334cf3899004405fedc8e4
data/README.md CHANGED
@@ -19,10 +19,8 @@
19
19
 
20
20
  Automatically validate ActiveRecord models based on the database schema.
21
21
 
22
- Author: Shaun Mangelsdorf
23
-
24
22
  ```
25
- Copyright 2015, Australian Access Federation
23
+ Copyright 2015-2016, Australian Access Federation
26
24
 
27
25
  Licensed under the Apache License, Version 2.0 (the "License");
28
26
  you may not use this file except in compliance with the License.
@@ -82,6 +80,11 @@ Generated validations are:
82
80
  * `:length` — added to `string` columns to ensure the value fits in the
83
81
  column
84
82
 
83
+ **SQLite Note:** In SQLite, a `string` column has no default length restriction
84
+ (except for the [hard limit on data size set at compile
85
+ time](https://www.sqlite.org/limits.html)). Valhammer will not apply a `length`
86
+ validation unless the column was created with an explicit limit.
87
+
85
88
  ### Disabling Validators
86
89
 
87
90
  Passing a block to `valhammer` allows some selective calls to `disable` to
@@ -261,6 +264,14 @@ end
261
264
  In this case, it is not possible for valhammer to determine the behaviour of the
262
265
  `where` clause, so the validation must be manually created.
263
266
 
267
+ ## Logging
268
+
269
+ To make Valhammer tell you exactly what it's doing, turn on verbose mode:
270
+
271
+ ```ruby
272
+ Valhammer.config.verbose = true
273
+ ```
274
+
264
275
  ## Contributing
265
276
 
266
277
  Refer to [GitHub Flow](https://guides.github.com/introduction/flow/) for
@@ -0,0 +1,14 @@
1
+ require 'singleton'
2
+
3
+ module Valhammer
4
+ class Configuration
5
+ include Singleton
6
+
7
+ def initialize
8
+ @verbose = false
9
+ end
10
+
11
+ attr_accessor :verbose
12
+ alias verbose? verbose
13
+ end
14
+ end
@@ -64,8 +64,6 @@ module Valhammer
64
64
  end
65
65
 
66
66
  def valhammer_validations(column, opts)
67
- logger.debug("Valhammer generating options for #{valhammer_info(column)}")
68
-
69
67
  validations = {}
70
68
  valhammer_presence(validations, column, opts)
71
69
  valhammer_inclusion(validations, column, opts)
@@ -73,8 +71,10 @@ module Valhammer
73
71
  valhammer_numeric(validations, column, opts)
74
72
  valhammer_length(validations, column, opts)
75
73
 
76
- logger.debug("Valhammer options for #{valhammer_log_key(column)} " \
77
- "are: #{validations.inspect}")
74
+ if Valhammer.config.verbose?
75
+ logger.debug("Valhammer options for #{valhammer_log_key(column)} " \
76
+ "are: #{validations.inspect}")
77
+ end
78
78
  validations
79
79
  end
80
80
 
@@ -97,13 +97,13 @@ module Valhammer
97
97
  return unless unique_keys.one?
98
98
 
99
99
  scope = unique_keys.first.columns[0..-2]
100
- validations[:uniqueness] = valhammer_unique_opts(scope)
100
+ validations[:uniqueness] = valhammer_unique_opts(column, scope)
101
101
  end
102
102
 
103
- def valhammer_unique_opts(scope)
103
+ def valhammer_unique_opts(column, scope)
104
104
  nullable = scope.select { |c| columns_hash[c].null }
105
-
106
- opts = { allow_nil: true }
105
+ opts = { allow_nil: true,
106
+ case_sensitive: case_sensitive?(column) }
107
107
  opts[:scope] = scope.map(&:to_sym) if scope.any?
108
108
  opts[:if] = -> { nullable.all? { |c| send(c) } } if nullable.any?
109
109
  opts
@@ -150,5 +150,11 @@ module Valhammer
150
150
  reflect_on_all_associations(:belongs_to)
151
151
  .find { |a| a.foreign_key == field }.try(:name)
152
152
  end
153
+
154
+ def case_sensitive?(column)
155
+ return true unless column.respond_to?(:case_sensitive?)
156
+
157
+ column.case_sensitive?
158
+ end
153
159
  end
154
160
  end
@@ -1,3 +1,3 @@
1
1
  module Valhammer
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/lib/valhammer.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'valhammer/version'
2
+ require 'valhammer/configuration'
3
+ require 'valhammer/validations'
4
+ require 'valhammer/railtie' if defined?(Rails::Railtie)
2
5
 
3
6
  module Valhammer
7
+ def self.config
8
+ Configuration.instance
9
+ end
4
10
  end
5
-
6
- require 'valhammer/validations'
7
- require 'valhammer/railtie' if defined?(Rails::Railtie)
@@ -291,6 +291,58 @@ RSpec.describe Valhammer::Validations do
291
291
  end
292
292
  end
293
293
 
294
+ describe 'logging' do
295
+ before do
296
+ hammerable =
297
+ Class.new(ActiveRecord::Base) do
298
+ self.table_name = :organisations
299
+ end
300
+
301
+ def self.logger
302
+ # Hook for specs to spy on log output
303
+ end
304
+
305
+ stub_const 'Hammerable', hammerable
306
+ end
307
+
308
+ context 'when verbose' do
309
+ around do |example|
310
+ Valhammer.config.verbose = true
311
+ example.run
312
+ ensure
313
+ Valhammer.config.verbose = false
314
+ end
315
+
316
+ it 'logs information about validations' do
317
+ logger = spy(:debug)
318
+ allow(Hammerable).to receive(:logger).and_return(logger)
319
+
320
+ Hammerable.valhammer
321
+
322
+ expect(logger).to have_received(:debug).with(
323
+ 'Valhammer options for `organisations`.`name` are: {:presence=>true}'
324
+ )
325
+ expect(logger).to have_received(:debug).with(
326
+ 'Valhammer options for `organisations`.`country` are: {:presence=>true}'
327
+ )
328
+ expect(logger).to have_received(:debug).with(
329
+ 'Valhammer options for `organisations`.`city` are: {:presence=>true}'
330
+ )
331
+ end
332
+ end
333
+
334
+ context 'when quiet' do
335
+ it 'does not log' do
336
+ logger = spy(:debug)
337
+ allow(Hammerable).to receive(:logger).and_return(logger)
338
+
339
+ Hammerable.valhammer
340
+
341
+ expect(logger).not_to have_received(:debug)
342
+ end
343
+ end
344
+ end
345
+
294
346
  context 'sanity check' do
295
347
  let(:organisation) do
296
348
  Organisation.create!(name: 'Enhanced Collaborative Methodologies Pty Ltd',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valhammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Mangelsdorf
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-11 00:00:00.000000000 Z
11
+ date: 2023-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -192,7 +192,7 @@ dependencies:
192
192
  - - ">="
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
- description:
195
+ description:
196
196
  email:
197
197
  - s.mangelsdorf@gmail.com
198
198
  executables: []
@@ -209,6 +209,7 @@ files:
209
209
  - README.md
210
210
  - Rakefile
211
211
  - lib/valhammer.rb
212
+ - lib/valhammer/configuration.rb
212
213
  - lib/valhammer/railtie.rb
213
214
  - lib/valhammer/validations.rb
214
215
  - lib/valhammer/version.rb
@@ -229,7 +230,7 @@ homepage: https://github.com/ausaccessfed/valhammer
229
230
  licenses:
230
231
  - Apache-2.0
231
232
  metadata: {}
232
- post_install_message:
233
+ post_install_message:
233
234
  rdoc_options: []
234
235
  require_paths:
235
236
  - lib
@@ -244,9 +245,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
245
  - !ruby/object:Gem::Version
245
246
  version: '0'
246
247
  requirements: []
247
- rubyforge_project:
248
- rubygems_version: 2.5.1
249
- signing_key:
248
+ rubygems_version: 3.3.7
249
+ signing_key:
250
250
  specification_version: 4
251
251
  summary: Automatically validate ActiveRecord models based on the database schema.
252
252
  test_files: