validates_timeliness 7.0.0.beta1 → 7.0.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
  SHA256:
3
- metadata.gz: 284d18473a0007c273fc4608579b619afac6a3809dfdcd12864d6aeea30c6f17
4
- data.tar.gz: 12d2db9663bb6c80a643d7c7365fc4d44218a64a9b69feacb6d060dc737ddd93
3
+ metadata.gz: 506a3b1b6bf8487da01734e154c8bbd8c4afd5ab308dc11b4928b26c30d1a658
4
+ data.tar.gz: 3c380ad2708dc2946a274c473451bb1db6c1d71d0e5da462b658dad735cc9560
5
5
  SHA512:
6
- metadata.gz: 77eb167f597fe224c293b6a9e9143c0b5eb8b443ed54539dc89e34c28e5cb7c9517e826c25660641281d6b477451cb2ff4b1187f0332e33fd0d94d7618223204
7
- data.tar.gz: de5c7cb989b1148acf4a7af524171c7eead44663b43088da99f79bb5af7c7b1c120f28ace62cb62bcfe533979fbe9975763e59aa463acccf7ed1a951496e1f39
6
+ metadata.gz: 2ef5523111dee8a055989d0fa7c751d6dc34e959e2442ef99b0b46050097a31983a350ab4d6a1d3bc00ac4ebc4d00320622c9cc7aaad799535bd6dcb2084849f
7
+ data.tar.gz: b15a9e6cd84b59830b45a3bce59d9cc664b2a0a5504423a355cea340614ae2a136a41ee37fc180f00b70398cd50deb67d20a1459165945fccc28eb2d79ee73e2
@@ -14,6 +14,22 @@ jobs:
14
14
  - gemfile: rails_7_0
15
15
  ruby: 3.1
16
16
 
17
+ - gemfile: rails_7_1
18
+ ruby: 2.7
19
+ - gemfile: rails_7_1
20
+ ruby: 3.0
21
+ - gemfile: rails_7_1
22
+ ruby: 3.1
23
+ - gemfile: rails_7_1
24
+ ruby: 3.2
25
+
26
+ - gemfile: rails_7_2
27
+ ruby: 3.1
28
+ - gemfile: rails_7_2
29
+ ruby: 3.2
30
+ - gemfile: rails_7_2
31
+ ruby: 3.3
32
+
17
33
  name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
18
34
  env:
19
35
  BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
data/README.md CHANGED
@@ -30,7 +30,7 @@ Older Rails versions:
30
30
 
31
31
  In Gemfile
32
32
  ```ruby
33
- gem 'validates_timeliness', '~> 7.0.0.beta1'
33
+ gem 'validates_timeliness', '~> 7.0.0'
34
34
  ```
35
35
 
36
36
  Run bundler:
@@ -1,13 +1,13 @@
1
1
  # This file was generated by Appraisal
2
2
 
3
- source "http://rubygems.org"
3
+ source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 7.0.0"
6
6
  gem "rspec"
7
7
  gem "rspec-rails", "~> 6.0"
8
- gem "sqlite3"
8
+ gem "sqlite3", "~> 1.4"
9
9
  gem "byebug"
10
10
  gem "appraisal"
11
- gem "nokogiri", "~> 1.8"
11
+ gem "nokogiri"
12
12
 
13
13
  gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 7.1.0"
6
+ gem "rspec"
7
+ gem "rspec-rails", "~> 6.0"
8
+ gem "sqlite3", "~> 1.4"
9
+ gem "byebug"
10
+ gem "appraisal"
11
+ gem "nokogiri"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 7.2.0"
6
+ gem "rspec"
7
+ gem "rspec-rails", "~> 6.0"
8
+ gem "sqlite3", "~> 1.4"
9
+ gem "byebug"
10
+ gem "appraisal"
11
+ gem "nokogiri"
12
+
13
+ gemspec path: "../"
@@ -1,6 +1,6 @@
1
1
  module ValidatesTimeliness
2
2
  module Extensions
3
- module TimelinessDateTimeSelect
3
+ module DateTimeSelect
4
4
  # Intercepts the date and time select helpers to reuse the values from
5
5
  # the params rather than the parsed value. This allows invalid date/time
6
6
  # values to be redisplayed instead of blanks to aid correction by the user.
@@ -1,10 +1,11 @@
1
1
  module ValidatesTimeliness
2
2
  module Extensions
3
- autoload :TimelinessDateTimeSelect, 'validates_timeliness/extensions/date_time_select'
3
+ autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select'
4
4
  end
5
5
 
6
6
  def self.enable_date_time_select_extension!
7
- ::ActionView::Helpers::Tags::DateSelect.send(:prepend, ValidatesTimeliness::Extensions::TimelinessDateTimeSelect)
7
+ require 'uri' # Do we need this? No, but the test suite fails without it.
8
+ ::ActionView::Helpers::Tags::DateSelect.send(:prepend, ValidatesTimeliness::Extensions::DateTimeSelect)
8
9
  end
9
10
 
10
11
  def self.enable_multiparameter_extension!
@@ -3,25 +3,19 @@ module ActiveModel
3
3
 
4
4
  module HelperMethods
5
5
  def validates_date(*attr_names)
6
- timeliness_validation_for attr_names, :date
6
+ validates_with TimelinessValidator, _merge_attributes(attr_names).merge(type: :date)
7
7
  end
8
8
 
9
9
  def validates_time(*attr_names)
10
- timeliness_validation_for attr_names, :time
10
+ validates_with TimelinessValidator, _merge_attributes(attr_names).merge(type: :time)
11
11
  end
12
12
 
13
13
  def validates_datetime(*attr_names)
14
- timeliness_validation_for attr_names, :datetime
14
+ validates_with TimelinessValidator, _merge_attributes(attr_names).merge(type: :datetime)
15
15
  end
16
16
 
17
17
  def validates_timeliness_of(*attr_names)
18
- timeliness_validation_for attr_names
19
- end
20
-
21
- def timeliness_validation_for(attr_names, type=nil)
22
- options = _merge_attributes(attr_names)
23
- options.update(:type => type) if type
24
- validates_with TimelinessValidator, options
18
+ validates_with TimelinessValidator, _merge_attributes(attr_names)
25
19
  end
26
20
  end
27
21
 
@@ -3,67 +3,8 @@ module ValidatesTimeliness
3
3
  module ActiveModel
4
4
  extend ActiveSupport::Concern
5
5
 
6
- module ClassMethods
7
- public
8
-
9
- def define_attribute_methods(*attr_names)
10
- super.tap { define_timeliness_methods }
11
- end
12
-
13
- def undefine_attribute_methods
14
- super.tap { undefine_timeliness_attribute_methods }
15
- end
16
-
17
- def define_timeliness_methods(before_type_cast=false)
18
- return if timeliness_validated_attributes.blank?
19
- timeliness_validated_attributes.each do |attr_name|
20
- define_attribute_timeliness_methods(attr_name, before_type_cast)
21
- end
22
- end
23
-
24
- def generated_timeliness_methods
25
- @generated_timeliness_methods ||= Module.new { |m|
26
- extend Mutex_m
27
- }.tap { |mod| include mod }
28
- end
29
-
30
- def undefine_timeliness_attribute_methods
31
- generated_timeliness_methods.module_eval do
32
- instance_methods.each { |m| undef_method(m) }
33
- end
34
- end
35
-
36
- def define_attribute_timeliness_methods(attr_name, before_type_cast=false)
37
- define_timeliness_write_method(attr_name)
38
- define_timeliness_before_type_cast_method(attr_name) if before_type_cast
39
- end
40
-
41
- def define_timeliness_write_method(attr_name)
42
- generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
43
- def #{attr_name}=(value)
44
- @timeliness_cache ||= {}
45
- @timeliness_cache['#{attr_name}'] = value
46
-
47
- @attributes['#{attr_name}'] = super
48
- end
49
- STR
50
- end
51
-
52
- def define_timeliness_before_type_cast_method(attr_name)
53
- generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
54
- def #{attr_name}_before_type_cast
55
- read_timeliness_attribute_before_type_cast('#{attr_name}')
56
- end
57
- STR
58
- end
59
- end
60
-
61
6
  def read_timeliness_attribute_before_type_cast(attr_name)
62
- @timeliness_cache && @timeliness_cache[attr_name] || @attributes[attr_name]
63
- end
64
-
65
- def _clear_timeliness_cache
66
- @timeliness_cache = {}
7
+ @attributes[attr_name].value_before_type_cast
67
8
  end
68
9
 
69
10
  end
@@ -95,8 +95,9 @@ module ValidatesTimeliness
95
95
  end
96
96
 
97
97
  def attribute_raw_value(record, attr_name)
98
- record.respond_to?(:read_timeliness_attribute_before_type_cast) &&
98
+ if record.respond_to?(:read_timeliness_attribute_before_type_cast)
99
99
  record.read_timeliness_attribute_before_type_cast(attr_name.to_s)
100
+ end
100
101
  end
101
102
 
102
103
  def time_zone_aware?(record, attr_name)
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '7.0.0.beta1'
2
+ VERSION = '7.0.0'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -33,8 +33,6 @@ I18n.available_locales = ['en', 'es']
33
33
  # Extend TestModel as you would another ORM/ODM module
34
34
  module TestModelShim
35
35
  extend ActiveSupport::Concern
36
- include ValidatesTimeliness::AttributeMethods
37
- include ValidatesTimeliness::ORM::ActiveModel
38
36
 
39
37
  module ClassMethods
40
38
  # Hook into native time zone handling check, if any
@@ -46,11 +44,10 @@ end
46
44
 
47
45
  class Person
48
46
  include TestModel
47
+
49
48
  attribute :birth_date, :date
50
49
  attribute :birth_time, :time
51
50
  attribute :birth_datetime, :datetime
52
-
53
- define_attribute_methods model_attributes.keys
54
51
  end
55
52
 
56
53
  class PersonWithShim < Person
@@ -1,22 +1,29 @@
1
1
  module ModelHelpers
2
2
 
3
3
  # Some test helpers from Rails source
4
- def invalid!(attr_name, values, error = nil)
5
- with_each_person_value(attr_name, values) do |record, value|
6
- expect(record).to be_invalid
4
+ def invalid!(attr_name, values, error = nil, model_class = Person)
5
+ with_each_model_value(attr_name, values, model_class) do |record, value|
6
+ expect(record).to_not be_valid
7
7
  expect(record.errors[attr_name].size).to be >= 1
8
- expect(record.errors[attr_name].first).to eq(error) if error
8
+
9
+ return unless error
10
+
11
+ if error.is_a?(Regexp)
12
+ expect(record.errors[attr_name].first).to match(error)
13
+ else
14
+ expect(record.errors[attr_name].first).to eq(error)
15
+ end
9
16
  end
10
17
  end
11
18
 
12
- def valid!(attr_name, values)
13
- with_each_person_value(attr_name, values) do |record, value|
19
+ def valid!(attr_name, values, model_class = Person)
20
+ with_each_model_value(attr_name, values, model_class) do |record, value|
14
21
  expect(record).to be_valid
15
22
  end
16
23
  end
17
24
 
18
- def with_each_person_value(attr_name, values)
19
- record = Person.new
25
+ def with_each_model_value(attr_name, values, model_class)
26
+ record = model_class.new
20
27
  Array.wrap(values).each do |value|
21
28
  record.send("#{attr_name}=", value)
22
29
  yield record, value
@@ -1,61 +1,13 @@
1
1
  module TestModel
2
2
  extend ActiveSupport::Concern
3
- extend ActiveModel::Translation
3
+ include ActiveModel::Model
4
+ include ActiveModel::Attributes
4
5
  include ActiveModel::Validations
5
- include ActiveModel::AttributeMethods
6
+ include ValidatesTimeliness::AttributeMethods
7
+ include ValidatesTimeliness::ORM::ActiveModel
6
8
 
7
9
  included do
8
10
  attribute_method_suffix "="
9
- cattr_accessor :model_attributes
10
- end
11
-
12
- module ClassMethods
13
- def attribute(name, type)
14
- self.model_attributes ||= {}
15
- self.model_attributes[name] = type
16
- end
17
-
18
- def define_method_attribute=(attr_name, owner: nil)
19
- generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast('#{attr_name}', new_value); end", __FILE__, __LINE__)
20
- end
21
-
22
- def define_method_attribute(attr_name, owner: nil)
23
- generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__)
24
- end
25
-
26
- def type_cast(attr_name, value)
27
- return value unless value.is_a?(String)
28
- type_name = model_attributes[attr_name.to_sym]
29
- type = ActiveModel::Type.lookup(type_name)
30
- type.cast(value)
31
- end
32
- end
33
-
34
- def initialize(attributes = nil)
35
- @attributes = self.class.model_attributes.keys.inject({}) do |hash, column|
36
- hash[column.to_s] = nil
37
- hash
38
- end
39
- self.attributes = attributes unless attributes.nil?
40
- end
41
-
42
- def attributes
43
- @attributes
44
- end
45
-
46
- def attributes=(new_attributes={})
47
- new_attributes.each do |key, value|
48
- send "#{key}=", value
49
- end
50
- end
51
-
52
- def method_missing(method_id, *args, &block)
53
- if !matched_attribute_method(method_id.to_s).nil?
54
- self.class.define_attribute_methods self.class.model_attributes.keys
55
- send(method_id, *args, &block)
56
- else
57
- super
58
- end
59
11
  end
60
12
  end
61
13
 
@@ -44,9 +44,11 @@ RSpec.describe ValidatesTimeliness::AttributeMethods do
44
44
  class PersonWithParser
45
45
  include TestModel
46
46
  include TestModelShim
47
+
47
48
  attribute :birth_date, :date
48
49
  attribute :birth_time, :time
49
50
  attribute :birth_datetime, :datetime
51
+
50
52
  validates_date :birth_date
51
53
  validates_time :birth_time
52
54
  validates_datetime :birth_datetime
@@ -54,8 +56,10 @@ RSpec.describe ValidatesTimeliness::AttributeMethods do
54
56
 
55
57
  it 'should parse a string value' do
56
58
  expect(Timeliness::Parser).to receive(:parse)
59
+
57
60
  r = PersonWithParser.new
58
61
  r.birth_date = '2010-01-01'
62
+ r.birth_date # parsing happens on read not write
59
63
  end
60
64
 
61
65
  end
@@ -0,0 +1,57 @@
1
+ RSpec.describe ValidatesTimeliness::Validator, ":format option" do
2
+ with_config(:use_plugin_parser, true)
3
+
4
+ describe "for date type" do
5
+ before do
6
+ Person.validates_date :birth_date, format: "yyyy-mm-dd"
7
+ end
8
+
9
+ it "should not be valid for string given in the wrong format" do
10
+ invalid!(:birth_date, '23/12/2023', /is not a valid date/)
11
+ end
12
+
13
+ it "should be valid for string given in the right format" do
14
+ valid!(:birth_date, '2023-12-23')
15
+ end
16
+
17
+ it "should be valid for date instance" do
18
+ valid!(:birth_date, Date.new(2022,12,23))
19
+ end
20
+ end
21
+
22
+ describe "for time type" do
23
+ before do
24
+ Person.validates_time :birth_time, format: "hh:nn:ss"
25
+ end
26
+
27
+ it "should not be valid for string given in the wrong format" do
28
+ invalid!(:birth_time, "00-00-00", /is not a valid time/)
29
+ end
30
+
31
+ it "should be valid for string given in the right format" do
32
+ valid!(:birth_time, "00:00:00")
33
+ end
34
+
35
+ it "should be valid for date instance" do
36
+ valid!(:birth_time, Time.new(2010, 1, 1, 0, 0, 0))
37
+ end
38
+ end
39
+
40
+ describe "for datetime type" do
41
+ before do
42
+ Person.validates_datetime :birth_datetime, format: "yyyy-mm-dd hh:nn:ss"
43
+ end
44
+
45
+ it "should not be valid for string given in the wrong format" do
46
+ invalid!(:birth_datetime, "01-01-2010 00-00-00", /is not a valid datetime/)
47
+ end
48
+
49
+ it "should be valid for string given in the right format" do
50
+ valid!(:birth_datetime, "2010-01-01 00:00:00")
51
+ end
52
+
53
+ it "should be valid for date instance" do
54
+ valid!(:birth_datetime, DateTime.new(2010, 1, 1, 0, 0, 0))
55
+ end
56
+ end
57
+ end
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.extra_rdoc_files = ["README.md", "CHANGELOG.md", "LICENSE"]
22
22
 
23
23
  s.metadata = {
24
+ "rubygems_mfa_required" => "true",
24
25
  "bug_tracker_uri" => "#{github_url}/issues",
25
26
  "changelog_uri" => "#{github_url}/blob/master/CHANGELOG.md",
26
27
  "source_code_uri" => "#{github_url}",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_timeliness
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0.beta1
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-21 00:00:00.000000000 Z
11
+ date: 2024-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -67,7 +67,8 @@ files:
67
67
  - README.md
68
68
  - Rakefile
69
69
  - gemfiles/rails_7_0.gemfile
70
- - gemfiles/rails_edge.gemfile
70
+ - gemfiles/rails_7_1.gemfile
71
+ - gemfiles/rails_7_2.gemfile
71
72
  - init.rb
72
73
  - lib/generators/validates_timeliness/install_generator.rb
73
74
  - lib/generators/validates_timeliness/templates/en.yml
@@ -99,6 +100,7 @@ files:
99
100
  - spec/validates_timeliness/railtie_spec.rb
100
101
  - spec/validates_timeliness/validator/after_spec.rb
101
102
  - spec/validates_timeliness/validator/before_spec.rb
103
+ - spec/validates_timeliness/validator/format_spec.rb
102
104
  - spec/validates_timeliness/validator/is_at_spec.rb
103
105
  - spec/validates_timeliness/validator/on_or_after_spec.rb
104
106
  - spec/validates_timeliness/validator/on_or_before_spec.rb
@@ -109,6 +111,7 @@ homepage: https://github.com/adzap/validates_timeliness
109
111
  licenses:
110
112
  - MIT
111
113
  metadata:
114
+ rubygems_mfa_required: 'true'
112
115
  bug_tracker_uri: https://github.com/adzap/validates_timeliness/issues
113
116
  changelog_uri: https://github.com/adzap/validates_timeliness/blob/master/CHANGELOG.md
114
117
  source_code_uri: https://github.com/adzap/validates_timeliness
@@ -124,11 +127,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
127
  version: '0'
125
128
  required_rubygems_version: !ruby/object:Gem::Requirement
126
129
  requirements:
127
- - - ">"
130
+ - - ">="
128
131
  - !ruby/object:Gem::Version
129
- version: 1.3.1
132
+ version: '0'
130
133
  requirements: []
131
- rubygems_version: 3.1.6
134
+ rubygems_version: 3.3.27
132
135
  signing_key:
133
136
  specification_version: 4
134
137
  summary: Date and time validation plugin for Rails which allows custom formats
@@ -148,6 +151,7 @@ test_files:
148
151
  - spec/validates_timeliness/railtie_spec.rb
149
152
  - spec/validates_timeliness/validator/after_spec.rb
150
153
  - spec/validates_timeliness/validator/before_spec.rb
154
+ - spec/validates_timeliness/validator/format_spec.rb
151
155
  - spec/validates_timeliness/validator/is_at_spec.rb
152
156
  - spec/validates_timeliness/validator/on_or_after_spec.rb
153
157
  - spec/validates_timeliness/validator/on_or_before_spec.rb
@@ -1,13 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
6
- gem "rspec"
7
- gem "rspec-rails", "~> 3.7"
8
- gem "sqlite3"
9
- gem "byebug"
10
- gem "appraisal"
11
- gem "nokogiri", "~> 1.8"
12
-
13
- gemspec path: "../"