Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use find instead of find by #3580

Merged
merged 5 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Load budgets using slugs
  • Loading branch information
voodoorai2000 authored and javierm committed Jun 3, 2019
commit 998b4d9e39726ecb491a426aa0a5fd71a76b5281
5 changes: 5 additions & 0 deletions app/controllers/admin/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,4 +67,8 @@ def budget_params
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
2 changes: 1 addition & 1 deletion app/controllers/budgets/ballot/lines_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def line_params
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
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/budgets/ballots_controller.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -13,6 +14,10 @@ def show

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
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/budgets/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -9,5 +11,14 @@ class GroupsController < ApplicationController
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
7 changes: 6 additions & 1 deletion app/controllers/budgets/investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class InvestmentsController < ApplicationController
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",
Expand Down Expand Up @@ -136,7 +137,7 @@ def load_ballot

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
Expand All @@ -154,6 +155,10 @@ def tag_cloud
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
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,4 +20,10 @@ def index
@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
5 changes: 5 additions & 0 deletions app/controllers/management/budgets/investments_controller.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -60,6 +61,10 @@ def only_verified_users
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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/valuation/budget_investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def resource_name
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
Expand Down
56 changes: 28 additions & 28 deletions spec/features/budgets/investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [122/110] (https://github.com/bbatsov/ruby-style-guide#80-character-limits)

expect(page).not_to have_content(unfeasible_investment.title)
end
end
Expand Down Expand Up @@ -476,7 +476,7 @@
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)
Expand Down Expand Up @@ -810,7 +810,7 @@ def investments_order

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"
Expand All @@ -822,14 +822,14 @@ def investments_order

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"
Expand All @@ -839,13 +839,13 @@ def investments_order
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"
Expand All @@ -872,7 +872,7 @@ def investments_order
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
Expand Down Expand Up @@ -948,7 +948,7 @@ def investments_order

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("")
Expand All @@ -964,7 +964,7 @@ def investments_order

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)
Expand All @@ -984,7 +984,7 @@ def investments_order
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)
Expand All @@ -1003,7 +1003,7 @@ def investments_order
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)
Expand All @@ -1025,7 +1025,7 @@ def investments_order
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)
Expand All @@ -1044,7 +1044,7 @@ def investments_order
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
Expand All @@ -1054,14 +1054,14 @@ def investments_order
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
Expand Down Expand Up @@ -1092,7 +1092,7 @@ def investments_order

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")
Expand Down Expand Up @@ -1120,14 +1120,14 @@ def investments_order
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")
Expand All @@ -1147,7 +1147,7 @@ def investments_order
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
Expand All @@ -1166,13 +1166,13 @@ def investments_order
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
Expand All @@ -1189,7 +1189,7 @@ def investments_order
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
Expand All @@ -1205,7 +1205,7 @@ def investments_order
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")
Expand All @@ -1225,7 +1225,7 @@ def investments_order
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")
Expand All @@ -1243,7 +1243,7 @@ def investments_order
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")
Expand Down Expand Up @@ -1647,7 +1647,7 @@ def investments_order
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)
Expand Down Expand Up @@ -1691,7 +1691,7 @@ def investments_order
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")
Expand Down