Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Merge 243fab2 into e50a8b1
Browse files Browse the repository at this point in the history
  • Loading branch information
HouseTrip CI User committed Feb 3, 2014
2 parents e50a8b1 + 243fab2 commit 43cff72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/idea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Idea < ActiveRecord::Base
}

def self.limit_per_state(limit:10)
ids = values_of(:id, :state).group_by(&:last).values.map { |id_states| id_states.take(limit).map(&:first) }.flatten
ids = pluck(:id, :state).group_by(&:last).values.map { |id_states| id_states.take(limit).map(&:first) }.flatten
where id:ids
end

Expand Down
19 changes: 19 additions & 0 deletions spec/models/idea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@
end
end

describe '.limit_per_state' do
before { Idea.delete_all }
let(:result) { Idea.limit_per_state }

it 'returns a relation' do
result.should be_a_kind_of(ActiveRecord::Relation)
end

it 'returns nothing when no ideas' do
result.should be_empty
end

it 'returns a mapping when ideas present' do
2.times { Idea.make!(state: 0) }
1.times { Idea.make!(state: 2) }
result.to_a.map(&:state).sort.should == [0, 0, 2]
end
end

describe '(sort orders)' do
before { Idea.delete_all }

Expand Down

0 comments on commit 43cff72

Please sign in to comment.