adds featured proposals

via new action featured_vote
Featured proposals are never duplicated in the filtered lists
Featured are not calculated/shown in searches.
This commit is contained in:
Juanjo Bazán
2015-10-22 11:49:14 +02:00
committed by Juanjo Bazán
parent c896fc3eef
commit 1136c5546a
9 changed files with 125 additions and 59 deletions

View File

@@ -11,11 +11,24 @@ class ProposalsController < ApplicationController
load_and_authorize_resource
respond_to :html, :js
def index_customization
@featured_proposals = Proposal.all.sort_by_confidence_score.limit(3) if @search_terms.blank?
if @featured_proposals.present?
set_featured_proposal_votes(@featured_proposals)
@resources = @resources.where('proposals.id NOT IN (?)', @featured_proposals.map(&:id))
end
end
def vote
@proposal.register_vote(current_user, 'yes')
set_proposal_votes(@proposal)
end
def vote_featured
@proposal.register_vote(current_user, 'yes')
set_featured_proposal_votes(@proposal)
end
private
def proposal_params
@@ -25,4 +38,8 @@ class ProposalsController < ApplicationController
def resource_model
Proposal
end
def set_featured_proposal_votes(proposals)
@featured_proposals_votes = current_user ? current_user.proposal_votes(proposals) : {}
end
end

View File

@@ -12,48 +12,7 @@
</div>
<div id="<%= dom_id(proposal) %>_votes" class="small-12 medium-3 left">
<div class="supports">
<div class="in-favor">
<% if voted_for?(@proposal_votes, proposal) %>
<div class="supported">
<%= t("proposals.proposal.already_supported") %>
</div>
<% else %>
<%= link_to vote_proposal_path(proposal, value: 'yes'),
class: "button button-support tiny radius expand",
title: t('proposals.proposal.support_title'), method: "post", remote: true do %>
<%= t("proposals.proposal.support") %>
<% end %>
<% end %>
</div>
<% if user_signed_in? && current_user.organization? %>
<div class="organizations-votes" style='display:none'>
<p>
<%= t("votes.organizations") %>
</p>
</div>
<% elsif user_signed_in? && !proposal.votable_by?(current_user)%>
<div class="anonymous-votes" style='display:none'>
<p>
<%= t("votes.verified_only",
verify_account: link_to(t("votes.verify_account"), verification_path )).html_safe %>
</p>
</div>
<% elsif !user_signed_in? %>
<div class="not-logged" style='display:none'>
<%= t("votes.unauthenticated",
signin: link_to(t("votes.signin"), new_user_session_path),
signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
</div>
<% end %>
<% if voted_for?(@proposal_votes, proposal) %>
<div class="share-supported">
<%= social_share_button_tag(proposal.title, url: proposal_url(proposal), via: "AbriendoMadrid") %>
</div>
<% end %>
</div>
<%= render 'featured_votes', proposal: proposal %>
</div>
</div>
</div>

View File

@@ -0,0 +1,42 @@
<div class="supports">
<div class="in-favor">
<% if voted_for?(@featured_proposals_votes, proposal) %>
<div class="supported">
<%= t("proposals.proposal.already_supported") %>
</div>
<% else %>
<%= link_to vote_featured_proposal_path(proposal, value: 'yes'),
class: "button button-support tiny radius expand",
title: t('proposals.proposal.support_title'), method: "post", remote: true do %>
<%= t("proposals.proposal.support") %>
<% end %>
<% end %>
</div>
<% if user_signed_in? && current_user.organization? %>
<div class="organizations-votes" style='display:none'>
<p>
<%= t("votes.organizations") %>
</p>
</div>
<% elsif user_signed_in? && !proposal.votable_by?(current_user)%>
<div class="anonymous-votes" style='display:none'>
<p>
<%= t("votes.verified_only",
verify_account: link_to(t("votes.verify_account"), verification_path )).html_safe %>
</p>
</div>
<% elsif !user_signed_in? %>
<div class="not-logged" style='display:none'>
<%= t("votes.unauthenticated",
signin: link_to(t("votes.signin"), new_user_session_path),
signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
</div>
<% end %>
<% if voted_for?(@featured_proposals_votes, proposal) %>
<div class="share-supported">
<%= social_share_button_tag(proposal.title, url: proposal_url(proposal), via: "AbriendoMadrid") %>
</div>
<% end %>
</div>

View File

@@ -44,21 +44,23 @@
<%= link_to t("proposals.index.start_proposal"), new_proposal_path, class: 'button radius expand' %>
</div>
<div class="row featured-proposals-container">
<div class="small-12 medium-3 column">
<h2>
<%= t("proposals.index.featured_proposals_html") %>
</h2>
<span class="show-for-medium-up">
<%= image_tag("featured_proposals.jpg", size: "210x135") %>
</span>
<% if @featured_proposals.present? %>
<div id="featured-proposals" class="row featured-proposals-container">
<div class="small-12 medium-3 column">
<h2>
<%= t("proposals.index.featured_proposals_html") %>
</h2>
<span class="show-for-medium-up">
<%= image_tag("featured_proposals.jpg", size: "210x135") %>
</span>
</div>
<div class="small-12 medium-9 column proposals-list">
<% @featured_proposals.each do |featured_proposal| %>
<%= render "featured_proposal", proposal: featured_proposal %>
<% end %>
</div>
</div>
<div class="small-12 medium-9 column proposals-list">
<% @proposals.limit(3).each do |proposal| %>
<%= render "featured_proposal", proposal: proposal %>
<% end %>
</div>
</div>
<% end %>
<%= render partial: 'proposals/proposal', collection: @proposals %>
<%= paginate @proposals %>

View File

@@ -0,0 +1 @@
$("#<%= dom_id(@proposal) %>_votes").html('<%= j render("proposals/featured_votes", proposal: @proposal) %>');

View File

@@ -47,6 +47,7 @@ Rails.application.routes.draw do
resources :proposals do
member do
post :vote
post :vote_featured
put :flag
put :unflag
end

View File

@@ -3,10 +3,19 @@ require 'rails_helper'
feature 'Proposals' do
scenario 'Index' do
featured_proposals = create_featured_proposals
proposals = [create(:proposal), create(:proposal), create(:proposal)]
visit proposals_path
expect(page).to have_selector('#proposals .proposal-featured', count: 3)
featured_proposals.each do |featured_proposal|
within('#featured-proposals') do
expect(page).to have_content featured_proposal.title
expect(page).to have_css("a[href='#{proposal_path(featured_proposal)}']")
end
end
expect(page).to have_selector('#proposals .proposal', count: 3)
proposals.each do |proposal|
within('#proposals') do
@@ -18,7 +27,7 @@ feature 'Proposals' do
scenario 'Paginated Index' do
per_page = Kaminari.config.default_per_page
(per_page + 2).times { create(:proposal) }
(per_page + 5).times { create(:proposal) }
visit proposals_path
@@ -422,6 +431,7 @@ feature 'Proposals' do
describe 'Limiting tags shown' do
scenario 'Index page shows up to 5 tags per proposal' do
create_featured_proposals
tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa"]
create :proposal, tag_list: tag_list
@@ -433,6 +443,7 @@ feature 'Proposals' do
end
scenario 'Index page shows 3 tags with no plus link' do
create_featured_proposals
tag_list = ["Medio Ambiente", "Corrupción", "Fiestas populares"]
create :proposal, tag_list: tag_list
@@ -450,6 +461,8 @@ feature 'Proposals' do
feature 'Proposal index order filters' do
scenario 'Default order is hot_score', :js do
create_featured_proposals
create(:proposal, title: 'Best proposal').update_column(:hot_score, 10)
create(:proposal, title: 'Worst proposal').update_column(:hot_score, 2)
create(:proposal, title: 'Medium proposal').update_column(:hot_score, 5)
@@ -461,6 +474,8 @@ feature 'Proposals' do
end
scenario 'Proposals are ordered by confidence_score', :js do
create_featured_proposals
create(:proposal, title: 'Best proposal').update_column(:confidence_score, 10)
create(:proposal, title: 'Worst proposal').update_column(:confidence_score, 2)
create(:proposal, title: 'Medium proposal').update_column(:confidence_score, 5)
@@ -480,6 +495,8 @@ feature 'Proposals' do
end
scenario 'Proposals are ordered by most commented', :js do
create_featured_proposals
create(:proposal, title: 'Best proposal', comments_count: 10)
create(:proposal, title: 'Medium proposal', comments_count: 5)
create(:proposal, title: 'Worst proposal', comments_count: 2)
@@ -499,6 +516,8 @@ feature 'Proposals' do
end
scenario 'Proposals are ordered by newest', :js do
create_featured_proposals
create(:proposal, title: 'Best proposal', created_at: Time.now)
create(:proposal, title: 'Medium proposal', created_at: Time.now - 1.hour)
create(:proposal, title: 'Worst proposal', created_at: Time.now - 1.day)
@@ -518,6 +537,8 @@ feature 'Proposals' do
end
scenario 'Proposals are ordered randomly', :js do
create_featured_proposals
create_list(:proposal, 12)
visit proposals_path
@@ -616,5 +637,10 @@ feature 'Proposals' do
visit proposal_path(proposal)
expect(page).to have_content('Deleted user')
create_featured_proposals
visit proposals_path
expect(page).to have_content('Deleted user')
end
end

View File

@@ -250,10 +250,11 @@ feature 'Votes' do
end
end
scenario 'Create in index', :js do
scenario 'Create in listed proposal in index', :js do
create_featured_proposals
visit proposals_path
within("#proposals") do
within("#proposal_#{@proposal.id}") do
find('.in-favor a').click
expect(page).to have_content "1 support"
@@ -261,6 +262,17 @@ feature 'Votes' do
end
expect(URI.parse(current_url).path).to eq(proposals_path)
end
scenario 'Create in featured proposal in index', :js do
visit proposals_path
within("#proposal_#{@proposal.id}") do
find('.in-favor a').click
expect(page).to have_content "You already supported this proposal, share it!"
end
expect(URI.parse(current_url).path).to eq(proposals_path)
end
end
end

View File

@@ -161,4 +161,10 @@ module CommonActions
expect(page).to have_selector('.in-favor a', visible: false)
end
def create_featured_proposals
[create(:proposal, :with_confidence_score, cached_votes_up: 100),
create(:proposal, :with_confidence_score, cached_votes_up: 90),
create(:proposal, :with_confidence_score, cached_votes_up: 80)]
end
end