Merge branch 'budget' into admin-budgets

This commit is contained in:
Juanjo Bazán
2016-09-09 15:13:05 +02:00
11 changed files with 63 additions and 153 deletions

View File

@@ -1,8 +1,7 @@
class UsersController < ApplicationController
has_filters %w{proposals debates comments spending_proposals}, only: :show
has_filters %w{proposals debates budget_investments comments}, only: :show
load_and_authorize_resource
helper_method :authorized_for_filter?
helper_method :author?
helper_method :author_or_admin?
@@ -15,8 +14,8 @@ class UsersController < ApplicationController
@activity_counts = HashWithIndifferentAccess.new(
proposals: Proposal.where(author_id: @user.id).count,
debates: Debate.where(author_id: @user.id).count,
comments: Comment.not_as_admin_or_moderator.where(user_id: @user.id).count,
spending_proposals: SpendingProposal.where(author_id: @user.id).count)
budget_investments: Budget::Investment.where(author_id: @user.id).count,
comments: Comment.not_as_admin_or_moderator.where(user_id: @user.id).count)
end
def load_filtered_activity
@@ -24,8 +23,8 @@ class UsersController < ApplicationController
case params[:filter]
when "proposals" then load_proposals
when "debates" then load_debates
when "budget_investments" then load_budget_investments
when "comments" then load_comments
when "spending_proposals" then load_spending_proposals if author_or_admin?
else load_available_activity
end
end
@@ -37,12 +36,12 @@ class UsersController < ApplicationController
elsif @activity_counts[:debates] > 0
load_debates
@current_filter = "debates"
elsif @activity_counts[:budget_investments] > 0
load_budget_investments
@current_filter = "budget_investments"
elsif @activity_counts[:comments] > 0
load_comments
@current_filter = "comments"
elsif @activity_counts[:spending_proposals] > 0 && author_or_admin?
load_spending_proposals
@current_filter = "spending_proposals"
end
end
@@ -58,8 +57,8 @@ class UsersController < ApplicationController
@comments = Comment.not_as_admin_or_moderator.where(user_id: @user.id).includes(:commentable).order(created_at: :desc).page(params[:page])
end
def load_spending_proposals
@spending_proposals = SpendingProposal.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
def load_budget_investments
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
end
def valid_access?
@@ -78,7 +77,4 @@ class UsersController < ApplicationController
@authorized_current_user ||= current_user && (current_user == @user || current_user.moderator? || current_user.administrator?)
end
def authorized_for_filter?(filter)
filter == "spending_proposals" ? author_or_admin? : true
end
end

View File

@@ -19,6 +19,7 @@ class User < ActiveRecord::Base
has_many :identities, dependent: :destroy
has_many :debates, -> { with_hidden }, foreign_key: :author_id
has_many :proposals, -> { with_hidden }, foreign_key: :author_id
has_many :budget_investments, -> { with_hidden }, foreign_key: :author_id, class_name: 'Budget::Investment'
has_many :comments, -> { with_hidden }
has_many :spending_proposals, foreign_key: :author_id
has_many :failed_census_calls

View File

@@ -1,4 +1,4 @@
<%= render "proposals" if @proposals.present? %>
<%= render "debates" if @debates.present? && feature?(:debates) %>
<%= render "budget_investments" if @budget_investments.present? %>
<%= render "comments" if @comments.present? %>
<%= render "spending_proposals" if @spending_proposals.present? %>

View File

@@ -0,0 +1,11 @@
<table id="budget_investments_list" class="clear activity-investment-projects">
<% @budget_investments.each do |budget_investment| %>
<tr id="budget_investment_<%= budget_investment.id %>">
<td>
<%= link_to budget_investment.title, budget_investment_path(budget_investment.budget, budget_investment) %>
</td>
</tr>
<% end %>
</table>
<%= paginate @budget_investments %>

View File

@@ -1,20 +0,0 @@
<table id="spending_proposals_list" class="clear activity-investment-projects">
<% @spending_proposals.each do |spending_proposal| %>
<tr id="spending_proposal_<%= spending_proposal.id %>">
<td>
<%= link_to spending_proposal.title, spending_proposal %>
</td>
<td>
<% if can?(:destroy, spending_proposal) %>
<%= link_to t("users.show.delete_spending_proposal"),
spending_proposal,
method: :delete,
data: { confirm: t("users.show.confirm_deletion_spending_proposal") },
class: 'delete' %>
<% end %>
</td>
</tr>
<% end %>
</table>
<%= paginate @spending_proposals %>

View File

@@ -26,17 +26,15 @@
<ul class="menu simple margin-top">
<% @valid_filters.each do |filter| %>
<% if @activity_counts[filter] > 0 %>
<% if authorized_for_filter?(filter) %>
<% if @current_filter == filter %>
<li class="active">
<%= t("users.show.filters.#{filter}", count: @activity_counts[filter]) %>
</li>
<% else %>
<li>
<%= link_to t("users.show.filters.#{filter}", count: @activity_counts[filter]),
current_path_with_query_params(filter: filter, page: 1) %>
</li>
<% end %>
<% if @current_filter == filter %>
<li class="active">
<%= t("users.show.filters.#{filter}", count: @activity_counts[filter]) %>
</li>
<% else %>
<li>
<%= link_to t("users.show.filters.#{filter}", count: @activity_counts[filter]),
current_path_with_query_params(filter: filter, page: 1) %>
</li>
<% end %>
<% end %>
<% end %>

View File

@@ -525,8 +525,6 @@ en:
deleted: Deleted
deleted_debate: This debate has been deleted
deleted_proposal: This proposal has been deleted
delete_spending_proposal: Delete
confirm_deletion_spending_proposal: Are you sure? This action can not be undone
filters:
comments:
one: 1 Comment
@@ -537,9 +535,9 @@ en:
proposals:
one: 1 Proposal
other: "%{count} Proposals"
spending_proposals:
one: 1 Spending proposal
other: "%{count} Spending proposals"
budget_investments:
one: 1 Investment
other: "%{count} Investments"
no_activity: User has no public activity
no_private_messages: "This user doesn't accept private messages."
private_activity: This user decided to keep the activity list private

View File

@@ -525,8 +525,6 @@ es:
deleted: Eliminado
deleted_debate: Este debate ha sido eliminado
deleted_proposal: Este propuesta ha sido eliminada
delete_spending_proposal: Eliminar
confirm_deletion_spending_proposal: "¿Seguro que desea eliminar esta propuesta?"
filters:
comments:
one: 1 Comentario
@@ -537,7 +535,7 @@ es:
proposals:
one: 1 Propuesta
other: "%{count} Propuestas"
spending_proposals:
budget_investments:
one: 1 Propuesta de inversión
other: "%{count} Propuestas de inversión"
no_activity: Usuario sin actividad pública

View File

@@ -170,25 +170,29 @@ feature 'Budget Investments' do
end
xscenario 'Create notice' do
budget.update(phase: "accepting")
login_as(author)
visit new_budget_investment_path(budget_id: budget.id)
fill_in 'investment_title', with: 'Build a skyscraper'
fill_in 'investment_description', with: 'I want to live in a high tower over the clouds'
fill_in 'investment_external_url', with: 'http://http://skyscraperpage.com/'
save_and_open_page
fill_in 'budget_investment_title', with: 'Build a skyscraper'
fill_in 'budget_investment_description', with: 'I want to live in a high tower over the clouds'
fill_in 'budget_investment_external_url', with: 'http://http://skyscraperpage.com/'
select 'All city', from: 'investment_heading_id'
check 'investment_terms_of_service'
click_button 'Create'
expect(page).to_not have_content 'Investment project created successfully'
expect(page).to have_content '1 error'
expect(page).to have_content 'Investment created successfully'
expect(page).to have_content 'You can access it from My activity'
within "#notice" do
click_link 'My activity'
end
expect(page).to have_content 'Investment project created successfully'
expect(current_url).to eq(user_url(author, filter: :budget_investments))
expect(page).to have_content "1 Investment"
expect(page).to have_content "Build a skyscraper"
end
xscenario 'Errors on create' do

View File

@@ -159,14 +159,6 @@ feature 'Spending proposals' do
expect(page).to have_content 'Spending proposal created successfully'
expect(page).to have_content 'You can access it from My activity'
within "#notice" do
click_link 'My activity'
end
expect(current_url).to eq(user_url(author, filter: :spending_proposals))
expect(page).to have_content "1 Spending proposal"
expect(page).to have_content "Build a skyscraper"
end
scenario 'Errors on create' do
@@ -194,26 +186,4 @@ feature 'Spending proposals' do
end
end
context "Destroy" do
scenario "Admin can destroy owned spending proposals" do
admin = create(:administrator)
user = create(:user, :level_two)
spending_proposal = create(:spending_proposal, author: user)
login_as(admin.user)
visit user_path(user)
within("#spending_proposal_#{spending_proposal.id}") do
click_link "Delete"
end
expect(page).to have_content("Spending proposal deleted succesfully.")
visit user_path(user)
expect(page).not_to have_css("spending_proposal_list")
end
end
end

View File

@@ -8,7 +8,8 @@ feature 'Users' do
@user = create(:user)
1.times {create(:debate, author: @user)}
2.times {create(:proposal, author: @user)}
3.times {create(:comment, user: @user)}
3.times {create(:budget_investment, author: @user)}
4.times {create(:comment, user: @user)}
visit user_path(@user)
end
@@ -16,7 +17,8 @@ feature 'Users' do
scenario 'shows user public activity' do
expect(page).to have_content('1 Debate')
expect(page).to have_content('2 Proposals')
expect(page).to have_content('3 Comments')
expect(page).to have_content('3 Investments')
expect(page).to have_content('4 Comments')
end
scenario 'shows only items where user has activity' do
@@ -24,7 +26,8 @@ feature 'Users' do
expect(page).to_not have_content('0 Proposals')
expect(page).to have_content('1 Debate')
expect(page).to have_content('3 Comments')
expect(page).to have_content('3 Investments')
expect(page).to have_content('4 Comments')
end
scenario 'default filter is proposals' do
@@ -48,11 +51,20 @@ feature 'Users' do
expect(page).to have_content(@user.debates.first.title)
end
scenario 'shows comments by default if user has no proposals nor debates' do
scenario 'shows investments by default if user has no proposals nor debates' do
@user.proposals.destroy_all
@user.debates.destroy_all
visit user_path(@user)
expect(page).to have_content(@user.budget_investments.first.title)
end
scenario 'shows comments by default if user has no proposals nor debates nor investments' do
@user.proposals.destroy_all
@user.debates.destroy_all
@user.budget_investments.destroy_all
visit user_path(@user)
@user.comments.each do |comment|
expect(page).to have_content(comment.body)
end
@@ -73,7 +85,7 @@ feature 'Users' do
expect(page).to_not have_content(comment.body)
end
click_link '3 Comments'
click_link '4 Comments'
@user.comments.each do |comment|
expect(page).to have_content(comment.body)
@@ -199,64 +211,6 @@ feature 'Users' do
end
feature 'Spending proposals' do
background do
@author = create(:user, :level_two)
@spending_proposal = create(:spending_proposal, author: @author, title: 'Build a school')
end
scenario 'is not shown if no user logged in' do
visit user_path(@author)
expect(page).to_not have_content('Build a school')
end
scenario 'is not shown if no user logged in (filtered url)' do
visit user_path(@author, filter: 'spending_proposals')
expect(page).to_not have_content('Build a school')
end
scenario 'is not shown if logged in user is a regular user' do
login_as(create(:user))
visit user_path(@author)
expect(page).to_not have_content('Build a school')
end
scenario 'is not shown if logged in user is moderator' do
login_as(create(:moderator).user)
visit user_path(@author)
expect(page).to_not have_content('Build a school')
end
scenario 'is shown if logged in user is admin' do
login_as(create(:administrator).user)
visit user_path(@author)
expect(page).to have_content('Build a school')
end
scenario 'is shown if logged in user is author' do
login_as(@author)
visit user_path(@author)
expect(page).to have_content('Build a school')
end
scenario 'delete button is not shown if logged in user is author' do
login_as(@author)
visit user_path(@author)
within("#spending_proposal_#{@spending_proposal.id}") do
expect(page).to_not have_content('Delete')
end
end
scenario 'delete button is shown if logged in user is admin' do
login_as(create(:administrator).user)
visit user_path(@author)
within("#spending_proposal_#{@spending_proposal.id}") do
expect(page).to have_content('Delete')
end
end
end
end
feature 'Special comments' do
@@ -283,4 +237,4 @@ feature 'Users' do
end
end
end
end