Merge pull request #3234 from consul/use_find_instead_of_find_by_id
[Backport] Use find instead of find_by_id
This commit is contained in:
@@ -25,7 +25,7 @@ module Budgets
|
||||
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[:budget_id]
|
||||
end
|
||||
|
||||
def investments_by_heading_ordered_alphabetically
|
||||
|
||||
@@ -4,7 +4,6 @@ class Management::Budgets::InvestmentsController < Management::BaseController
|
||||
load_resource :investment, through: :budget, class: 'Budget::Investment'
|
||||
|
||||
before_action :only_verified_users, except: :print
|
||||
before_action :load_heading, only: [:index, :show, :print]
|
||||
|
||||
def index
|
||||
@investments = @investments.apply_filters_and_search(@budget, params).page(params[:page])
|
||||
@@ -61,10 +60,6 @@ class Management::Budgets::InvestmentsController < Management::BaseController
|
||||
check_verified_user t("management.budget_investments.alert.unverified_user")
|
||||
end
|
||||
|
||||
def load_heading
|
||||
@heading = @budget.headings.find(params[:heading_id]) if params[:heading_id].present?
|
||||
end
|
||||
|
||||
def load_categories
|
||||
@categories = ActsAsTaggableOn::Tag.category.order(:name)
|
||||
end
|
||||
|
||||
@@ -31,7 +31,7 @@ class RelatedContentsController < ApplicationController
|
||||
private
|
||||
|
||||
def score(action)
|
||||
@related = RelatedContent.find_by(id: params[:id])
|
||||
@related = RelatedContent.find params[:id]
|
||||
@related.send("score_#{action}", current_user)
|
||||
|
||||
render template: 'relationable/_refresh_score_actions'
|
||||
|
||||
@@ -3,6 +3,10 @@ module Sluggable
|
||||
|
||||
included do
|
||||
before_validation :generate_slug, if: :generate_slug?
|
||||
|
||||
def self.find_by_slug_or_id(slug_or_id)
|
||||
find_by_slug(slug_or_id) || find_by_id(slug_or_id)
|
||||
end
|
||||
end
|
||||
|
||||
def generate_slug
|
||||
|
||||
15
spec/controllers/related_contents_controller_spec.rb
Normal file
15
spec/controllers/related_contents_controller_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe RelatedContentsController do
|
||||
|
||||
describe "#score" do
|
||||
it "raises an error if related content does not exist" do
|
||||
controller.params[:id] = 0
|
||||
|
||||
expect do
|
||||
controller.send(:score, "action")
|
||||
end.to raise_error ActiveRecord::RecordNotFound
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user