Merge pull request #3580 from consul/use_find_instead_of_find_by_id

Use find instead of find by
This commit is contained in:
Javier Martín
2019-06-05 19:04:47 +02:00
committed by GitHub
30 changed files with 549 additions and 51 deletions

View File

@@ -46,11 +46,11 @@ class Admin::BudgetGroupsController < Admin::BaseController
private private
def load_budget def load_budget
@budget = Budget.includes(:groups).find(params[:budget_id]) @budget = Budget.find_by_slug_or_id! params[:budget_id]
end end
def load_group def load_group
@group = @budget.groups.find(params[:id]) @group = @budget.groups.find_by_slug_or_id! params[:id]
end end
def groups_index def groups_index

View File

@@ -47,15 +47,15 @@ class Admin::BudgetHeadingsController < Admin::BaseController
private private
def load_budget def load_budget
@budget = Budget.includes(:groups).find(params[:budget_id]) @budget = Budget.find_by_slug_or_id! params[:budget_id]
end end
def load_group def load_group
@group = @budget.groups.find(params[:group_id]) @group = @budget.groups.find_by_slug_or_id! params[:group_id]
end end
def load_heading def load_heading
@heading = @group.headings.find(params[:id]) @heading = @group.headings.find_by_slug_or_id! params[:id]
end end
def headings_index def headings_index

View File

@@ -87,7 +87,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
end end
def load_budget def load_budget
@budget = Budget.includes(:groups).find(params[:budget_id]) @budget = Budget.find_by_slug_or_id! params[:budget_id]
end end
def load_investment def load_investment

View File

@@ -6,6 +6,7 @@ class Admin::BudgetsController < Admin::BaseController
has_filters %w{open finished}, only: :index has_filters %w{open finished}, only: :index
before_action :load_budget, except: [:index, :new, :create]
load_and_authorize_resource load_and_authorize_resource
def index def index
@@ -66,4 +67,8 @@ class Admin::BudgetsController < Admin::BaseController
params.require(:budget).permit(*valid_attributes, *report_attributes, translation_params(Budget)) params.require(:budget).permit(*valid_attributes, *report_attributes, translation_params(Budget))
end end
def load_budget
@budget = Budget.find_by_slug_or_id! params[:id]
end
end end

View File

@@ -37,7 +37,7 @@ module Budgets
end end
def load_budget def load_budget
@budget = Budget.find(params[:budget_id]) @budget = Budget.find_by_slug_or_id! params[:budget_id]
end end
def load_ballot def load_ballot

View File

@@ -1,6 +1,7 @@
module Budgets module Budgets
class BallotsController < ApplicationController class BallotsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :load_budget
load_and_authorize_resource :budget load_and_authorize_resource :budget
before_action :load_ballot before_action :load_ballot
after_action :store_referer, only: [:show] after_action :store_referer, only: [:show]
@@ -13,6 +14,10 @@ module Budgets
private private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_ballot def load_ballot
query = Budget::Ballot.where(user: current_user, budget: @budget) query = Budget::Ballot.where(user: current_user, budget: @budget)
@ballot = @budget.balloting? ? query.first_or_create : query.first_or_initialize @ballot = @budget.balloting? ? query.first_or_create : query.first_or_initialize

View File

@@ -1,5 +1,7 @@
module Budgets module Budgets
class GroupsController < ApplicationController class GroupsController < ApplicationController
before_action :load_budget
before_action :load_group
load_and_authorize_resource :budget load_and_authorize_resource :budget
load_and_authorize_resource :group, class: "Budget::Group" load_and_authorize_resource :group, class: "Budget::Group"
@@ -9,5 +11,14 @@ module Budgets
def show def show
end end
private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_group
@group = @budget.groups.find_by_slug_or_id! params[:id]
end
end end
end end

View File

@@ -10,6 +10,7 @@ module Budgets
PER_PAGE = 10 PER_PAGE = 10
before_action :authenticate_user!, except: [:index, :show, :json_data] 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 :budget, except: :json_data
load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment", load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment",
@@ -136,7 +137,7 @@ module Budgets
def load_heading def load_heading
if params[:heading_id].present? if params[:heading_id].present?
@heading = @budget.headings.find(params[:heading_id]) @heading = @budget.headings.find_by_slug_or_id! params[:heading_id]
@assigned_heading = @ballot.try(:heading_for_group, @heading.try(:group)) @assigned_heading = @ballot.try(:heading_for_group, @heading.try(:group))
load_map load_map
end end
@@ -154,6 +155,10 @@ module Budgets
TagCloud.new(Budget::Investment, params[:search]) TagCloud.new(Budget::Investment, params[:search])
end end
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def set_view def set_view
@view = (params[:view] == "minimal") ? "minimal" : "default" @view = (params[:view] == "minimal") ? "minimal" : "default"
end end

View File

@@ -14,15 +14,14 @@ module Budgets
private private
def load_budget def load_budget
@budget = Budget.find_by(id: params[:budget_id]) @budget = Budget.find_by_slug_or_id(params[:budget_id]) || Budget.first
end end
def load_heading def load_heading
@heading = if params[:heading_id].present? if @budget.present?
@budget.headings.find(params[:heading_id]) headings = @budget.headings
else @heading = headings.find_by_slug_or_id(params[:heading_id]) || headings.first
@budget.headings.first end
end
end end
end end

View File

@@ -13,7 +13,7 @@ module Budgets
private private
def load_budget def load_budget
@budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) @budget = Budget.find_by_slug_or_id! params[:budget_id]
end end
end end

View File

@@ -3,6 +3,7 @@ class BudgetsController < ApplicationController
include BudgetsHelper include BudgetsHelper
feature_flag :budgets feature_flag :budgets
before_action :load_budget, only: :show
load_and_authorize_resource load_and_authorize_resource
before_action :set_default_budget_filter, only: :show before_action :set_default_budget_filter, only: :show
has_filters %w[not_unfeasible feasible unfeasible unselected selected winners], 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 @banners = Banner.in_section("budgets").with_active
end end
private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:id]
end
end end

View File

@@ -1,4 +1,5 @@
class Management::Budgets::InvestmentsController < Management::BaseController class Management::Budgets::InvestmentsController < Management::BaseController
before_action :load_budget
load_resource :budget load_resource :budget
load_resource :investment, through: :budget, class: "Budget::Investment" 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") check_verified_user t("management.budget_investments.alert.unverified_user")
end end
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_categories def load_categories
@categories = ActsAsTaggableOn::Tag.category.order(:name) @categories = ActsAsTaggableOn::Tag.category.order(:name)
end end

View File

@@ -65,7 +65,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
end end
def load_budget def load_budget
@budget = Budget.find(params[:budget_id]) @budget = Budget.find_by_slug_or_id! params[:budget_id]
end end
def load_investment def load_investment

View File

@@ -113,7 +113,7 @@ class Budget
end end
def self.scoped_filter(params, current_filter) def self.scoped_filter(params, current_filter)
budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) budget = Budget.find_by_slug_or_id params[:budget_id]
results = Investment.by_budget(budget) results = Investment.by_budget(budget)
results = results.where("cached_votes_up + physical_votes >= ?", results = results.where("cached_votes_up + physical_votes >= ?",

View File

@@ -7,6 +7,10 @@ module Sluggable
def self.find_by_slug_or_id(slug_or_id) def self.find_by_slug_or_id(slug_or_id)
find_by_slug(slug_or_id) || find_by_id(slug_or_id) find_by_slug(slug_or_id) || find_by_id(slug_or_id)
end end
def self.find_by_slug_or_id!(slug_or_id)
find_by_slug(slug_or_id) || find(slug_or_id)
end
end end
def generate_slug def generate_slug

View File

@@ -0,0 +1,25 @@
require "rails_helper"
describe Budgets::Ballot::LinesController do
describe "#load_budget" do
it "raises an error if budget slug is not found" do
controller.params[:budget_id] = "wrong_budget"
expect do
controller.send(:load_budget)
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if budget id is not found" do
controller.params[:budget_id] = 0
expect do
controller.send(:load_budget)
end.to raise_error ActiveRecord::RecordNotFound
end
end
end

View File

@@ -32,6 +32,43 @@ describe "Admin budget groups" do
end end
context "Load" do
let!(:budget) { create(:budget, slug: "budget_slug") }
let!(:group) { create(:budget_group, slug: "group_slug", budget: budget) }
scenario "finds budget and group by slug" do
visit edit_admin_budget_group_path("budget_slug", "group_slug")
expect(page).to have_content(budget.name)
expect(page).to have_field "Group name", with: group.name
end
scenario "raises an error if budget slug is not found" do
expect do
visit edit_admin_budget_group_path("wrong_budget", group)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit edit_admin_budget_group_path(0, group)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if group slug is not found" do
expect do
visit edit_admin_budget_group_path(budget, "wrong_group")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if group id is not found" do
expect do
visit edit_admin_budget_group_path(budget, 0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Index" do context "Index" do
scenario "Displaying no groups for budget" do scenario "Displaying no groups for budget" do

View File

@@ -33,6 +33,56 @@ describe "Admin budget headings" do
end end
context "Load" do
let!(:budget) { create(:budget, slug: "budget_slug") }
let!(:group) { create(:budget_group, slug: "group_slug", budget: budget) }
let!(:heading) { create(:budget_heading, slug: "heading_slug", group: group) }
scenario "finds budget, group and heading by slug" do
visit edit_admin_budget_group_heading_path("budget_slug", "group_slug", "heading_slug")
expect(page).to have_content(budget.name)
expect(page).to have_content(group.name)
expect(page).to have_field "Heading name", with: heading.name
end
scenario "raises an error if budget slug is not found" do
expect do
visit edit_admin_budget_group_heading_path("wrong_budget", group, heading)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit edit_admin_budget_group_heading_path(0, group, heading)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if group slug is not found" do
expect do
visit edit_admin_budget_group_heading_path(budget, "wrong_group", heading)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if group id is not found" do
expect do
visit edit_admin_budget_group_heading_path(budget, 0, heading)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if heading slug is not found" do
expect do
visit edit_admin_budget_group_heading_path(budget, group, "wrong_heading")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if heading id is not found" do
expect do
visit edit_admin_budget_group_heading_path(budget, group, 0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Index" do context "Index" do
scenario "Displaying no headings for group" do scenario "Displaying no headings for group" do

View File

@@ -32,6 +32,34 @@ describe "Admin budget investments" do
end end
context "Load" do
let(:group) { create(:budget_group, budget: budget) }
let(:heading) { create(:budget_heading, group: group) }
let!(:investment) { create(:budget_investment, heading: heading) }
before { budget.update(slug: "budget_slug") }
scenario "finds investments using budget slug" do
visit admin_budget_budget_investments_path("budget_slug")
expect(page).to have_link investment.title
end
scenario "raises an error if budget slug is not found" do
expect do
visit admin_budget_budget_investments_path("wrong_budget", investment)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit admin_budget_budget_investments_path(0, investment)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Index" do context "Index" do
scenario "Displaying investments" do scenario "Displaying investments" do

View File

@@ -24,6 +24,29 @@ describe "Admin budgets" do
end end
context "Load" do
let!(:budget) { create(:budget, slug: "budget_slug") }
scenario "finds budget by slug" do
visit admin_budget_path("budget_slug")
expect(page).to have_content(budget.name)
end
scenario "raises an error if budget slug is not found" do
expect do
visit admin_budget_path("wrong_budget")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit admin_budget_path(0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Index" do context "Index" do
scenario "Displaying no open budgets text" do scenario "Displaying no open budgets text" do

View File

@@ -8,6 +8,55 @@ describe "Ballots" do
let!(:california) { create(:budget_heading, group: states, name: "California", price: 1000) } let!(:california) { create(:budget_heading, group: states, name: "California", price: 1000) }
let!(:new_york) { create(:budget_heading, group: states, name: "New York", price: 1000000) } let!(:new_york) { create(:budget_heading, group: states, name: "New York", price: 1000000) }
context "Load" do
let(:ballot) { create(:budget_ballot, user: user, budget: budget) }
before do
budget.update(slug: "budget_slug")
ballot.investments << create(:budget_investment, :selected, heading: california)
login_as(user)
end
scenario "finds ballot using budget slug" do
visit budget_ballot_path("budget_slug")
expect(page).to have_content("You have voted one investment")
end
scenario "raises an error if budget slug is not found" do
expect do
visit budget_ballot_path("wrong_budget")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit budget_ballot_path(0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Lines Load" do
let!(:investment) { create(:budget_investment, :selected, heading: california) }
before do
create(:budget_ballot, user: user, budget: budget)
budget.update(slug: "budget_slug")
login_as(user)
end
scenario "finds ballot lines using budget slug", :js do
visit budget_investments_path("budget_slug", states, california)
add_to_ballot(investment)
within("#sidebar") { expect(page).to have_content investment.title }
end
end
context "Voting" do context "Voting" do
before do before do

View File

@@ -6,6 +6,30 @@ describe "Budgets" do
let(:level_two_user) { create(:user, :level_two) } let(:level_two_user) { create(:user, :level_two) }
let(:allowed_phase_list) { ["balloting", "reviewing_ballots", "finished"] } let(:allowed_phase_list) { ["balloting", "reviewing_ballots", "finished"] }
context "Load" do
before { budget.update(slug: "budget_slug") }
scenario "finds budget by slug" do
visit budget_path("budget_slug")
expect(page).to have_content budget.name
end
scenario "raises an error if budget slug is not found" do
expect do
visit budget_path("wrong_budget")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit budget_path(0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Index" do context "Index" do
scenario "Show normal index with links" do scenario "Show normal index with links" do

View File

@@ -11,6 +11,22 @@ describe "Executions" do
let!(:investment4) { create(:budget_investment, :winner, heading: heading) } let!(:investment4) { create(:budget_investment, :winner, heading: heading) }
let!(:investment3) { create(:budget_investment, :incompatible, heading: heading) } let!(:investment3) { create(:budget_investment, :incompatible, heading: heading) }
scenario "finds budget by id or slug" do
budget.update(slug: "budget_slug")
visit budget_executions_path("budget_slug")
within(".budgets-stats") { expect(page).to have_content budget.name }
visit budget_executions_path(budget)
within(".budgets-stats") { expect(page).to have_content budget.name }
visit budget_executions_path("budget_slug")
within(".budgets-stats") { expect(page).to have_content budget.name }
visit budget_executions_path(budget)
within(".budgets-stats") { expect(page).to have_content budget.name }
end
scenario "only displays investments with milestones" do scenario "only displays investments with milestones" do
create(:milestone, milestoneable: investment1) create(:milestone, milestoneable: investment1)

View File

@@ -2,8 +2,45 @@ require "rails_helper"
describe "Budget Groups" do describe "Budget Groups" do
let(:budget) { create(:budget) } let(:budget) { create(:budget, slug: "budget_slug") }
let(:group) { create(:budget_group, budget: budget) } let!(:group) { create(:budget_group, slug: "group_slug", budget: budget) }
context "Load" do
scenario "finds group using budget slug and group slug" do
visit budget_group_path("budget_slug", "group_slug")
expect(page).to have_content "Select an option"
end
scenario "finds group using budget id and group id" do
visit budget_group_path(budget, group)
expect(page).to have_content "Select an option"
end
scenario "raises an error if budget slug is not found" do
expect do
visit budget_group_path("wrong_budget", group)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit budget_group_path(0, group)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if group slug is not found" do
expect do
visit budget_group_path(budget, "wrong_group")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if group id is not found" do
expect do
visit budget_group_path(budget, 0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Show" do context "Show" do
scenario "Headings are sorted by name" do scenario "Headings are sorted by name" do

View File

@@ -26,6 +26,53 @@ describe "Budget Investments" do
it_behaves_like "relationable", Budget::Investment it_behaves_like "relationable", Budget::Investment
end end
context "Load" do
let(:investment) { create(:budget_investment, heading: heading) }
before do
budget.update(slug: "budget_slug")
heading.update(slug: "heading_slug")
end
scenario "finds investment using budget slug" do
visit budget_investment_path("budget_slug", investment)
expect(page).to have_content investment.title
end
scenario "raises an error if budget slug is not found" do
expect do
visit budget_investment_path("wrong_budget", investment)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit budget_investment_path(0, investment)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "finds investment using heading slug" do
visit budget_investment_path(budget, investment, heading_id: "heading_slug")
expect(page).to have_content investment.title
end
scenario "raises an error if heading slug is not found" do
expect do
visit budget_investment_path(budget, investment, heading_id: "wrong_heading")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if heading id is not found" do
expect do
visit budget_investment_path(budget, investment, heading_id: 0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
scenario "Index" do scenario "Index" do
investments = [create(:budget_investment, heading: heading), investments = [create(:budget_investment, heading: heading),
create(:budget_investment, heading: heading), create(:budget_investment, heading: heading),
@@ -40,7 +87,7 @@ describe "Budget Investments" do
investments.each do |investment| investments.each do |investment|
within("#budget-investments") do within("#budget-investments") do
expect(page).to have_content investment.title 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) expect(page).not_to have_content(unfeasible_investment.title)
end end
end end
@@ -476,7 +523,7 @@ describe "Budget Investments" do
investment3 = create(:budget_investment, heading: heading) investment3 = create(:budget_investment, heading: heading)
investment4 = create(:budget_investment, :feasible, 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 within("#budget-investments") do
expect(page).to have_css(".budget-investment", count: 1) expect(page).to have_css(".budget-investment", count: 1)
@@ -810,7 +857,7 @@ describe "Budget Investments" do
scenario "Create with invisible_captcha honeypot field" do scenario "Create with invisible_captcha honeypot field" do
login_as(author) 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" select heading.name, from: "budget_investment_heading_id"
fill_in "budget_investment_title", with: "I am a bot" fill_in "budget_investment_title", with: "I am a bot"
@@ -822,14 +869,14 @@ describe "Budget Investments" do
expect(page.status_code).to eq(200) expect(page.status_code).to eq(200)
expect(page.html).to be_empty 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 end
scenario "Create budget investment too fast" do scenario "Create budget investment too fast" do
allow(InvisibleCaptcha).to receive(:timestamp_threshold).and_return(Float::INFINITY) allow(InvisibleCaptcha).to receive(:timestamp_threshold).and_return(Float::INFINITY)
login_as(author) 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" select heading.name, from: "budget_investment_heading_id"
fill_in "budget_investment_title", with: "I am a bot" fill_in "budget_investment_title", with: "I am a bot"
@@ -839,13 +886,13 @@ describe "Budget Investments" do
click_button "Create Investment" click_button "Create Investment"
expect(page).to have_content "Sorry, that was too quick! Please resubmit" 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 end
scenario "Create" do scenario "Create" do
login_as(author) 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" select heading.name, from: "budget_investment_heading_id"
fill_in "budget_investment_title", with: "Build a skyscraper" fill_in "budget_investment_title", with: "Build a skyscraper"
@@ -872,7 +919,7 @@ describe "Budget Investments" do
scenario "Errors on create" do scenario "Errors on create" do
login_as(author) login_as(author)
visit new_budget_investment_path(budget_id: budget.id) visit new_budget_investment_path(budget)
click_button "Create Investment" click_button "Create Investment"
expect(page).to have_content error_message expect(page).to have_content error_message
end end
@@ -948,7 +995,7 @@ describe "Budget Investments" do
login_as(author) 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) select_options = find("#budget_investment_heading_id").all("option").collect(&:text)
expect(select_options.first).to eq("") expect(select_options.first).to eq("")
@@ -964,7 +1011,7 @@ describe "Budget Investments" do
investment = create(:budget_investment, heading: heading) 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.title)
expect(page).to have_content(investment.description) expect(page).to have_content(investment.description)
@@ -984,7 +1031,7 @@ describe "Budget Investments" do
scenario "Price & explanation is shown when Budget is on published prices phase" do scenario "Price & explanation is shown when Budget is on published prices phase" do
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase| Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: 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.formatted_price)
expect(page).to have_content(investment.price_explanation) expect(page).to have_content(investment.price_explanation)
@@ -1003,7 +1050,7 @@ describe "Budget Investments" do
scenario "Price & explanation isn't shown when Budget is not on published prices phase" 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::Phase::PHASE_KINDS - Budget::Phase::PUBLISHED_PRICES_PHASES).each do |phase|
budget.update(phase: 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.formatted_price)
expect(page).not_to have_content(investment.price_explanation) expect(page).not_to have_content(investment.price_explanation)
@@ -1025,7 +1072,7 @@ describe "Budget Investments" do
scenario "Price & explanation isn't shown for any Budget's phase" do scenario "Price & explanation isn't shown for any Budget's phase" do
Budget::Phase::PHASE_KINDS.each do |phase| Budget::Phase::PHASE_KINDS.each do |phase|
budget.update(phase: 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.formatted_price)
expect(page).not_to have_content(investment.price_explanation) expect(page).not_to have_content(investment.price_explanation)
@@ -1044,7 +1091,7 @@ describe "Budget Investments" do
Setting["feature.community"] = true Setting["feature.community"] = true
investment = create(:budget_investment, heading: heading) 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" expect(page).to have_content "Access the community"
Setting["feature.community"] = false Setting["feature.community"] = false
@@ -1054,14 +1101,14 @@ describe "Budget Investments" do
Setting["feature.community"] = false Setting["feature.community"] = false
investment = create(:budget_investment, heading: heading) 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" expect(page).not_to have_content "Access the community"
end end
scenario "Don't display flaggable buttons" do scenario "Don't display flaggable buttons" do
investment = create(:budget_investment, heading: heading) 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" expect(page).not_to have_selector ".js-follow"
end end
@@ -1092,7 +1139,7 @@ describe "Budget Investments" do
scenario "Budget in selecting phase" do scenario "Budget in selecting phase" do
budget.update(phase: "selecting") 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("Unfeasibility explanation")
expect(page).not_to have_content("Price explanation") expect(page).not_to have_content("Price explanation")
@@ -1120,14 +1167,14 @@ describe "Budget Investments" do
heading: heading, heading: heading,
unfeasibility_explanation: "The unfeasible explanation") 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("Unfeasibility explanation")
expect(page).not_to have_content("Local government is not competent in this") 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 "\ expect(page).not_to have_content("This investment project has been marked as not feasible "\
"and will not go to balloting phase") "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("Unfeasibility explanation")
expect(page).to have_content("The unfeasible explanation") expect(page).to have_content("The unfeasible explanation")
@@ -1147,7 +1194,7 @@ describe "Budget Investments" do
group: group, group: group,
heading: heading) 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") expect(page).to have_content("This investment project has been selected for balloting phase")
end end
@@ -1166,13 +1213,13 @@ describe "Budget Investments" do
group: group, group: group,
heading: heading) 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") expect(page).not_to have_content("Winning investment project")
budget.update(phase: "finished") 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") expect(page).to have_content("Winning investment project")
end end
@@ -1189,7 +1236,7 @@ describe "Budget Investments" do
group: group, group: group,
heading: heading) 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") expect(page).to have_content("This investment project has not been selected for balloting phase")
end end
@@ -1205,7 +1252,7 @@ describe "Budget Investments" do
group: group, group: group,
heading: heading) heading: heading)
visit budget_investment_path(budget_id: budget.id, id: investment.id) visit budget_investment_path(budget, id: investment.id)
within("aside") do within("aside") do
expect(page).to have_content("Investment project") expect(page).to have_content("Investment project")
@@ -1225,7 +1272,7 @@ describe "Budget Investments" do
heading: heading, heading: heading,
unfeasibility_explanation: "Local government is not competent in this matter") 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("Unfeasibility explanation")
expect(page).not_to have_content("Local government is not competent in this matter") expect(page).not_to have_content("Local government is not competent in this matter")
@@ -1243,7 +1290,7 @@ describe "Budget Investments" do
heading: heading, heading: heading,
unfeasibility_explanation: "Local government is not competent in this matter") 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("Unfeasibility explanation")
expect(page).not_to have_content("Local government is not competent in this matter") expect(page).not_to have_content("Local government is not competent in this matter")
@@ -1647,7 +1694,7 @@ describe "Budget Investments" do
investment3 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true) investment3 = create(:budget_investment, :selected, :feasible, heading: heading, valuation_finished: true)
investment4 = 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 within("#budget-investments") do
expect(page).to have_css(".budget-investment", count: 1) expect(page).to have_css(".budget-investment", count: 1)
@@ -1691,7 +1738,7 @@ describe "Budget Investments" do
scenario "Do not display vote button for unselected investments in index" do scenario "Do not display vote button for unselected investments in index" do
investment = create(:budget_investment, :unselected, heading: heading) 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).to have_content investment.title
expect(page).not_to have_link("Vote") expect(page).not_to have_link("Vote")

View File

@@ -52,6 +52,30 @@ describe "Results" do
end end
end end
scenario "Does not raise error if budget (slug or id) is not found" do
visit budget_results_path("wrong budget")
within(".budgets-stats") do
expect(page).to have_content "Participatory budget results"
end
visit budget_results_path(0)
within(".budgets-stats") do
expect(page).to have_content "Participatory budget results"
end
end
scenario "Loads budget and heading by slug" do
visit budget_results_path(budget.slug, heading.slug)
expect(page).to have_selector("a.is-active", text: heading.name)
within("#budget-investments-compatible") do
expect(page).to have_content investment1.title
end
end
scenario "Load first budget heading if not specified" do scenario "Load first budget heading if not specified" do
other_heading = create(:budget_heading, group: group) other_heading = create(:budget_heading, group: group)
other_investment = create(:budget_investment, :winner, heading: other_heading) other_investment = create(:budget_investment, :winner, heading: other_heading)

View File

@@ -2,14 +2,36 @@ require "rails_helper"
describe "Stats" do describe "Stats" do
let(:budget) { create(:budget) } let(:budget) { create(:budget, :finished) }
let(:group) { create(:budget_group, budget: budget) } let(:group) { create(:budget_group, budget: budget) }
let(:heading) { create(:budget_heading, group: group, price: 1000) } let(:heading) { create(:budget_heading, group: group, price: 1000) }
context "Load" do
before { budget.update(slug: "budget_slug") }
scenario "finds budget by slug" do
visit budget_stats_path("budget_slug")
expect(page).to have_content budget.name
end
scenario "raises an error if budget slug is not found" do
expect do
visit budget_stats_path("wrong_budget")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit budget_stats_path(0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
describe "Show" do describe "Show" do
describe "advanced stats" do describe "advanced stats" do
let(:budget) { create(:budget, :finished) }
scenario "advanced stats enabled" do scenario "advanced stats enabled" do
budget.update(advanced_stats_enabled: true) budget.update(advanced_stats_enabled: true)

View File

@@ -3,7 +3,7 @@ require "rails_helper"
describe "Budget Investments" do describe "Budget Investments" do
let(:manager) { create(:manager) } let(:manager) { create(:manager) }
let(:budget) { create(:budget, phase: "selecting", name: "2033") } let(:budget) { create(:budget, phase: "selecting", name: "2033", slug: "budget_slug") }
let(:group) { create(:budget_group, budget: budget, name: "Whole city") } let(:group) { create(:budget_group, budget: budget, name: "Whole city") }
let(:heading) { create(:budget_heading, group: group, name: "Health") } let(:heading) { create(:budget_heading, group: group, name: "Health") }
@@ -18,6 +18,33 @@ describe "Budget Investments" do
{ "budget_id": "budget_id" }, { "budget_id": "budget_id" },
management = true management = true
context "Load" do
let(:investment) { create(:budget_investment, budget: budget) }
let(:user) { create(:user, :level_two) }
before { login_managed_user(user) }
scenario "finds investment using budget slug" do
visit management_budget_investment_path("budget_slug", investment)
expect(page).to have_content investment.title
end
scenario "raises an error if budget slug is not found" do
expect do
visit management_budget_investment_path("wrong_budget", investment)
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit management_budget_investment_path(0, investment)
end.to raise_error ActiveRecord::RecordNotFound
end
end
context "Create" do context "Create" do
before { heading.budget.update(phase: "accepting") } before { heading.budget.update(phase: "accepting") }

View File

@@ -11,6 +11,30 @@ describe "Valuation budget investments" do
login_as(valuator.user) login_as(valuator.user)
end end
context "Load" do
before { budget.update(slug: "budget_slug") }
scenario "finds investment using budget slug" do
visit valuation_budget_budget_investments_path("budget_slug")
expect(page).to have_content budget.name
end
scenario "raises an error if budget slug is not found" do
expect do
visit valuation_budget_budget_investments_path("wrong_budget")
end.to raise_error ActiveRecord::RecordNotFound
end
scenario "raises an error if budget id is not found" do
expect do
visit valuation_budget_budget_investments_path(0)
end.to raise_error ActiveRecord::RecordNotFound
end
end
scenario "Disabled with a feature flag" do scenario "Disabled with a feature flag" do
Setting["process.budgets"] = nil Setting["process.budgets"] = nil
expect{ expect{

View File

@@ -382,6 +382,30 @@ describe Budget::Investment do
end end
end end
describe "scoped_filter" do
let!(:budget) { create(:budget, slug: "budget_slug") }
let!(:group) { create(:budget_group, budget: budget) }
let!(:heading) { create(:budget_heading, group: group) }
let!(:investment) { create(:budget_investment, :feasible, heading: heading) }
it "finds budget by id or slug" do
result = described_class.scoped_filter({budget_id: budget.id}, nil)
expect(result.count).to be 1
expect(result.first.id).to be investment.id
result = described_class.scoped_filter({budget_id: "budget_slug"}, nil)
expect(result.count).to be 1
expect(result.first.id).to be investment.id
end
it "does not raise error if budget is not found" do
result = described_class.scoped_filter({budget_id: "wrong_budget"}, nil)
expect(result).to be_empty
end
end
describe "scopes" do describe "scopes" do
describe "valuation_open" do describe "valuation_open" do
it "returns all investments with false valuation_finished" do it "returns all investments with false valuation_finished" do