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

feat(tresholds): Add models #2368

Merged
merged 2 commits into from
Aug 5, 2024
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
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
18 changes: 18 additions & 0 deletions db/migrate/20240802115017_create_progressive_billing_tresholds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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

add_index :progressive_billing_tresholds, %i[amount_cents plan_id recurring], unique: true
add_index :progressive_billing_tresholds, %i[plan_id recurring], unique: true, where: "recurring is true"
end
end
16 changes: 15 additions & 1 deletion db/schema.rb

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

15 changes: 15 additions & 0 deletions spec/factories/progressive_billing_tresholds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

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