Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate attribute of subscription #408

Merged
merged 3 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class Subscription < ApplicationRecord
validates :duration, presence: true, numericality: { only_integer: true, greater_than: 0 }
validate :cannot_change_after_cancelled, :cannot_change_duration, on: :update
Letiste marked this conversation as resolved.
Show resolved Hide resolved

private
Letiste marked this conversation as resolved.
Show resolved Hide resolved

def cannot_change_after_cancelled
return if cancelled_at_was.nil?

errors.add(:cancelled_at, 'Subscription has already been cancelled')
end

def cannot_change_duration
return if changes[:duration].nil?

errors.add(:duration, 'Duration is immutable')
end
end
12 changes: 12 additions & 0 deletions db/migrate/20230104090306_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateSubscriptions < ActiveRecord::Migration[7.0]
def change
create_table :subscriptions do |t|
t.integer :duration
t.datetime :cancelled_at

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/subscriptions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
duration: 1
55 changes: 55 additions & 0 deletions test/models/subscription_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'test_helper'

class SubscriptionTest < ActiveSupport::TestCase
def setup
@subscription = subscriptions(:one)
end

test 'subscription is valid' do
assert_predicate @subscription, :valid?
end

test "duration can't be changed" do
@subscription.duration = 8
assert_not_predicate @subscription, :valid?
end

test "duration can't be nil" do
subscription = Subscription.new(duration: nil)
assert_not_predicate subscription, :valid?
end

test 'duration must be integer' do
subscription = Subscription.new(duration: 1.1)
assert_not_predicate subscription, :valid?
end

test 'duration must be strictly positive' do
subscription = Subscription.new(duration: 0)
assert_not_predicate subscription, :valid?

subscription = Subscription.new(duration: -1)
assert_not_predicate subscription, :valid?
end

test "cancelled_at can't be changed when not nil" do
subscription = Subscription.new(duration: 2, cancelled_at: DateTime.now)
subscription.save

subscription.cancelled_at = subscription.cancelled_at + 1.day
assert_not_predicate subscription, :valid?

subscription.cancelled_at = nil
assert_not_predicate subscription, :valid?
end

test 'cancelled_at can be changed when nil' do
subscription = Subscription.new(duration: 2)
subscription.save

subscription.cancelled_at = DateTime.now
assert_predicate subscription, :valid?
end
end