Skip to content

Commit

Permalink
misc(tresholds): Rename progressive_billing_tresholds to usage_tresho…
Browse files Browse the repository at this point in the history
…lds (#2379)

## Roadmap Task

👉  https://getlago.canny.io/feature-requests/p/progressive-billing

## Description

Rename `progressive_billing_tresholds` to `usage_tresholds`:
- table
- models
- serializers
  • Loading branch information
ivannovosad authored Aug 7, 2024
1 parent 8bbc04e commit a393b74
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 71 deletions.
1 change: 1 addition & 0 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ guard :rspec, cmd: 'bundle exec rspec' do
end
watch('app/services/integrations/aggregator/base_service.rb') { 'spec/services/integrations/aggregator/' }
watch('app/services/base_service.rb') { 'spec/services/' }
watch('app/models/application_record.rb') { 'spec/models/' }
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
Expand Down
2 changes: 1 addition & 1 deletion app/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +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 :usage_tresholds

has_many :applied_taxes, class_name: 'Plan::AppliedTax', dependent: :destroy
has_many :taxes, through: :applied_taxes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ProgressiveBillingTreshold < ApplicationRecord
class UsageTreshold < ApplicationRecord
include PaperTrailTraceable
include Currencies

Expand Down
10 changes: 5 additions & 5 deletions app/serializers/v1/plan_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def serialize
}

payload.merge!(charges) if include?(:charges)
payload.merge!(progressive_billing_tresholds) if include?(:progressive_billing_tresholds)
payload.merge!(usage_tresholds) if include?(:usage_tresholds)
payload.merge!(taxes) if include?(:taxes)
payload.merge!(minimum_commitment) if include?(:minimum_commitment) && model.minimum_commitment

Expand All @@ -41,11 +41,11 @@ def charges
).serialize
end

def progressive_billing_tresholds
def usage_tresholds
::CollectionSerializer.new(
model.progressive_billing_tresholds,
::V1::ProgressiveBillingTresholdSerializer,
collection_name: 'progressive_billing_tresholds'
model.usage_tresholds,
::V1::UsageTresholdSerializer,
collection_name: 'usage_tresholds'
).serialize
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module V1
class ProgressiveBillingTresholdSerializer < ModelSerializer
class UsageTresholdSerializer < ModelSerializer
def serialize
{
lago_id: model.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RenameProgressiveBillingTresholds < ActiveRecord::Migration[7.1]
def change
rename_table :progressive_billing_tresholds, :usage_tresholds
end
end
30 changes: 15 additions & 15 deletions db/schema.rb

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

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :progressive_billing_treshold do
factory :usage_treshold do
plan
treshold_display_name { Faker::Name.name }
amount_cents { 100 }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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 { is_expected.to have_many(:usage_tresholds) }

it_behaves_like 'paper_trail traceable'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require 'rails_helper'

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

it { is_expected.to belong_to(:plan) }

Expand Down
38 changes: 19 additions & 19 deletions spec/serializers/v1/plan_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
described_class.new(
plan,
root_name: 'plan',
includes: %i[charges taxes minimum_commitment progressive_billing_tresholds]
includes: %i[charges taxes minimum_commitment usage_tresholds]
)
end

let(:plan) { create(:plan) }
let(:customer) { create(:customer, organization: plan.organization) }
let(:subscription) { create(:subscription, customer:, plan:) }
let(:charge) { create(:standard_charge, plan:) }
let(:progressive_billing_treshold) { create(:progressive_billing_treshold, plan:) }
let(:usage_treshold) { create(:usage_treshold, plan:) }

before { subscription && charge && progressive_billing_treshold }
before { subscription && charge && usage_treshold }

context 'when plan has one minimium commitment' do
let(:commitment) { create(:commitment, plan:) }
Expand Down Expand Up @@ -55,14 +55,14 @@
'lago_id' => charge.id
)

expect(result['plan']['progressive_billing_tresholds'].first).to include(
'lago_id' => progressive_billing_treshold.id,
'treshold_display_name' => progressive_billing_treshold.treshold_display_name,
'amount_cents' => progressive_billing_treshold.amount_cents,
'amount_currency' => progressive_billing_treshold.amount_currency,
'recurring' => progressive_billing_treshold.recurring?,
'created_at' => progressive_billing_treshold.created_at.iso8601,
'updated_at' => progressive_billing_treshold.updated_at.iso8601
expect(result['plan']['usage_tresholds'].first).to include(
'lago_id' => usage_treshold.id,
'treshold_display_name' => usage_treshold.treshold_display_name,
'amount_cents' => usage_treshold.amount_cents,
'amount_currency' => usage_treshold.amount_currency,
'recurring' => usage_treshold.recurring?,
'created_at' => usage_treshold.created_at.iso8601,
'updated_at' => usage_treshold.updated_at.iso8601
)

expect(result['plan']['minimum_commitment']).to include(
Expand Down Expand Up @@ -113,14 +113,14 @@
'lago_id' => charge.id
)

expect(result['plan']['progressive_billing_tresholds'].first).to include(
'lago_id' => progressive_billing_treshold.id,
'treshold_display_name' => progressive_billing_treshold.treshold_display_name,
'amount_cents' => progressive_billing_treshold.amount_cents,
'amount_currency' => progressive_billing_treshold.amount_currency,
'recurring' => progressive_billing_treshold.recurring?,
'created_at' => progressive_billing_treshold.created_at.iso8601,
'updated_at' => progressive_billing_treshold.updated_at.iso8601
expect(result['plan']['usage_tresholds'].first).to include(
'lago_id' => usage_treshold.id,
'treshold_display_name' => usage_treshold.treshold_display_name,
'amount_cents' => usage_treshold.amount_cents,
'amount_currency' => usage_treshold.amount_currency,
'recurring' => usage_treshold.recurring?,
'created_at' => usage_treshold.created_at.iso8601,
'updated_at' => usage_treshold.updated_at.iso8601
)

expect(result['plan']['minimum_commitment']).to be_nil
Expand Down

This file was deleted.

25 changes: 25 additions & 0 deletions spec/serializers/v1/usage_treshold_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ::V1::UsageTresholdSerializer do
subject(:serializer) { described_class.new(usage_treshold, root_name: 'usage_treshold') }

let(:usage_treshold) { create(:usage_treshold) }

it 'serializes the object' do
result = JSON.parse(serializer.to_json)

aggregate_failures do
expect(result['usage_treshold']).to include(
'lago_id' => usage_treshold.id,
'treshold_display_name' => usage_treshold.treshold_display_name,
'amount_cents' => usage_treshold.amount_cents,
'amount_currency' => usage_treshold.amount_currency,
'recurring' => usage_treshold.recurring?,
'created_at' => usage_treshold.created_at.iso8601,
'updated_at' => usage_treshold.updated_at.iso8601
)
end
end
end

0 comments on commit a393b74

Please sign in to comment.