Skip to content

Commit

Permalink
Don't use DEFAULT_INTERVAL
Browse files Browse the repository at this point in the history
  • Loading branch information
martent committed Sep 2, 2019
1 parent 162f4c0 commit 5002e7f
Show file tree
Hide file tree
Showing 18 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Run the test cases in the projects root directory in your Vagrant box:
```shell
$ bundle exec rspec
```
Currently, if some tests fail, you may want to execute a second run as you see above. This is because an rspec order issue that hasn't been tracked down yet.
Currently, if some tests fail, you may want to execute a second test run. This is because an rspec order issue that hasn't been tracked down yet.

```shell
$ bundle exec rspec --only-failures
Expand Down
5 changes: 2 additions & 3 deletions app/lib/application_report/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
# See app/reports for implementation examples
module ApplicationReport
class Base
DEFAULT_INTERVAL = { from: Date.new(0), to: Date.today }.freeze
HELPERS_PATH = File.join(Rails.root, 'app', 'reports', 'helpers').freeze
$LOAD_PATH.unshift(HELPERS_PATH)

def initialize(options)
@options = options

@filename = options[:filename] || 'Utan titel.xlsx'
@from = options[:from] || DEFAULT_INTERVAL[:from]
@to = options[:to] || DEFAULT_INTERVAL[:to]
@from = options[:from]
@to = options[:to]
@interval = { from: @from, to: @to }
@sheet_name = options[:sheet_name] ||= format_sheet_name

Expand Down
2 changes: 0 additions & 2 deletions app/lib/economy/base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Economy
class Base
DEFAULT_INTERVAL = { from: Date.new(0), to: Date.today }.freeze

# Implement a #sum method in the subclass if the hashes in the as_array
# doesn't have :days and :amount
def sum
Expand Down
4 changes: 2 additions & 2 deletions app/lib/economy/family_and_emergency_home_cost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Economy
# 'Familje/jourhemskostnad'
# Calculation of FamilyAndEmergencyHomeCost for an array of placements
class FamilyAndEmergencyHomeCost < CostWithPoRate
def initialize(placements, options = {})
def initialize(placements, interval)
@placements = placements
@interval = { from: options[:from], to: (options[:to] || Date.today) }
@interval = interval
end

def as_array
Expand Down
2 changes: 1 addition & 1 deletion app/lib/economy/one_time_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.all(interval)
.where('municipality_placement_migrationsverket_at between ? and ? ', interval[:from], interval[:to])
end

def initialize(person, interval = DEFAULT_INTERVAL)
def initialize(person, interval = { from: Date.new(0), to: Date.today })
@person = person
@from = interval[:from].to_date
@to = interval[:to].to_date
Expand Down
2 changes: 1 addition & 1 deletion app/lib/economy/payment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Economy
class Payment < Base
def initialize(payments, interval = DEFAULT_INTERVAL)
def initialize(payments, interval)
@payments = payments
@interval = interval
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/economy/person_extra_cost.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Extra kostnader för personer
module Economy
class PersonExtraCost < Base
def initialize(person, interval = DEFAULT_INTERVAL)
def initialize(person, interval)
@person = person
@interval = interval
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/economy/placement_and_home_cost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Economy
# are calculated in Economy::FamilyAndEmergencyHomeCost since the structure different
# from #cost_per_day? and #cost_per_placement?
class PlacementAndHomeCost < Base
def initialize(placements, interval = DEFAULT_INTERVAL)
def initialize(placements, interval)
@placements = placements
@interval = interval
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/economy/placement_extra_cost.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Extra utgifter för placering
module Economy
class PlacementExtraCost < Base
def initialize(placements, interval = DEFAULT_INTERVAL)
def initialize(placements, interval)
@placements = placements
@interval = interval
end
Expand Down
1 change: 0 additions & 1 deletion app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class ApplicationRecord < ActiveRecord::Base
DEFAULT_INTERVAL = { from: Date.new(0), to: Date.today }.freeze
self.abstract_class = true

def date_format(field_name)
Expand Down
23 changes: 15 additions & 8 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,20 @@ def total_placement_time
end

def total_placement_and_home_costs
Economy::PlacementAndHomeCost.new(placements).sum +
Economy::FamilyAndEmergencyHomeCost.new(placements).sum
Economy::PlacementAndHomeCost.new(placements, default_interval).sum +
Economy::FamilyAndEmergencyHomeCost.new(placements, default_interval).sum
end

def total_costs
total_placement_and_home_costs +
Economy::PlacementExtraCost.new(placements).sum +
Economy::ExtraContributionCost.new(self).sum +
Economy::PersonExtraCost.new(self).sum
Economy::PlacementExtraCost.new(placements, default_interval).sum +
Economy::ExtraContributionCost.new(self, default_interval).sum +
Economy::PersonExtraCost.new(self, default_interval).sum
end

def total_rate
(Economy::RatesForPerson.new(self).sum || 0) + (Economy::OneTimePayment.new(self).sum || 0)
(Economy::RatesForPerson.new(self, default_interval).sum || 0) +
(Economy::OneTimePayment.new(self, default_interval).sum || 0)
end

# Return a people placements within a give range
Expand Down Expand Up @@ -185,9 +186,15 @@ def asylum
dates.sort_by { |_k, v| v }.last
end

def self.per_type_of_housing(type_of_housing, registered = DEFAULT_INTERVAL)
def self.per_type_of_housing(type_of_housing)
includes(:payments, placements: { home: [:type_of_housings, :costs] })
.where(placements: { home: { type_of_housings: { id: type_of_housing.id } } })
.where(registered: registered[:from]..registered[:to])
.where(registered: default_interval[:from]..default_interval[:to])
end

private

def default_interval
{ from: Date.new(0), to: Date.today }
end
end
3 changes: 2 additions & 1 deletion spec/unit/family_and_emergency_home_cost_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
RSpec.describe 'FamilyAndEmergencyHomeCost' do
let(:interval) { { from: Date.new(0), to: Date.today } }
let(:person) { create(:person) }
let(:home) { create(:home, type_of_cost: :cost_for_family_and_emergency_home) }

Expand Down Expand Up @@ -62,7 +63,7 @@
end

it 'should use default report range' do
costs = Economy::FamilyAndEmergencyHomeCost.new(person.placements)
costs = Economy::FamilyAndEmergencyHomeCost.new(person.placements, interval)
expect(costs.sum).to be_a(Numeric)
end

Expand Down
7 changes: 4 additions & 3 deletions spec/unit/home_cost_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
RSpec.describe 'Home Cost calculations' do
let(:interval) { { from: Date.new(0), to: Date.today } }
let(:person) { create(:person) }
let(:cost) do
create(
Expand Down Expand Up @@ -26,7 +27,7 @@
end

it 'should have correct total cost' do
expect(Economy::PlacementAndHomeCost.new(person.placements).sum).to eq (97 + 1) * 1234
expect(Economy::PlacementAndHomeCost.new(person.placements, interval).sum).to eq (97 + 1) * 1234
end

it 'should have a total cost limited by report interval' do
Expand All @@ -36,10 +37,10 @@
end

it 'should return an array of days and amount as a string' do
expect(Economy::PlacementAndHomeCost.new(person.placements).as_formula).to eq '98*1234'
expect(Economy::PlacementAndHomeCost.new(person.placements, interval).as_formula).to eq '98*1234'
end

it 'should return an array of days and amount' do
expect(Economy::PlacementAndHomeCost.new(person.placements).as_array).to eq([days: 98, amount: 1234])
expect(Economy::PlacementAndHomeCost.new(person.placements, interval).as_array).to eq([days: 98, amount: 1234])
end
end
4 changes: 2 additions & 2 deletions spec/unit/one_time_payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
describe 'for other municipality' do
it 'should not have a one time payment' do
municipality.our_municipality = false
expect(Economy::OneTimePayment.new(person).sum).to eq(nil)
expect(Economy::OneTimePayment.new(person).as_formula).to eq('')
expect(Economy::OneTimePayment.new(person, interval).sum).to eq(nil)
expect(Economy::OneTimePayment.new(person, interval).as_formula).to eq('')
end
end

Expand Down
5 changes: 0 additions & 5 deletions spec/unit/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
expect(payments.comments).to be_empty
end

it 'should use default report range' do
payments = Economy::Payment.new(person.payments)
expect(payments.sum).to be_a(Numeric)
end

it 'should have correct payment for a limiting full month report period' do
payments = Economy::Payment.new(
person.payments,
Expand Down
5 changes: 0 additions & 5 deletions spec/unit/person_extra_cost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
expect(rec.sum).to eq 1234
end

it 'should use default report range' do
rec = Economy::PersonExtraCost.new(person)
expect(rec.sum).to be_a(Numeric)
end

it 'should have correct cost for a limiting full month report period' do
rec = Economy::PersonExtraCost.new(
person,
Expand Down
7 changes: 4 additions & 3 deletions spec/unit/placement_cost_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
RSpec.describe 'PlacementAndHomeCost calculations' do
let(:interval) { { from: Date.new(0), to: Date.today } }
let(:person) { create(:person) }
let(:home) { create(:home, type_of_cost: :cost_per_placement) }
let(:placement) do
Expand All @@ -19,7 +20,7 @@
end

it 'should have correct total cost' do
expect(Economy::PlacementAndHomeCost.new(person.placements).sum).to eq((97 + 1) * 1234)
expect(Economy::PlacementAndHomeCost.new(person.placements, interval).sum).to eq((97 + 1) * 1234)
end

it 'should have a total cost limited by report interval' do
Expand All @@ -29,10 +30,10 @@
end

it 'should return an array of days and amount as a string' do
expect(Economy::PlacementAndHomeCost.new(person.placements).as_formula).to eq '98*1234'
expect(Economy::PlacementAndHomeCost.new(person.placements, interval).as_formula).to eq '98*1234'
end

it 'should return an array of days and amount' do
expect(Economy::PlacementAndHomeCost.new(person.placements).as_array).to eq([days: 98, amount: 1234])
expect(Economy::PlacementAndHomeCost.new(person.placements, interval).as_array).to eq([days: 98, amount: 1234])
end
end
4 changes: 3 additions & 1 deletion spec/unit/placement_extra_cost_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
RSpec.describe 'PlacementExtraCost' do
let(:interval) { { from: Date.new(0), to: Date.today } }

describe 'single cost' do
let(:person) { create(:person) }
let(:placement) { create(:placement, person: person) }
Expand Down Expand Up @@ -28,7 +30,7 @@
end

it 'should use default report range' do
rec = Economy::PlacementExtraCost.new(person.placements)
rec = Economy::PlacementExtraCost.new(person.placements, interval)
expect(rec.sum).to be_a(Numeric)
end

Expand Down

0 comments on commit 5002e7f

Please sign in to comment.