diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb
index 14dce9de1..aa91ad02f 100644
--- a/app/controllers/proposals_controller.rb
+++ b/app/controllers/proposals_controller.rb
@@ -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
diff --git a/app/views/proposals/_featured_proposal.html.erb b/app/views/proposals/_featured_proposal.html.erb
index 629a56283..d23c83ade 100644
--- a/app/views/proposals/_featured_proposal.html.erb
+++ b/app/views/proposals/_featured_proposal.html.erb
@@ -12,48 +12,7 @@
-
-
- <% if voted_for?(@proposal_votes, proposal) %>
-
- <%= t("proposals.proposal.already_supported") %>
-
- <% 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 %>
-
-
- <% if user_signed_in? && current_user.organization? %>
-
-
- <%= t("votes.organizations") %>
-
-
- <% elsif user_signed_in? && !proposal.votable_by?(current_user)%>
-
-
- <%= t("votes.verified_only",
- verify_account: link_to(t("votes.verify_account"), verification_path )).html_safe %>
-
-
- <% elsif !user_signed_in? %>
-
- <%= 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 %>
-
- <% end %>
-
- <% if voted_for?(@proposal_votes, proposal) %>
-
- <%= social_share_button_tag(proposal.title, url: proposal_url(proposal), via: "AbriendoMadrid") %>
-
- <% end %>
-
+ <%= render 'featured_votes', proposal: proposal %>
diff --git a/app/views/proposals/_featured_votes.html.erb b/app/views/proposals/_featured_votes.html.erb
new file mode 100644
index 000000000..bda617e96
--- /dev/null
+++ b/app/views/proposals/_featured_votes.html.erb
@@ -0,0 +1,42 @@
+
+
+ <% if voted_for?(@featured_proposals_votes, proposal) %>
+
+ <%= t("proposals.proposal.already_supported") %>
+
+ <% 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 %>
+
+
+ <% if user_signed_in? && current_user.organization? %>
+
+
+ <%= t("votes.organizations") %>
+
+
+ <% elsif user_signed_in? && !proposal.votable_by?(current_user)%>
+
+
+ <%= t("votes.verified_only",
+ verify_account: link_to(t("votes.verify_account"), verification_path )).html_safe %>
+
+
+ <% elsif !user_signed_in? %>
+
+ <%= 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 %>
+
+ <% end %>
+
+ <% if voted_for?(@featured_proposals_votes, proposal) %>
+
+ <%= social_share_button_tag(proposal.title, url: proposal_url(proposal), via: "AbriendoMadrid") %>
+
+ <% end %>
+
\ No newline at end of file
diff --git a/app/views/proposals/index.html.erb b/app/views/proposals/index.html.erb
index 8343cfbdb..77a0a79a0 100644
--- a/app/views/proposals/index.html.erb
+++ b/app/views/proposals/index.html.erb
@@ -44,21 +44,23 @@
<%= link_to t("proposals.index.start_proposal"), new_proposal_path, class: 'button radius expand' %>
-
-
-
- <%= t("proposals.index.featured_proposals_html") %>
-
-
- <%= image_tag("featured_proposals.jpg", size: "210x135") %>
-
+ <% if @featured_proposals.present? %>
+
+
+
+ <%= t("proposals.index.featured_proposals_html") %>
+
+
+ <%= image_tag("featured_proposals.jpg", size: "210x135") %>
+
+
+
+ <% @featured_proposals.each do |featured_proposal| %>
+ <%= render "featured_proposal", proposal: featured_proposal %>
+ <% end %>
+
-
- <% @proposals.limit(3).each do |proposal| %>
- <%= render "featured_proposal", proposal: proposal %>
- <% end %>
-
-
+ <% end %>
<%= render partial: 'proposals/proposal', collection: @proposals %>
<%= paginate @proposals %>
diff --git a/app/views/proposals/vote_featured.js.erb b/app/views/proposals/vote_featured.js.erb
new file mode 100644
index 000000000..52619282b
--- /dev/null
+++ b/app/views/proposals/vote_featured.js.erb
@@ -0,0 +1 @@
+$("#<%= dom_id(@proposal) %>_votes").html('<%= j render("proposals/featured_votes", proposal: @proposal) %>');
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index 712e835c1..6afbe1f18 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -47,6 +47,7 @@ Rails.application.routes.draw do
resources :proposals do
member do
post :vote
+ post :vote_featured
put :flag
put :unflag
end
diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb
index d15ade63b..8706afd42 100644
--- a/spec/features/proposals_spec.rb
+++ b/spec/features/proposals_spec.rb
@@ -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
diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb
index 03aaa7e4c..06f7ad4c9 100644
--- a/spec/features/votes_spec.rb
+++ b/spec/features/votes_spec.rb
@@ -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
diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb
index b53ff2816..5a3f2d7e5 100644
--- a/spec/support/common_actions.rb
+++ b/spec/support/common_actions.rb
@@ -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