Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#v0.6.1 2026-04-30
Comment thread
nsommer marked this conversation as resolved.
Outdated
- Added `.validate_duration` and `.validates_durations` to `NxtSupport::DurationAttributeAccessor`

# v0.6.0 2024-09-27
- Added `NxtSupport::Uuid`

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
nxt_support (0.6.0)
nxt_support (0.6.1)
activerecord
activesupport
nxt_init
Expand Down
10 changes: 10 additions & 0 deletions lib/nxt_support/models/duration_attribute_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def duration_attribute_writer(*attrs)
define_duration_attribute_writer(attr_name)
end
end

def validates_durations(*attrs, **opts)
Comment thread
nsommer marked this conversation as resolved.
validates_each *attrs, **opts do |record, attr, value|
Comment thread
nsommer marked this conversation as resolved.
Outdated
record.errors.add attr, "#{value} is not a valid iso8601 duration." unless record.is_valid_iso8601_duration?(value)
Comment thread
nsommer marked this conversation as resolved.
Outdated
end
end

def validates_duration(attr, **opts)
validates_durations(attr, **opts)
end

private

Expand Down
2 changes: 1 addition & 1 deletion lib/nxt_support/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module NxtSupport
VERSION = "0.6.0".freeze
VERSION = "0.6.1".freeze
end
71 changes: 71 additions & 0 deletions spec/models/duration_attribute_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,75 @@
end
end
end

describe '.validates_durations' do
subject(:klass) do
Class.new do
include ActiveModel::Model
include NxtSupport::DurationAttributeAccessor

attr_accessor :class_duration, :topic_duration

validates_durations :class_duration
end
end

context 'when the value is a valid ISO8601 duration string' do
it 'does not add an error' do
record = klass.new(class_duration: 'PT1H')
expect(record).to be_valid
end
end

context 'when the value is an invalid string' do
it 'adds an error with the invalid value in the message' do
record = klass.new(class_duration: 'not_a_duration')
expect(record).not_to be_valid
expect(record.errors[:class_duration]).to include('not_a_duration is not a valid iso8601 duration.')
end
end

context 'with allow_nil: true' do
subject(:klass) do
Class.new do
include ActiveModel::Model
include NxtSupport::DurationAttributeAccessor

attr_accessor :class_duration

validates_durations :class_duration, allow_nil: true
end
end

it 'does not add an error for nil' do
record = klass.new(class_duration: nil)
expect(record).to be_valid
end

it 'still adds an error for an invalid string' do
record = klass.new(class_duration: 'not_a_duration')
expect(record).not_to be_valid
end
end

context 'with multiple attributes' do
subject(:klass) do
Class.new do
include ActiveModel::Model
include NxtSupport::DurationAttributeAccessor

attr_accessor :class_duration, :topic_duration

validates_durations :class_duration, :topic_duration
end
end

it 'validates all specified attributes' do
record = klass.new(class_duration: 'not_valid', topic_duration: 'also_not_valid')
expect(record).not_to be_valid
expect(record.errors[:class_duration]).to be_present
expect(record.errors[:topic_duration]).to be_present
end
end
end
end