cleans up

This commit is contained in:
rgarcia
2016-02-16 15:21:08 +01:00
parent d9b7802af4
commit cfb48ef937
3 changed files with 15 additions and 9 deletions

View File

@@ -36,7 +36,7 @@ class ProposalsController < ApplicationController
end end
def summary def summary
@proposals = Proposal.last_week.sort_by_confidence_score.grouped_by_categories @proposals = Proposal.for_summary
@tag_cloud = tag_cloud @tag_cloud = tag_cloud
end end

View File

@@ -42,10 +42,7 @@ class Proposal < ActiveRecord::Base
scope :sort_by_relevance , -> { all } scope :sort_by_relevance , -> { all }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)} scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)}
scope :in_categories, -> { where("lower(tags.name) IN (?)", Proposal.category_names) }
scope :grouped_by_categories, -> { where("lower(tags.name) IN (?)", Proposal.category_names).
joins(:tags).select('proposals.*, tags.name').
group_by(&:name) }
def searchable_values def searchable_values
{ title => 'A', { title => 'A',
@@ -75,6 +72,15 @@ class Proposal < ActiveRecord::Base
/\A#{Setting["proposal_code_prefix"]}-\d\d\d\d-\d\d-(\d*)\z/.match(terms) /\A#{Setting["proposal_code_prefix"]}-\d\d\d\d-\d\d-(\d*)\z/.match(terms)
end end
def self.for_summary
last_week.
sort_by_confidence_score.
in_categories.
joins(:tags).
select('proposals.*, tags.name as tag_name').
group_by(&:tag_name)
end
def self.category_names def self.category_names
ActsAsTaggableOn::Tag.where("kind = 'category'").map {|tag| tag.name.downcase } ActsAsTaggableOn::Tag.where("kind = 'category'").map {|tag| tag.name.downcase }
end end

View File

@@ -642,19 +642,19 @@ describe Proposal do
end end
end end
describe "grouped_by_categories" do describe "for_summary" do
it "should return proposals tagged with a category" do it "should return proposals tagged with a category" do
create(:tag, kind: 'category', name: 'Culture') create(:tag, kind: 'category', name: 'Culture')
proposal = create(:proposal, tag_list: 'Culture') proposal = create(:proposal, tag_list: 'Culture')
expect(Proposal.grouped_by_categories.values.flatten).to include(proposal) expect(Proposal.for_summary.values.flatten).to include(proposal)
end end
it "should not return proposals tagged without a category" do it "should not return proposals tagged without a category" do
create(:tag, kind: 'category', name: 'Culture') create(:tag, kind: 'category', name: 'Culture')
proposal = create(:proposal, tag_list: 'Parks') proposal = create(:proposal, tag_list: 'Parks')
expect(Proposal.grouped_by_categories.values.flatten).to_not include(proposal) expect(Proposal.for_summary.values.flatten).to_not include(proposal)
end end
it "should return proposals grouped by tag" do it "should return proposals grouped by tag" do
@@ -665,7 +665,7 @@ describe Proposal do
proposal2 = create(:proposal, tag_list: 'Culture') proposal2 = create(:proposal, tag_list: 'Culture')
proposal3 = create(:proposal, tag_list: 'Health') proposal3 = create(:proposal, tag_list: 'Health')
expect(Proposal.grouped_by_categories).to include('Culture' => [proposal2, proposal1], 'Health' => [proposal3]) expect(Proposal.for_summary).to include('Culture' => [proposal2, proposal1], 'Health' => [proposal3])
end end
end end