valid mood unique per day per user
This commit is contained in:
parent
560a9442a7
commit
d6fba7e6e8
3 changed files with 59 additions and 5 deletions
|
|
@ -1,4 +1,19 @@
|
||||||
class Mood < ApplicationRecord
|
class Mood < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :mode
|
belongs_to :mode
|
||||||
|
|
||||||
|
validates :recorded_at, presence: true
|
||||||
|
validate :unique_per_day_and_user
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def unique_per_day_and_user
|
||||||
|
return unless recorded_at && user
|
||||||
|
|
||||||
|
existing = user.moods.where.not(id: id).any? do |mood|
|
||||||
|
mood.recorded_at.to_date == recorded_at.to_date
|
||||||
|
end
|
||||||
|
|
||||||
|
errors.add(:recorded_at, :taken) if existing
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :mood do
|
factory :mood do
|
||||||
mode { "croisiere" }
|
recorded_at { DateTime.parse("2026-01-15 10:00:00") }
|
||||||
recorded_at { DateTime.now }
|
|
||||||
association :user
|
association :user
|
||||||
|
association :mode
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,46 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe Mood, type: :model do
|
describe Mood do
|
||||||
it 'works' do
|
let(:user) { create(:user) }
|
||||||
expect(true)
|
let(:mode) { create(:mode, user: user) }
|
||||||
|
|
||||||
|
subject { build(:mood, user: user, mode: mode, recorded_at: DateTime.parse("2026-01-15 10:00:00")) }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
|
||||||
|
describe "validations" do
|
||||||
|
it "is invalid without a user" do
|
||||||
|
subject.user = nil
|
||||||
|
expect(subject).not_to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is invalid without a mode" do
|
||||||
|
subject.mode = nil
|
||||||
|
expect(subject).not_to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is invalid without a recorded_at" do
|
||||||
|
subject.recorded_at = nil
|
||||||
|
expect(subject).not_to be_valid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "uniqueness per day and user" do
|
||||||
|
it "is invalid if a mood already exists for the same day and user" do
|
||||||
|
create(:mood, user: user, mode: mode, recorded_at: DateTime.parse("2026-01-15 18:00:00"))
|
||||||
|
expect(subject).not_to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is valid if the same day exists but for a different user" do
|
||||||
|
other_user = create(:user)
|
||||||
|
other_mode = create(:mode, user: other_user)
|
||||||
|
create(:mood, user: other_user, mode: other_mode, recorded_at: DateTime.parse("2026-01-15 10:00:00"))
|
||||||
|
expect(subject).to be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is valid if the same user has a mood on a different day" do
|
||||||
|
create(:mood, user: user, mode: mode, recorded_at: DateTime.parse("2026-01-14 10:00:00"))
|
||||||
|
expect(subject).to be_valid
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue