Skip to content

Commit

Permalink
feat(tresholds): Add models
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Aug 2, 2024
1 parent 42bc34d commit 897b639
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Plan < ApplicationRecord
has_many :coupon_targets
has_many :coupons, through: :coupon_targets
has_many :invoices, through: :subscriptions
has_many :progressive_billing_tresholds

has_many :applied_taxes, class_name: 'Plan::AppliedTax', dependent: :destroy
has_many :taxes, through: :applied_taxes
Expand Down
15 changes: 15 additions & 0 deletions app/models/progressive_billing_treshold.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class ProgressiveBillingTreshold < ApplicationRecord
include PaperTrailTraceable
include Currencies

belongs_to :plan

monetize :amount_cents

validates :amount_currency, inclusion: {in: currency_list}
validates :amount_cents, numericality: {greater_than: 0}
validates :amount_cents, uniqueness: {scope: %i[plan_id recurring]}
validates :recurring, uniqueness: {scope: :plan_id}, if: -> {recurring?}
end
15 changes: 15 additions & 0 deletions db/migrate/20240802115017_create_progressive_billing_tresholds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class CreateProgressiveBillingTresholds < ActiveRecord::Migration[7.1]
def change
create_table :progressive_billing_tresholds, id: :uuid do |t|
t.references :plan, null: false, index: true, foreign_key: true, type: :uuid
t.string :treshold_display_name
t.bigint :amount_cents, null: false
t.string :amount_currency, null: false
t.boolean :recurring, null: false, default: false

t.timestamps
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb

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

13 changes: 13 additions & 0 deletions spec/factories/progressive_billing_tresholds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FactoryBot.define do
factory :progressive_billing_treshold do
plan
treshold_display_name { Faker::Name.name }
amount_cents { 100 }
amount_currency { "EUR" }
recurring { false }

trait :recurring do
recurring { true }
end
end
end
1 change: 1 addition & 0 deletions spec/models/plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
subject(:plan) { build(:plan, trial_period: 3) }

it { is_expected.to have_one(:minimum_commitment) }
it { is_expected.to have_many(:progressive_billing_tresholds) }

it_behaves_like 'paper_trail traceable'

Expand Down
14 changes: 14 additions & 0 deletions spec/models/progressive_billing_treshold_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ProgressiveBillingTreshold, type: :model do
subject(:progressive_billing_treshold) { build(:progressive_billing_treshold) }

it { is_expected.to belong_to(:plan) }

it { is_expected.to validate_inclusion_of(:amount_currency).in_array(Currencies::ACCEPTED_CURRENCIES.keys) }
it { is_expected.to validate_numericality_of(:amount_cents).is_greater_than(0) }
it { is_expected.to validate_uniqueness_of(:amount_cents).scoped_to(%i[plan_id recurring]) }
it { is_expected.to validate_uniqueness_of(:recurring).scoped_to(:plan_id) }
end

0 comments on commit 897b639

Please sign in to comment.