Merge pull request #2411 from wairbut-m2c/aperez-investments-search-by-id

Admins can search investments by title or ID
This commit is contained in:
Raimond Garcia
2018-01-31 15:44:19 +01:00
committed by GitHub
4 changed files with 30 additions and 9 deletions

View File

@@ -35,7 +35,9 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def update
set_valuation_tags
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")
else
load_admins
@@ -62,9 +64,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
end
def load_investments
@investments = if params[:project_title].present?
Budget::Investment.where("title ILIKE ?",
"%#{params[:project_title].strip}%")
@investments = if params[:title_or_id].present?
Budget::Investment.search_by_title_or_id(params)
else
Budget::Investment.scoped_filter(params, @current_filter)
.order(sort_by(params[:sort_by]))
@@ -74,8 +75,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
def budget_investment_params
params.require(:budget_investment)
.permit(:title, :description, :external_url, :heading_id, :administrator_id, :tag_list, :valuation_tag_list,
:incompatible, :selected, valuator_ids: [])
.permit(:title, :description, :external_url, :heading_id, :administrator_id, :tag_list,
:valuation_tag_list, :incompatible, :selected, valuator_ids: [])
end
def load_budget

View File

@@ -102,7 +102,7 @@ class Budget
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(id: params[:budget_id]) || Budget.find_by(slug: params[:budget_id])
results = Investment.where(budget_id: budget.id)
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)
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
{ title => 'A',
author.username => 'B',

View File

@@ -3,7 +3,7 @@
method: :get,
remote: true) do |f| %>
<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">
<%= f.submit t("admin.budget_investments.index.buttons.search"), class: "button" %>
</div>

View File

@@ -368,12 +368,25 @@ feature 'Admin budget investments' do
expect(page).to have_content(@investment_1.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'
expect(page).to have_content(@investment_1.title)
expect(page).not_to have_content(@investment_2.title)
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
context 'Sorting' do