tram-validators 0.0.4 → 0.0.5

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: ec92d7c68a5e2dbfa32186ae6c1c36ce9cc1664d
4
- data.tar.gz: 66e7694f05340e8ca51656599b0dfd9fbfb2f821
3
+ metadata.gz: f890e67e5985736d2fb52569af92743acd02f021
4
+ data.tar.gz: '0952d74dcd81759ce8b04118b053758452f86f8f'
5
5
  SHA512:
6
- metadata.gz: f09e4fc491253e62c2c34cdefa8f0ca27f73098e6cd83f887266913dc47d5a49c6d2b9721a695a9f97f1d7e09ac5f92a4e83ef504fdfe8a167a5fe6b9ec3b4d7
7
- data.tar.gz: 0ee2f8fbf636a5e993cc3e452e4914ba34711cd4de2573bfe8f252c4ba270055c59eb47d00d0dd6ed8deaf8cefe1c9331359ee154f335b473d0e6f47875c067d
6
+ metadata.gz: c8a72c30378c53d742caa9069adc4b77fab8c9bd144bd81d6649f4073000bc8041b8e51a65e8bba50ccbc5123572de02bedf13e82ad660855114f7350b0460e7
7
+ data.tar.gz: cb5e2e9ff8a40adef4f072a558e2b6e187124e4b128bd12f85d9e8bfde74ccf13599a403c3ccc1ccfd7b572767894227101bd88c99dd21ab90cc1022483940c3
@@ -0,0 +1,12 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-02-06 13:19:54 +0300 using RuboCop version 0.47.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: CountComments.
11
+ Metrics/MethodLength:
12
+ Max: 12
@@ -1,3 +1,10 @@
1
+ # v0.0.5 (2017-02-06)
2
+
3
+ ## Bug fixes
4
+ - Error messages from outcom validators (nepalez)
5
+
6
+ [Compare v0.0.4...v0.0.5](https://github.com/tram-rb/tram-validators/compare/v0.0.4...v0.0.5)
7
+
1
8
  # v0.0.4 (2017-01-30)
2
9
 
3
10
  ## Bug fixes
@@ -0,0 +1,12 @@
1
+ ---
2
+ en:
3
+ errors:
4
+ messages:
5
+ not_validatable: "cannot be validated"
6
+ not_found: "refers to no record of %{klass} by %{key}: %{value}"
7
+ size_equal_to: "has size %{size} other than %{limit}"
8
+ size_other_than: "has size equal to %{limit}"
9
+ size_greater_than: "has size %{size} which isn't greater than %{limit}"
10
+ size_greater_than_or_equal_to: "has size %{size} which is less than %{limit}"
11
+ size_less_than: "has size %{size} which isn't less than %{limit}"
12
+ size_less_than_or_equal_to: "has size %{size} which is greater than %{limit}"
@@ -0,0 +1,13 @@
1
+ ---
2
+ ru:
3
+ errors:
4
+ messages:
5
+ invalid: "имеет некорректное значение"
6
+ not_validatable: "не может быть проверен"
7
+ not_found: "не соответствует объекту класса %{klass} с идентификатором %{key}: %{value}"
8
+ size_equal_to: "имеет размер %{size} отличный от %{limit}"
9
+ size_other_than: "имеет размер равный %{limit}"
10
+ size_greater_than: "имеет размер %{size} не превышающий %{limit}"
11
+ size_greater_than_or_equal_to: "имеет размер %{size} меньший %{limit}"
12
+ size_less_than: "имеет размер %{size} не меньший %{limit}"
13
+ size_less_than_or_equal_to: "имеет размер %{size} превышающий %{limit}"
@@ -77,3 +77,7 @@ module Tram
77
77
  require_relative "validators/validity_validator"
78
78
  end
79
79
  end
80
+
81
+ Dir["#{File.dirname(__FILE__)}/locales/*.yml}"].each do |file|
82
+ I18n.load_path << file
83
+ end
@@ -21,6 +21,9 @@ class ConsistencyValidator < ActiveModel::EachValidator
21
21
  return if value && other_value && yield(value, other_value)
22
22
 
23
23
  error_name = [condition, chain.to_s.split(".")].flatten.join("_").to_sym
24
- record.errors.add attribute, error_name, value: value, other: other_value
24
+ record.errors.add attribute, error_name, model: record,
25
+ attribute: attribute,
26
+ value: value,
27
+ other: other_value
25
28
  end
26
29
  end
@@ -11,6 +11,7 @@
11
11
  # # user_id.user_name_presence
12
12
  #
13
13
  class OutcomeValidator < ActiveModel::EachValidator
14
+ # rubocop: disable Metrics/MethodLength
14
15
  def validate_each(record, attribute, value)
15
16
  dependency = options[:value].to_s.gsub(".", "_")
16
17
  dependent = Tram::Validators.chained_value(record, options[:value])
@@ -20,11 +21,14 @@ class OutcomeValidator < ActiveModel::EachValidator
20
21
  validators.each do |condition, validator|
21
22
  next if valid_in_sandbox(sandbox, attribute, dependent, validator)
22
23
 
23
- key = message_key(dependency, condition)
24
- text = message(record, attribute, value, dependent, key)
25
- record.errors.add attribute, text
24
+ key = message_key(dependency, condition)
25
+ record.errors.add attribute, key, model: record,
26
+ attribute: attribute,
27
+ value: value,
28
+ delependent: dependent
26
29
  end
27
30
  end
31
+ # rubocop: enable Metrics/MethodLength
28
32
 
29
33
  private
30
34
 
@@ -37,14 +41,4 @@ class OutcomeValidator < ActiveModel::EachValidator
37
41
  def message_key(dependency, condition)
38
42
  [dependency, condition.to_s].compact.join("_").to_sym
39
43
  end
40
-
41
- def message(record, attribute, value, dependent, message_key)
42
- model = record.class.name.underscore
43
- scope = %W(active_model errors models #{model} attributes #{attribute})
44
- I18n.t message_key, record: record,
45
- attribute: attribute,
46
- value: value,
47
- delependent: dependent,
48
- scope: scope
49
- end
50
44
  end
@@ -6,9 +6,14 @@
6
6
  class ReferenceValidator < ActiveModel::EachValidator
7
7
  def validate_each(record, attribute, value)
8
8
  model = options.fetch(:model) { raise "You should define :model option" }
9
+ model = model.constantize if model.is_a? String
9
10
  key = options.fetch(:find_by, :id)
10
11
 
11
12
  return if model.find_by(key => value)
12
- record.errors.add attribute, :not_found
13
+ record.errors.add attribute, :not_found, model: record,
14
+ attribute: attribute,
15
+ value: value,
16
+ klass: model.name,
17
+ key: key
13
18
  end
14
19
  end
@@ -8,19 +8,23 @@ class SizeValidator < ActiveModel::EachValidator
8
8
  def validate_each(record, attribute, value)
9
9
  size = value.size if value.respond_to? :size
10
10
  Tram::Validators::CONDITIONS.each do |key, block|
11
- check(key, record, attribute, size, &block)
11
+ check(key, record, attribute, value, size, &block)
12
12
  end
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def check(condition, record, attribute, size)
17
+ def check(condition, record, attribute, value, size)
18
18
  return unless options.key? condition
19
19
 
20
20
  limit = extract_limit(record, condition)
21
21
  return if size && limit && yield(size, limit.to_i)
22
22
 
23
- record.errors.add attribute, error_key(condition), size: size, limit: limit
23
+ record.errors.add attribute, error_key(condition), model: record,
24
+ attribute: attribute,
25
+ value: value,
26
+ size: size,
27
+ limit: limit
24
28
  end
25
29
 
26
30
  def extract_limit(record, condition)
@@ -14,10 +14,11 @@
14
14
  class ValidityValidator < ActiveModel::EachValidator
15
15
  def validate_each(record, key, value)
16
16
  if !value.respond_to? :valid?
17
- record.errors
18
- .add attribute, :valid, record: record, attribute: key, value: value
17
+ record.errors.add attribute, :not_validatable, model: record,
18
+ attribute: key,
19
+ value: value
19
20
  elsif !value.valid?
20
- Tram::Validators.copy_errors(value, record, key, :valid, value, options)
21
+ Tram::Validators.copy_errors(value, record, key, :invalid, value, options)
21
22
  end
22
23
  end
23
24
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "tram-validators"
3
- gem.version = "0.0.4"
3
+ gem.version = "0.0.5"
4
4
  gem.author = "Andrew Kozin (nepalez)"
5
5
  gem.email = "andrew.kozin@gmail.com"
6
6
  gem.homepage = "https://github.com/tram-rb/tram-validators"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tram-validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin (nepalez)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-30 00:00:00.000000000 Z
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -79,6 +79,7 @@ files:
79
79
  - ".gitignore"
80
80
  - ".rspec"
81
81
  - ".rubocop.yml"
82
+ - ".rubocop_todo.yml"
82
83
  - ".travis.yml"
83
84
  - CHANGELOG.md
84
85
  - Gemfile
@@ -86,6 +87,8 @@ files:
86
87
  - README.md
87
88
  - Rakefile
88
89
  - lib/tram-validators.rb
90
+ - lib/tram/locales/en.yml
91
+ - lib/tram/locales/ru.yml
89
92
  - lib/tram/validators.rb
90
93
  - lib/tram/validators/consistency_validator.rb
91
94
  - lib/tram/validators/contract_validator.rb