diff --git a/app/controllers/admin/budget_investments_controller.rb b/app/controllers/admin/budget_investments_controller.rb index 2c27259ee..0258c23a6 100644 --- a/app/controllers/admin/budget_investments_controller.rb +++ b/app/controllers/admin/budget_investments_controller.rb @@ -84,12 +84,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController end def load_investments - @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])) - end + @investments = Budget::Investment.scoped_filter(params, @current_filter) + .order(sort_by(params[:sort_by])) @investments = @investments.page(params[:page]) unless request.format.csv? end diff --git a/app/helpers/admin_budget_investments_helper.rb b/app/helpers/admin_budget_investments_helper.rb new file mode 100644 index 000000000..742ff5bac --- /dev/null +++ b/app/helpers/admin_budget_investments_helper.rb @@ -0,0 +1,11 @@ +module AdminBudgetInvestmentsHelper + + def advanced_menu_visibility + (params[:advanced_filters].empty? && params["max_per_heading"].blank?) ? 'hide' : '' + end + + def init_advanced_menu + params[:advanced_filters] = [] unless params[:advanced_filters] + end + +end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 6a9b95cd4..ee3292fcd 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -109,14 +109,15 @@ class Budget budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) results = Investment.by_budget(budget) - results = limit_results(budget, params, results) if params[:max_per_heading].present? - results = results.where(group_id: params[:group_id]) if params[:group_id].present? - results = results.by_tag(params[:tag_name]) if params[:tag_name].present? - results = results.by_heading(params[:heading_id]) if params[:heading_id].present? - results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].present? - results = results.by_valuator_group(params[:valuator_group_id]) if params[:valuator_group_id].present? - results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present? - results = advanced_filters(params, results) if params[:advanced_filters].present? + results = limit_results(budget, params, results) if params[:max_per_heading].present? + results = results.where(group_id: params[:group_id]) if params[:group_id].present? + results = results.by_tag(params[:tag_name]) if params[:tag_name].present? + results = results.by_heading(params[:heading_id]) if params[:heading_id].present? + results = results.by_valuator(params[:valuator_id]) if params[:valuator_id].present? + results = results.by_valuator_group(params[:valuator_group_id]) if params[:valuator_group_id].present? + results = results.by_admin(params[:administrator_id]) if params[:administrator_id].present? + results = advanced_filters(params, results) if params[:advanced_filters].present? + results = search_by_title_or_id(params[:title_or_id].strip, results) if params[:title_or_id] results = results.send(current_filter) if current_filter.present? results.includes(:heading, :group, :budget, administrator: :user, valuators: :user) @@ -143,11 +144,12 @@ 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}%") + def self.search_by_title_or_id(title_or_id, results) + if title_or_id =~ /^[0-9]+$/ + results.where(id: title_or_id) + else + results.where("title ILIKE ?", "%#{title_or_id}%") + end end def searchable_values diff --git a/app/views/admin/budget_investments/_advanced_filters.html.erb b/app/views/admin/budget_investments/_advanced_filters.html.erb deleted file mode 100644 index 017d8e63f..000000000 --- a/app/views/admin/budget_investments/_advanced_filters.html.erb +++ /dev/null @@ -1,45 +0,0 @@ -<%= link_to "#advanced_filters_content", - data: {toggle: "advanced_filters"}, - class: "advanced-filters float-right clear" do %> - <%= t("admin.budget_investments.index.advanced_filters") %> -<% end %> - -
- <%= form_tag(admin_budget_budget_investments_path(budget: @budget, - filter: params[:filter], - sort_by: params[:sort_by], - max_per_heading: params[:max_per_heading], - advanced_filters: params[:advanced_filters], - page: 1), method: :get, remote: true, enforce_utf8: false) do %> -
- - <%= check_box_tag "advanced_filters[]", "feasible" %> - <%= t("#{i18n_namespace}.filters.feasible") %> - - - - <%= check_box_tag "advanced_filters[]", "selected" %> - <%= t("#{i18n_namespace}.filters.selected") %> - - - - <%= check_box_tag "advanced_filters[]", "undecided" %> - <%= t("#{i18n_namespace}.filters.undecided") %> - - - - <%= check_box_tag "advanced_filters[]", "unfeasible" %> - <%= t("#{i18n_namespace}.filters.unfeasible") %> - - - - <%= t("#{i18n_namespace}.filters.max_per_heading") %> - <%= text_field_tag :max_per_heading %> - -
- -
- <%= submit_tag t("admin.budget_investments.index.buttons.filter"), class: "button expanded" %> -
- <% end %> -
diff --git a/app/views/admin/budget_investments/_search_form.html.erb b/app/views/admin/budget_investments/_search_form.html.erb index 11f99e221..0f3090c60 100644 --- a/app/views/admin/budget_investments/_search_form.html.erb +++ b/app/views/admin/budget_investments/_search_form.html.erb @@ -1,12 +1,61 @@ -
- <%= form_for(Budget::Investment.new, url: admin_budget_budget_investments_path(budget: @budget), - method: :get, - remote: true) do |f| %> -
- <%= text_field_tag :title_or_id, "", placeholder: t("admin.budget_investments.index.placeholder") %> -
- <%= f.submit t("admin.budget_investments.index.buttons.search"), class: "button" %> +<% init_advanced_menu %> +<%= form_tag(admin_budget_budget_investments_path(budget: @budget), method: :get, enforce_utf8: false) do %> +
+
+ <%= link_to "#advanced_filters_content", + data: {toggle: "advanced_filters"}, + class: "advanced-filters float-right clear" do %> + <%= t("admin.budget_investments.index.advanced_filters") %> + <% end %> +
+
+
+
+ <% ["feasible", "selected", "undecided", "unfeasible"].each do |option| %> +
+ <%= check_box_tag "advanced_filters[]", option, params[:advanced_filters].index(option), id: "advanced_filters_#{option}" %> + <%= t("admin.budget_investments.index.filters.#{option}") %> +
+ <% end %> +
+ <%= text_field_tag :max_per_heading, params["max_per_heading"], placeholder: t("admin.budget_investments.index.filters.max_per_heading") %>
- <% end %> -
+
+
+
+ <%= select_tag :administrator_id, + options_for_select(admin_select_options, params[:administrator_id]), + { prompt: t("admin.budget_investments.index.administrator_filter_all"), + label: false} %> +
+
+ <%= select_tag :valuator_or_group_id, + options_for_select(valuator_or_group_select_options, params[:valuator_or_group_id]), + { prompt: t("admin.budget_investments.index.valuator_filter_all"), + label: false} %> +
+
+ <%= select_tag :heading_id, + options_for_select(budget_heading_select_options(@budget), params[:heading_id]), + { prompt: t("admin.budget_investments.index.heading_filter_all"), + label: false} %> +
+
+ <%= select_tag :tag_name, + options_for_select(investment_tags_select_options(@budget), params[:tag_name]), + { prompt: t("admin.budget_investments.index.tags_filter_all"), + label: false} %> +
+
+
+
+
+ <%= text_field_tag :title_or_id, params["title_or_id"], placeholder: t("admin.budget_investments.index.placeholder") %> +
+
+
+ <%= submit_tag t("admin.budget_investments.index.buttons.filter"), class: "button expanded" %> +
+
+<% end %> diff --git a/app/views/admin/budget_investments/index.html.erb b/app/views/admin/budget_investments/index.html.erb index f2672f564..09b6e875b 100644 --- a/app/views/admin/budget_investments/index.html.erb +++ b/app/views/admin/budget_investments/index.html.erb @@ -8,42 +8,6 @@ <%= render "search_form" %> -<%= form_tag(admin_budget_budget_investments_path(budget: @budget), method: :get, enforce_utf8: false) do %> -
- <%= select_tag :administrator_id, - options_for_select(admin_select_options, params[:administrator_id]), - { prompt: t("admin.budget_investments.index.administrator_filter_all"), - label: false, - class: "js-submit-on-change" } %> -
- -
- <%= select_tag :valuator_or_group_id, - options_for_select(valuator_or_group_select_options, params[:valuator_or_group_id]), - { prompt: t("admin.budget_investments.index.valuator_filter_all"), - label: false, - class: "js-submit-on-change" } %> -
- -
- <%= select_tag :heading_id, - options_for_select(budget_heading_select_options(@budget), params[:heading_id]), - { prompt: t("admin.budget_investments.index.heading_filter_all"), - label: false, - class: "js-submit-on-change" } %> -
- -
- <%= select_tag :tag_name, - options_for_select(investment_tags_select_options(@budget), params[:tag_name]), - { prompt: t("admin.budget_investments.index.tags_filter_all"), - label: false, - class: "js-submit-on-change" } %> -
-<% end %> - -<%= render "advanced_filters", i18n_namespace: "admin.budget_investments.index" %> - <%= render "/shared/filter_subnav", i18n_namespace: "admin.budget_investments.index" %>
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index ac23897df..48ee46db6 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -172,7 +172,6 @@ en: one_filter_html: "Current applied filters: %{filter}" two_filters_html: "Current applied filters: %{filter}, %{advanced_filters}" buttons: - search: Search filter: Filter download_current_selection: "Download current selection" no_budget_investments: "There are no investment projects." diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index a745be1c1..de34f5298 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -110,24 +110,28 @@ feature 'Admin budget investments' do expect(page).to have_link("Plant trees") select "Central Park", from: "heading_id" + click_button 'Filter' expect(page).not_to have_link("Realocate visitors") expect(page).not_to have_link("Change name") expect(page).to have_link("Plant trees") select "All headings", from: "heading_id" + click_button 'Filter' expect(page).to have_link("Realocate visitors") expect(page).to have_link("Change name") expect(page).to have_link("Plant trees") select "Streets: Main Avenue", from: "heading_id" + click_button 'Filter' expect(page).to have_link("Realocate visitors") expect(page).not_to have_link("Change name") expect(page).not_to have_link("Plant trees") select "Streets: Mercy Street", from: "heading_id" + click_button 'Filter' expect(page).not_to have_link("Realocate visitors") expect(page).to have_link("Change name") @@ -146,18 +150,22 @@ feature 'Admin budget investments' do expect(page).to have_link("Destroy the city") select "Admin 1", from: "administrator_id" + click_button 'Filter' expect(page).to have_content('There is 1 investment') expect(page).not_to have_link("Destroy the city") expect(page).to have_link("Realocate visitors") select "All administrators", from: "administrator_id" + click_button 'Filter' expect(page).to have_content('There are 2 investments') expect(page).to have_link("Destroy the city") expect(page).to have_link("Realocate visitors") select "Admin 1", from: "administrator_id" + click_button 'Filter' + expect(page).to have_content('There is 1 investment') expect(page).not_to have_link("Destroy the city") expect(page).to have_link("Realocate visitors") @@ -177,18 +185,21 @@ feature 'Admin budget investments' do expect(page).to have_link("Destroy the city") select "Valuator 1", from: "valuator_or_group_id" + click_button 'Filter' expect(page).to have_content('There is 1 investment') expect(page).not_to have_link("Destroy the city") expect(page).to have_link("Realocate visitors") select "All valuators", from: "valuator_or_group_id" + click_button 'Filter' expect(page).to have_content('There are 2 investments') expect(page).to have_link("Destroy the city") expect(page).to have_link("Realocate visitors") select "Valuator 1", from: "valuator_or_group_id" + click_button 'Filter' expect(page).to have_content('There is 1 investment') expect(page).not_to have_link("Destroy the city") expect(page).to have_link("Realocate visitors") @@ -210,18 +221,22 @@ feature 'Admin budget investments' do expect(page).to have_link("Build a theatre") select "Health", from: "valuator_or_group_id" + click_button 'Filter' expect(page).to have_content('There is 1 investment') expect(page).to have_link("Build a hospital") expect(page).not_to have_link("Build a theatre") select "All valuators", from: "valuator_or_group_id" + click_button 'Filter' expect(page).to have_content('There are 2 investments') expect(page).to have_link("Build a hospital") expect(page).to have_link("Build a theatre") select "Culture", from: "valuator_or_group_id" + click_button 'Filter' + expect(page).to have_content('There is 1 investment') expect(page).to have_link("Build a theatre") expect(page).not_to have_link("Build a hospital") @@ -411,6 +426,140 @@ feature 'Admin budget investments' do end end + scenario "Combination of checkbox with text search", :js do + user = create(:user, username: 'Admin 1') + administrator = create(:administrator, user: user) + + create(:budget_investment, budget: budget, title: 'Educate the children', + id: 20, administrator: administrator) + create(:budget_investment, budget: budget, title: 'More schools', + id: 10, administrator: administrator) + create(:budget_investment, budget: budget, title: 'More hospitals') + + + visit admin_budget_budget_investments_path(budget_id: budget.id) + + expect(page).to have_css(".budget_investment", count: 3) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).to have_content("More hospitals") + + select "Admin 1", from: "administrator_id" + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 2) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).not_to have_content("More hospitals") + + fill_in 'title_or_id', with: 20 + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 1) + expect(page).to have_content("Educate the children") + expect(page).not_to have_content("More schools") + expect(page).not_to have_content("More hospitals") + + expect(page).to have_content('Selected') + + end + + scenario "Combination of select with text search", :js do + create(:budget_investment, budget: budget, title: 'Educate the children', + feasibility: 'feasible', id: 20, + valuation_finished: true) + create(:budget_investment, budget: budget, title: 'More schools', + feasibility: 'feasible', id: 10, + valuation_finished: true) + create(:budget_investment, budget: budget, title: 'More hospitals') + + visit admin_budget_budget_investments_path(budget_id: budget.id) + + expect(page).to have_css(".budget_investment", count: 3) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).to have_content("More hospitals") + + click_link 'Advanced filters' + + page.check('advanced_filters_feasible') + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 2) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).not_to have_content("More hospitals") + + fill_in 'title_or_id', with: 20 + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 1) + expect(page).to have_content("Educate the children") + expect(page).not_to have_content("More schools") + expect(page).not_to have_content("More hospitals") + + expect(page).to have_content('Selected') + + end + + scenario "Combination of checkbox with text search and checkbox", :js do + user = create(:user, username: 'Admin 1') + administrator = create(:administrator, user: user) + + create(:budget_investment, budget: budget, title: 'Educate the children', + feasibility: 'feasible', id: 20, + valuation_finished: true, + administrator: administrator) + create(:budget_investment, budget: budget, title: 'More schools', + feasibility: 'feasible', id: 10, + valuation_finished: true, + administrator: administrator) + create(:budget_investment, budget: budget, title: 'More hospitals', + administrator: administrator) + create(:budget_investment, budget: budget, title: 'More hostals') + + + visit admin_budget_budget_investments_path(budget_id: budget.id) + + expect(page).to have_css(".budget_investment", count: 4) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).to have_content("More hospitals") + expect(page).to have_content("More hostals") + + select "Admin 1", from: "administrator_id" + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 3) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).to have_content("More hospitals") + expect(page).not_to have_content("More hostals") + + click_link 'Advanced filters' + + page.check('advanced_filters_feasible') + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 2) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).not_to have_content("More hospitals") + expect(page).not_to have_content("More hostals") + + fill_in 'title_or_id', with: 20 + click_button "Filter" + + expect(page).to have_css(".budget_investment", count: 1) + expect(page).to have_content("Educate the children") + expect(page).not_to have_content("More schools") + expect(page).not_to have_content("More hospitals") + expect(page).not_to have_content("More hostals") + + expect(page).to have_content('Selected') + + end + end context 'Search' do @@ -429,7 +578,7 @@ feature 'Admin budget investments' do expect(page).to have_content('Some other investment') fill_in 'title_or_id', with: 'Some investment' - click_button 'Search' + click_button 'Filter' expect(page).to have_content('Some investment') expect(page).not_to have_content('Some other investment') @@ -442,7 +591,7 @@ feature 'Admin budget investments' do expect(page).to have_content('Some other investment') fill_in 'title_or_id', with: first_investment.id - click_button 'Search' + click_button 'Filter' expect(page).to have_content('Some other investment') expect(page).not_to have_content('Some investment') @@ -823,7 +972,7 @@ feature 'Admin budget investments' do expect(page).to have_content(winner_bi.title) click_link 'Advanced filters' - within('#advanced_filters') { find(:css, "#advanced_filters_[value='feasible']").set(true) } + within('#advanced_filters') { find(:css, "#advanced_filters_feasible").set(true) } click_button 'Filter' expect(page).not_to have_content(unfeasible_bi.title) @@ -832,8 +981,8 @@ feature 'Admin budget investments' do expect(page).to have_content(selected_bi.title) expect(page).to have_content(winner_bi.title) - within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) } - within('#advanced_filters') { find(:css, "#advanced_filters_[value='feasible']").set(false) } + within('#advanced_filters') { find(:css, "#advanced_filters_selected").set(true) } + within('#advanced_filters') { find(:css, "#advanced_filters_feasible").set(false) } click_button 'Filter' expect(page).not_to have_content(unfeasible_bi.title) @@ -854,7 +1003,7 @@ feature 'Admin budget investments' do visit admin_budget_budget_investments_path(budget) click_link 'Advanced filters' - within('#advanced_filters') { find(:css, "#advanced_filters_[value='undecided']").set(true) } + within('#advanced_filters') { find(:css, "#advanced_filters_undecided").set(true) } click_button 'Filter' expect(page).to have_content(undecided_bi.title) @@ -864,7 +1013,7 @@ feature 'Admin budget investments' do expect(page).not_to have_content(unfeasible_bi.title) expect(page).not_to have_content(feasible_vf_bi.title) - within('#advanced_filters') { find(:css, "#advanced_filters_[value='unfeasible']").set(true) } + within('#advanced_filters') { find(:css, "#advanced_filters_unfeasible").set(true) } click_button 'Filter' expect(page).to have_content(undecided_bi.title) @@ -908,7 +1057,7 @@ feature 'Admin budget investments' do end click_link 'Advanced filters' - within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) } + within('#advanced_filters') { find(:css, "#advanced_filters_selected").set(true) } click_button 'Filter' within("#budget_investment_#{feasible_vf_bi.id}") do @@ -920,7 +1069,9 @@ feature 'Admin budget investments' do scenario "Unselecting an investment", :js do visit admin_budget_budget_investments_path(budget) click_link 'Advanced filters' - within('#advanced_filters') { find(:css, "#advanced_filters_[value='selected']").set(true) } + + within('#advanced_filters') { find(:css, "#advanced_filters_selected").set(true) } + click_button 'Filter' expect(page).to have_content('There are 2 investments')