Updated milestone count helper plus tests

This commit is contained in:
Dan Rowden 2016-07-25 22:23:27 +01:00 committed by Alfredo Sumaran
parent c1f508b0b1
commit da7c14442c
4 changed files with 44 additions and 6 deletions

View file

@ -38,12 +38,12 @@ module MilestonesHelper
# Returns count of milestones for different states # Returns count of milestones for different states
# Uses explicit hash keys as the 'opened' state URL params differs from the db value # Uses explicit hash keys as the 'opened' state URL params differs from the db value
# and we need to add the total # and we need to add the total
def milestone_counts(project:) def milestone_counts(milestones)
counts = @project.milestones.reorder(nil).group(:state).count() counts = milestones.reorder(nil).group(:state).count
{ {
opened: counts['active'], opened: counts['active'],
closed: counts['closed'], closed: counts['closed'],
all: counts['active'] + counts['closed'] all: counts.values.sum
} }
end end

View file

@ -1,4 +1,4 @@
- counts = milestone_counts(project: @project) - counts = milestone_counts(@project.milestones)
%ul.nav-links %ul.nav-links
%li{class: ("active" if params[:state].blank? || params[:state] == 'opened')} %li{class: ("active" if params[:state].blank? || params[:state] == 'opened')}

View file

@ -3,10 +3,15 @@ FactoryGirl.define do
title title
project project
trait :closed do trait :active do
state :closed state "active"
end end
trait :closed do
state "closed"
end
factory :active_milestone, traits: [:active]
factory :closed_milestone, traits: [:closed] factory :closed_milestone, traits: [:closed]
end end
end end

View file

@ -0,0 +1,33 @@
require 'spec_helper'
describe MilestonesHelper do
describe '#milestone_counts' do
let(:project) { FactoryGirl.create(:project) }
let(:milestone_1) { FactoryGirl.create(:active_milestone, project: project) }
let(:milestone_2) { FactoryGirl.create(:active_milestone, project: project) }
let(:milestone_3) { FactoryGirl.create(:closed_milestone, project: project) }
let(:counts) { helper.milestone_counts(project.milestones) }
it 'returns a hash containing three items' do
expect(counts.length).to eq 3
end
it 'returns a hash containing "opened" key' do
expect(counts.has_key?(:opened)).to eq true
end
it 'returns a hash containing "closed" key' do
expect(counts.has_key?(:closed)).to eq true
end
it 'returns a hash containing "all" key' do
expect(counts.has_key?(:all)).to eq true
end
# This throws a "NoMethodError: undefined method `+' for nil:NilClass" error for line 27; can't figure out why it can't find the keys in the hash
# it 'shows "all" object is the sum of "opened" and "closed" objects' do
# total = counts[:opened] + counts[:closed]
# expect(counts[:all]).to eq total
# end
end
end