Skip to content

Commit

Permalink
test setting and fetching ivars
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Melo <[email protected]>
  • Loading branch information
squanto committed Feb 14, 2025
1 parent e3108f4 commit 0b36b7b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 50 deletions.
25 changes: 15 additions & 10 deletions app/models/concerns/date_accessible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ def self.date_reader(*properties)
properties = [properties] unless properties.is_a?(Enumerable)

properties.each do |property|
attr_reader :"#{property}_month", :"#{property}_year", :"#{property}_day"
self.define_method("#{property}_month") do
self.instance_variable_get("@#{property}_month") || send(property)&.month
end
self.define_method("#{property}_year") do
self.instance_variable_get("@#{property}_year") || send(property)&.year
end
self.define_method("#{property}_day") do
self.instance_variable_get("@#{property}_day") || send(property)&.day
end
end
end

Expand All @@ -44,15 +52,12 @@ def self.date_writer(*properties)
attr_writer :"#{property}_month", :"#{property}_year", :"#{property}_day"

before_validation do
if send("#{property}_year").present? && send("#{property}_month").present? && send("#{property}_day").present?
send(
"#{property}=",
Date.new(
send("#{property}_year").to_i,
send("#{property}_month").to_i,
send("#{property}_day").to_i,
)
)
month_to_set = self.instance_variable_get("@#{property}_month")
day_to_set = self.instance_variable_get("@#{property}_day")
year_to_set = self.instance_variable_get("@#{property}_year")

if year_to_set.present? && month_to_set.present? && day_to_set.present?
send("#{property}=", Date.new(year_to_set.to_i, month_to_set.to_i, day_to_set.to_i))
end
rescue Date::Error
send("#{property}=", nil)
Expand Down
86 changes: 46 additions & 40 deletions spec/models/concerns/date_accessible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

RSpec.describe DateAccessible do
let(:year) { Rails.configuration.statefile_current_tax_year }
subject { create(:state_id) }
let(:month) { 2 }
let(:day) { 1 }
let(:expiration_date) { Date.new(year, month, day) }
subject { create(:state_id, expiration_date: expiration_date) }

describe '#date_accessor' do
it 'should create readers & writers for a single property' do
Expand Down Expand Up @@ -42,52 +45,55 @@
subject.expiration_date_year = year
subject.valid?
expect(subject.expiration_date).to eq(Date.new(year, 2, 1))
end

subject.expiration_date_day = 21
subject.expiration_date_year = nil
subject.valid?
expect(subject.expiration_date).to eq(Date.new(year, 2, 1))
context "with a date set" do
context "and an invalid submitted value to update" do
it "is not set" do
subject.expiration_date_day = 21
subject.expiration_date_year = nil
subject.valid?
expect(subject.expiration_date).to eq(expiration_date)
end
end
end
end

describe "#date_reader" do
it 'should allow the use of the a _day reader' do
expect(subject).to respond_to(:expiration_date_day)

expect(subject.expiration_date_day).to be_nil

subject.expiration_date_day = 12
subject.expiration_date_month = 2
subject.expiration_date_year = year
subject.valid?

expect(subject.expiration_date_day).to eq(12)
context "when the date is not set" do
let(:expiration_date) { nil }

it 'should allow the use of the a _day reader' do
expect(subject).to respond_to(:expiration_date_day)
expect(subject.expiration_date_day).to be_nil
end

it 'should allow the use of the a _month reader' do
expect(subject).to respond_to(:expiration_date_month)
expect(subject.expiration_date_month).to be_nil
end

it 'should allow the use of the a _year reader' do
expect(subject).to respond_to(:expiration_date_year)
expect(subject.expiration_date_year).to be_nil
end
end

it 'should allow the use of the a _month reader' do
expect(subject).to respond_to(:expiration_date_month)

expect(subject.expiration_date_month).to be_nil

subject.expiration_date_day = 1
subject.expiration_date_month = 5
subject.expiration_date_year = year
subject.valid?

expect(subject.expiration_date_month).to eq(5)
end

it 'should allow the use of the a _year reader' do
expect(subject).to respond_to(:expiration_date_year)

expect(subject.expiration_date_year).to be_nil

subject.expiration_date_day = 1
subject.expiration_date_month = 2
subject.expiration_date_year = year
subject.valid?

expect(subject.expiration_date_year).to eq(year)
context "when the date is set" do
it 'should allow the use of the a _day reader' do
expect(subject).to respond_to(:expiration_date_day)
expect(subject.expiration_date_day).to eq(day)
end

it 'should allow the use of the a _month reader' do
expect(subject).to respond_to(:expiration_date_month)
expect(subject.expiration_date_month).to eq(month)
end

it 'should allow the use of the a _year reader' do
expect(subject).to respond_to(:expiration_date_year)
expect(subject.expiration_date_year).to eq(year)
end
end
end
end

0 comments on commit 0b36b7b

Please sign in to comment.