Admins can search investments by title or ID (#2401)
This commit is contained in:
@@ -35,7 +35,9 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
|
|||||||
def update
|
def update
|
||||||
set_valuation_tags
|
set_valuation_tags
|
||||||
if @investment.update(budget_investment_params)
|
if @investment.update(budget_investment_params)
|
||||||
redirect_to admin_budget_budget_investment_path(@budget, @investment, Budget::Investment.filter_params(params)),
|
redirect_to admin_budget_budget_investment_path(@budget,
|
||||||
|
@investment,
|
||||||
|
Budget::Investment.filter_params(params)),
|
||||||
notice: t("flash.actions.update.budget_investment")
|
notice: t("flash.actions.update.budget_investment")
|
||||||
else
|
else
|
||||||
load_admins
|
load_admins
|
||||||
@@ -62,9 +64,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_investments
|
def load_investments
|
||||||
@investments = if params[:project_title].present?
|
@investments = if params[:title_or_id].present?
|
||||||
Budget::Investment.where("title ILIKE ?",
|
Budget::Investment.search_by_title_or_id(params)
|
||||||
"%#{params[:project_title].strip}%")
|
|
||||||
else
|
else
|
||||||
Budget::Investment.scoped_filter(params, @current_filter)
|
Budget::Investment.scoped_filter(params, @current_filter)
|
||||||
.order(sort_by(params[:sort_by]))
|
.order(sort_by(params[:sort_by]))
|
||||||
@@ -74,8 +75,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
|
|||||||
|
|
||||||
def budget_investment_params
|
def budget_investment_params
|
||||||
params.require(:budget_investment)
|
params.require(:budget_investment)
|
||||||
.permit(:title, :description, :external_url, :heading_id, :administrator_id, :tag_list, :valuation_tag_list,
|
.permit(:title, :description, :external_url, :heading_id, :administrator_id, :tag_list,
|
||||||
:incompatible, :selected, valuator_ids: [])
|
:valuation_tag_list, :incompatible, :selected, valuator_ids: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_budget
|
def load_budget
|
||||||
|
|||||||
@@ -102,7 +102,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(id: params[:budget_id]) || Budget.find_by(slug: params[:budget_id])
|
||||||
results = Investment.where(budget_id: budget.id)
|
results = Investment.where(budget_id: budget.id)
|
||||||
|
|
||||||
results = limit_results(budget, params, results) if params[:max_per_heading].present?
|
results = limit_results(budget, params, results) if params[:max_per_heading].present?
|
||||||
@@ -138,6 +138,13 @@ class Budget
|
|||||||
results.where("budget_investments.id IN (?)", ids)
|
results.where("budget_investments.id IN (?)", ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.search_by_title_or_id(params)
|
||||||
|
results = Investment.where(budget_id: params[:budget_id])
|
||||||
|
|
||||||
|
return results.where(id: params[:title_or_id]) if params[:title_or_id] =~ /\A[0-9]+\z/
|
||||||
|
results.where("title ILIKE ?", "%#{params[:title_or_id].strip}%")
|
||||||
|
end
|
||||||
|
|
||||||
def searchable_values
|
def searchable_values
|
||||||
{ title => 'A',
|
{ title => 'A',
|
||||||
author.username => 'B',
|
author.username => 'B',
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
method: :get,
|
method: :get,
|
||||||
remote: true) do |f| %>
|
remote: true) do |f| %>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<%= text_field_tag :project_title, "", placeholder: t("admin.budget_investments.index.placeholder") %>
|
<%= text_field_tag :title_or_id, "", placeholder: t("admin.budget_investments.index.placeholder") %>
|
||||||
<div class="input-group-button">
|
<div class="input-group-button">
|
||||||
<%= f.submit t("admin.budget_investments.index.buttons.search"), class: "button" %>
|
<%= f.submit t("admin.budget_investments.index.buttons.search"), class: "button" %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -368,12 +368,25 @@ feature 'Admin budget investments' do
|
|||||||
expect(page).to have_content(@investment_1.title)
|
expect(page).to have_content(@investment_1.title)
|
||||||
expect(page).to have_content(@investment_2.title)
|
expect(page).to have_content(@investment_2.title)
|
||||||
|
|
||||||
fill_in 'project_title', with: 'Some investment'
|
fill_in 'title_or_id', with: 'Some investment'
|
||||||
click_button 'Search'
|
click_button 'Search'
|
||||||
|
|
||||||
expect(page).to have_content(@investment_1.title)
|
expect(page).to have_content(@investment_1.title)
|
||||||
expect(page).not_to have_content(@investment_2.title)
|
expect(page).not_to have_content(@investment_2.title)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario 'Search investments by ID' do
|
||||||
|
visit admin_budget_budget_investments_path(@budget)
|
||||||
|
|
||||||
|
expect(page).to have_content(@investment_1.title)
|
||||||
|
expect(page).to have_content(@investment_2.title)
|
||||||
|
|
||||||
|
fill_in 'title_or_id', with: @investment_2.id
|
||||||
|
click_button 'Search'
|
||||||
|
|
||||||
|
expect(page).to have_content(@investment_2.title)
|
||||||
|
expect(page).not_to have_content(@investment_1.title)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'Sorting' do
|
context 'Sorting' do
|
||||||
|
|||||||
Reference in New Issue
Block a user