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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Use find instead of find_by_id
Better raise a 404 HTML NotFound exception than any other unexpected error.
  • Loading branch information
microweb10 authored and javierm committed Jun 3, 2019
commit b122302c5884bb0b3a73fb624530e06428111ddd
4 changes: 2 additions & 2 deletions app/controllers/admin/budget_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def destroy
private

def load_budget
@budget = Budget.includes(:groups).find(params[:budget_id])
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end

def load_group
@group = @budget.groups.find(params[:id])
@group = @budget.groups.find_by_slug_or_id! params[:id]
end

def groups_index
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/admin/budget_headings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def destroy
private

def load_budget
@budget = Budget.includes(:groups).find(params[:budget_id])
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end

def load_group
@group = @budget.groups.find(params[:group_id])
@group = @budget.groups.find_by_slug_or_id! params[:group_id]
end

def load_heading
@heading = @group.headings.find(params[:id])
@heading = @group.headings.find_by_slug_or_id! params[:id]
end

def headings_index
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/budget_investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def budget_investment_params
end

def load_budget
@budget = Budget.includes(:groups).find(params[:budget_id])
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end

def load_investment
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Admin::BudgetsController < Admin::BaseController

has_filters %w{open finished}, only: :index

before_action :load_budget
before_action :load_budget, except: [:index, :new, :create]
load_and_authorize_resource

def index
Expand Down Expand Up @@ -68,7 +68,7 @@ def budget_params
end

def load_budget
@budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id])
@budget = Budget.find_by_slug_or_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_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id])
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end

def load_ballot
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/budgets/ballots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def show
private

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

def load_ballot
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/budgets/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def show
private

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

def load_group
@group = Budget::Group.find_by(slug: params[:id]) || Budget.find_by(id: params[:id])
@group = @budget.groups.find_by_slug_or_id! params[:id]
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/budgets/investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def load_ballot

def load_heading
if params[:heading_id].present?
@heading = @budget.headings.find_by(slug: params[:heading_id]) || @budget.headings.find_by(id: params[:heading_id])
@heading = @budget.headings.find_by_slug_or_id! params[:heading_id]
@assigned_heading = @ballot.try(:heading_for_group, @heading.try(:group))
load_map
end
Expand All @@ -156,7 +156,7 @@ def tag_cloud
end

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

def set_view
Expand Down
13 changes: 5 additions & 8 deletions app/controllers/budgets/results_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ def show
private

def load_budget
@budget = Budget.find_by(slug: params[:budget_id])
@budget ||= Budget.find_by(id: params[:budget_id])
@budget ||= Budget.first
@budget = Budget.find_by_slug_or_id(params[:budget_id]) || Budget.first
end

def load_heading
@heading = if params[:heading_id].present?
@budget.headings.find(params[:heading_id])
else
@budget.headings.first
end
if @budget.present?
headings = @budget.headings
@heading = headings.find_by_slug_or_id(params[:heading_id]) || headings.first
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/budgets/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show
private

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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def index
private

def load_budget
@budget = Budget.find_by(slug: params[:id]) || Budget.find_by(id: params[:id])
@budget = Budget.find_by_slug_or_id! params[:id]
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def only_verified_users
end

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

def load_categories
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_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id])
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end

def load_investment
Expand Down
2 changes: 1 addition & 1 deletion app/models/budget/investment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def self.filter_params(params)
end

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 = results.where("cached_votes_up + physical_votes >= ?",
Expand Down
25 changes: 25 additions & 0 deletions spec/controllers/budgets/ballots/lines_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions spec/features/admin/budget_groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@

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

scenario "Displaying no groups for budget" do
Expand Down
50 changes: 50 additions & 0 deletions spec/features/admin/budget_headings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,56 @@

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

scenario "Displaying no headings for group" do
Expand Down
28 changes: 28 additions & 0 deletions spec/features/admin/budget_investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@

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

scenario "Displaying investments" do
Expand Down
23 changes: 23 additions & 0 deletions spec/features/admin/budgets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@

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

scenario "Displaying no open budgets text" do
Expand Down