displays the top 3 proposals for every category

This commit is contained in:
rgarcia
2016-02-16 12:17:37 +01:00
parent 09c1c5e089
commit 6540092bcd
5 changed files with 38 additions and 7 deletions

View File

@@ -8,7 +8,7 @@ class ProposalsController < ApplicationController
before_action :set_search_order, only: :index before_action :set_search_order, only: :index
before_action :load_categories, only: [:index, :new, :edit, :map] before_action :load_categories, only: [:index, :new, :edit, :map]
before_action :load_geozones, only: [:edit, :map] before_action :load_geozones, only: [:edit, :map]
before_action :authenticate_user!, except: [:index, :show, :map] before_action :authenticate_user!, except: [:index, :show, :map, :summary]
has_orders %w{hot_score confidence_score created_at relevance}, only: :index has_orders %w{hot_score confidence_score created_at relevance}, only: :index
has_orders %w{most_voted newest oldest}, only: :show has_orders %w{most_voted newest oldest}, only: :show
@@ -35,6 +35,10 @@ class ProposalsController < ApplicationController
set_featured_proposal_votes(@proposal) set_featured_proposal_votes(@proposal)
end end
def summary
@proposals = Proposal.grouped_by_categories(Proposal.category_names)
end
private private
def proposal_params def proposal_params

View File

@@ -4,7 +4,7 @@ module Abilities
def initialize(user) def initialize(user)
can [:read, :map], Debate can [:read, :map], Debate
can [:read, :map], Proposal can [:read, :map, :summary], Proposal
can :read, Comment can :read, Comment
can :read, SpendingProposal can :read, SpendingProposal
can :read, Legislation can :read, Legislation

View File

@@ -41,7 +41,11 @@ class Proposal < ActiveRecord::Base
scope :sort_by_random, -> { reorder("RANDOM()") } scope :sort_by_random, -> { reorder("RANDOM()") }
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("created_at >= ?", 7.days.ago)} scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
scope :grouped_by_categories, -> (categories) { where("lower(tags.name) IN (?)", categories).
joins(:tags).select('proposals.*, tags.name').
group_by(&:name) }
def searchable_values def searchable_values
{ title => 'A', { title => 'A',
@@ -60,13 +64,21 @@ class Proposal < ActiveRecord::Base
end end
def self.search_by_code(terms) def self.search_by_code(terms)
if code_match = /\A#{Setting["proposal_code_prefix"]}-\d\d\d\d-\d\d-(\d*)\z/.match(terms) if code = self.match_code(terms)
results = where(id: code_match[1]) results = where(id: code[1])
end end
return results if (results.present? && results.first.code == terms) return results if (results.present? && results.first.code == terms)
end end
def self.match_code(terms)
/\A#{Setting["proposal_code_prefix"]}-\d\d\d\d-\d\d-(\d*)\z/.match(terms)
end
def self.category_names
ActsAsTaggableOn::Tag.where("kind = 'category'").map {|tag| tag.name.downcase }
end
def description def description
super.try :html_safe super.try :html_safe
end end

View File

@@ -0,0 +1,14 @@
<section role="main">
<div class="wrap row">
<div id="proposals" class="proposals-list small-12 medium-9 column">
<% @proposals.each do |group_name, proposals| %>
<div><strong><%= group_name %></strong></div><br/>
<% proposals[0..2].each do |proposal| %>
<div><strong><%= proposal.title %></strong></div>
<div><%= proposal.description %></div>
<% end %>
<% end %>
</div>
</div>
</section>

View File

@@ -43,9 +43,9 @@ Rails.application.routes.draw do
get :suggest get :suggest
end end
end end
get 'debate_links/new' => 'debate_links#new' get 'debate_links/new' => 'debate_links#new'
post 'debate_links' => 'debate_links#create' post 'debate_links' => 'debate_links#create'
resources :proposals do resources :proposals do
member do member do
@@ -57,6 +57,7 @@ Rails.application.routes.draw do
collection do collection do
get :map get :map
get :suggest get :suggest
get :summary
end end
end end