From 998b4d9e39726ecb491a426aa0a5fd71a76b5281 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sun, 15 Jan 2017 21:36:03 +0100 Subject: [PATCH] Load budgets using slugs --- app/controllers/admin/budgets_controller.rb | 5 ++ .../budgets/ballot/lines_controller.rb | 2 +- app/controllers/budgets/ballots_controller.rb | 5 ++ app/controllers/budgets/groups_controller.rb | 11 ++++ .../budgets/investments_controller.rb | 7 ++- app/controllers/budgets_controller.rb | 7 +++ .../budgets/investments_controller.rb | 5 ++ .../budget_investments_controller.rb | 2 +- spec/features/budgets/investments_spec.rb | 56 +++++++++---------- 9 files changed, 69 insertions(+), 31 deletions(-) diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index 771a8c91d..0aa868449 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -6,6 +6,7 @@ class Admin::BudgetsController < Admin::BaseController has_filters %w{open finished}, only: :index + before_action :load_budget load_and_authorize_resource def index @@ -66,4 +67,8 @@ class Admin::BudgetsController < Admin::BaseController params.require(:budget).permit(*valid_attributes, *report_attributes, translation_params(Budget)) end + def load_budget + @budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id]) + end + end diff --git a/app/controllers/budgets/ballot/lines_controller.rb b/app/controllers/budgets/ballot/lines_controller.rb index c2e617e08..67b53535e 100644 --- a/app/controllers/budgets/ballot/lines_controller.rb +++ b/app/controllers/budgets/ballot/lines_controller.rb @@ -37,7 +37,7 @@ module Budgets end def load_budget - @budget = Budget.find(params[:budget_id]) + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) end def load_ballot diff --git a/app/controllers/budgets/ballots_controller.rb b/app/controllers/budgets/ballots_controller.rb index b5b63b4aa..501ad7b24 100644 --- a/app/controllers/budgets/ballots_controller.rb +++ b/app/controllers/budgets/ballots_controller.rb @@ -1,6 +1,7 @@ module Budgets class BallotsController < ApplicationController before_action :authenticate_user! + before_action :load_budget load_and_authorize_resource :budget before_action :load_ballot after_action :store_referer, only: [:show] @@ -13,6 +14,10 @@ module Budgets private + def load_budget + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) + end + def load_ballot query = Budget::Ballot.where(user: current_user, budget: @budget) @ballot = @budget.balloting? ? query.first_or_create : query.first_or_initialize diff --git a/app/controllers/budgets/groups_controller.rb b/app/controllers/budgets/groups_controller.rb index f298b5a93..9c15eef28 100644 --- a/app/controllers/budgets/groups_controller.rb +++ b/app/controllers/budgets/groups_controller.rb @@ -1,5 +1,7 @@ module Budgets class GroupsController < ApplicationController + before_action :load_budget + before_action :load_group load_and_authorize_resource :budget load_and_authorize_resource :group, class: "Budget::Group" @@ -9,5 +11,14 @@ module Budgets def show end + private + + def load_budget + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) + end + + def load_group + @group = Budget::Group.find_by(slug: params[:id]) || Budget.find_by(id: params[:id]) + end end end \ No newline at end of file diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index cc762cff5..0280129af 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -10,6 +10,7 @@ module Budgets PER_PAGE = 10 before_action :authenticate_user!, except: [:index, :show, :json_data] + before_action :load_budget, except: :json_data load_and_authorize_resource :budget, except: :json_data load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment", @@ -136,7 +137,7 @@ module Budgets def load_heading if params[:heading_id].present? - @heading = @budget.headings.find(params[:heading_id]) + @heading = @budget.headings.find_by(slug: params[:heading_id]) || @budget.headings.find_by(id: params[:heading_id]) @assigned_heading = @ballot.try(:heading_for_group, @heading.try(:group)) load_map end @@ -154,6 +155,10 @@ module Budgets TagCloud.new(Budget::Investment, params[:search]) end + def load_budget + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) + end + def set_view @view = (params[:view] == "minimal") ? "minimal" : "default" end diff --git a/app/controllers/budgets_controller.rb b/app/controllers/budgets_controller.rb index c1f9e03ef..355184897 100644 --- a/app/controllers/budgets_controller.rb +++ b/app/controllers/budgets_controller.rb @@ -3,6 +3,7 @@ class BudgetsController < ApplicationController include BudgetsHelper feature_flag :budgets + before_action :load_budget, only: :show load_and_authorize_resource before_action :set_default_budget_filter, only: :show has_filters %w[not_unfeasible feasible unfeasible unselected selected winners], only: :show @@ -19,4 +20,10 @@ class BudgetsController < ApplicationController @banners = Banner.in_section("budgets").with_active end + private + + def load_budget + @budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id]) + end + end diff --git a/app/controllers/management/budgets/investments_controller.rb b/app/controllers/management/budgets/investments_controller.rb index 0b30820a2..aba7f0724 100644 --- a/app/controllers/management/budgets/investments_controller.rb +++ b/app/controllers/management/budgets/investments_controller.rb @@ -1,4 +1,5 @@ class Management::Budgets::InvestmentsController < Management::BaseController + before_action :load_budget load_resource :budget load_resource :investment, through: :budget, class: "Budget::Investment" @@ -60,6 +61,10 @@ class Management::Budgets::InvestmentsController < Management::BaseController check_verified_user t("management.budget_investments.alert.unverified_user") end + def load_budget + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) + end + def load_categories @categories = ActsAsTaggableOn::Tag.category.order(:name) end diff --git a/app/controllers/valuation/budget_investments_controller.rb b/app/controllers/valuation/budget_investments_controller.rb index aa29120f1..a024c1760 100644 --- a/app/controllers/valuation/budget_investments_controller.rb +++ b/app/controllers/valuation/budget_investments_controller.rb @@ -65,7 +65,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end def load_budget - @budget = Budget.find(params[:budget_id]) + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) end def load_investment diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index eece48251..6d84ed03a 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -40,7 +40,7 @@ describe "Budget Investments" do investments.each do |investment| within("#budget-investments") do expect(page).to have_content investment.title - expect(page).to have_css("a[href='#{budget_investment_path(budget_id: budget.id, id: investment.id)}']", text: investment.title) + expect(page).to have_css("a[href='#{budget_investment_path(budget, id: investment.id)}']", text: investment.title) expect(page).not_to have_content(unfeasible_investment.title) end end @@ -476,7 +476,7 @@ describe "Budget Investments" do investment3 = create(:budget_investment, heading: heading) investment4 = create(:budget_investment, :feasible, heading: heading) - visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, filter: "unfeasible") + visit budget_investments_path(budget, heading_id: heading.id, filter: "unfeasible") within("#budget-investments") do expect(page).to have_css(".budget-investment", count: 1) @@ -810,7 +810,7 @@ describe "Budget Investments" do scenario "Create with invisible_captcha honeypot field" do login_as(author) - visit new_budget_investment_path(budget_id: budget.id) + visit new_budget_investment_path(budget) select heading.name, from: "budget_investment_heading_id" fill_in "budget_investment_title", with: "I am a bot" @@ -822,14 +822,14 @@ describe "Budget Investments" do expect(page.status_code).to eq(200) expect(page.html).to be_empty - expect(page).to have_current_path(budget_investments_path(budget_id: budget.id)) + expect(page).to have_current_path(budget_investments_path(budget)) end scenario "Create budget investment too fast" do allow(InvisibleCaptcha).to receive(:timestamp_threshold).and_return(Float::INFINITY) login_as(author) - visit new_budget_investment_path(budget_id: budget.id) + visit new_budget_investment_path(budget) select heading.name, from: "budget_investment_heading_id" fill_in "budget_investment_title", with: "I am a bot" @@ -839,13 +839,13 @@ describe "Budget Investments" do click_button "Create Investment" expect(page).to have_content "Sorry, that was too quick! Please resubmit" - expect(page).to have_current_path(new_budget_investment_path(budget_id: budget.id)) + expect(page).to have_current_path(new_budget_investment_path(budget)) end scenario "Create" do login_as(author) - visit new_budget_investment_path(budget_id: budget.id) + visit new_budget_investment_path(budget) select heading.name, from: "budget_investment_heading_id" fill_in "budget_investment_title", with: "Build a skyscraper" @@ -872,7 +872,7 @@ describe "Budget Investments" do scenario "Errors on create" do login_as(author) - visit new_budget_investment_path(budget_id: budget.id) + visit new_budget_investment_path(budget) click_button "Create Investment" expect(page).to have_content error_message end @@ -948,7 +948,7 @@ describe "Budget Investments" do login_as(author) - visit new_budget_investment_path(budget_id: budget.id) + visit new_budget_investment_path(budget) select_options = find("#budget_investment_heading_id").all("option").collect(&:text) expect(select_options.first).to eq("") @@ -964,7 +964,7 @@ describe "Budget Investments" do investment = create(:budget_investment, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).to have_content(investment.title) expect(page).to have_content(investment.description) @@ -984,7 +984,7 @@ describe "Budget Investments" do scenario "Price & explanation is shown when Budget is on published prices phase" do Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase| budget.update(phase: phase) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).to have_content(investment.formatted_price) expect(page).to have_content(investment.price_explanation) @@ -1003,7 +1003,7 @@ describe "Budget Investments" do scenario "Price & explanation isn't shown when Budget is not on published prices phase" do (Budget::Phase::PHASE_KINDS - Budget::Phase::PUBLISHED_PRICES_PHASES).each do |phase| budget.update(phase: phase) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content(investment.formatted_price) expect(page).not_to have_content(investment.price_explanation) @@ -1025,7 +1025,7 @@ describe "Budget Investments" do scenario "Price & explanation isn't shown for any Budget's phase" do Budget::Phase::PHASE_KINDS.each do |phase| budget.update(phase: phase) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content(investment.formatted_price) expect(page).not_to have_content(investment.price_explanation) @@ -1044,7 +1044,7 @@ describe "Budget Investments" do Setting["feature.community"] = true investment = create(:budget_investment, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).to have_content "Access the community" Setting["feature.community"] = false @@ -1054,14 +1054,14 @@ describe "Budget Investments" do Setting["feature.community"] = false investment = create(:budget_investment, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content "Access the community" end scenario "Don't display flaggable buttons" do investment = create(:budget_investment, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_selector ".js-follow" end @@ -1092,7 +1092,7 @@ describe "Budget Investments" do scenario "Budget in selecting phase" do budget.update(phase: "selecting") - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content("Unfeasibility explanation") expect(page).not_to have_content("Price explanation") @@ -1120,14 +1120,14 @@ describe "Budget Investments" do heading: heading, unfeasibility_explanation: "The unfeasible explanation") - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content("Unfeasibility explanation") expect(page).not_to have_content("Local government is not competent in this") expect(page).not_to have_content("This investment project has been marked as not feasible "\ "and will not go to balloting phase") - visit budget_investment_path(budget_id: budget.id, id: investment_2.id) + visit budget_investment_path(budget, id: investment_2.id) expect(page).to have_content("Unfeasibility explanation") expect(page).to have_content("The unfeasible explanation") @@ -1147,7 +1147,7 @@ describe "Budget Investments" do group: group, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).to have_content("This investment project has been selected for balloting phase") end @@ -1166,13 +1166,13 @@ describe "Budget Investments" do group: group, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content("Winning investment project") budget.update(phase: "finished") - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).to have_content("Winning investment project") end @@ -1189,7 +1189,7 @@ describe "Budget Investments" do group: group, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).to have_content("This investment project has not been selected for balloting phase") end @@ -1205,7 +1205,7 @@ describe "Budget Investments" do group: group, heading: heading) - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) within("aside") do expect(page).to have_content("Investment project") @@ -1225,7 +1225,7 @@ describe "Budget Investments" do heading: heading, unfeasibility_explanation: "Local government is not competent in this matter") - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content("Unfeasibility explanation") expect(page).not_to have_content("Local government is not competent in this matter") @@ -1243,7 +1243,7 @@ describe "Budget Investments" do heading: heading, unfeasibility_explanation: "Local government is not competent in this matter") - visit budget_investment_path(budget_id: budget.id, id: investment.id) + visit budget_investment_path(budget, id: investment.id) expect(page).not_to have_content("Unfeasibility explanation") expect(page).not_to have_content("Local government is not competent in this matter") @@ -1647,7 +1647,7 @@ describe "Budget Investments" do investment3 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true) investment4 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true) - visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, filter: "unselected") + visit budget_investments_path(budget, heading_id: heading.id, filter: "unselected") within("#budget-investments") do expect(page).to have_css(".budget-investment", count: 1) @@ -1691,7 +1691,7 @@ describe "Budget Investments" do scenario "Do not display vote button for unselected investments in index" do investment = create(:budget_investment, :unselected, heading: heading) - visit budget_investments_path(budget_id: budget.id, heading_id: heading.id, filter: "unselected") + visit budget_investments_path(budget, heading_id: heading.id, filter: "unselected") expect(page).to have_content investment.title expect(page).not_to have_link("Vote")