diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 0953661e4..da99db004 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -21,6 +21,7 @@ class Budget include ActsAsParanoidAliases include Relationable include Notifiable + include Filterable belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :heading @@ -258,9 +259,10 @@ class Budget def self.apply_filters_and_search(_budget, params, current_filter = nil) investments = all - investments = investments.send(current_filter) if current_filter.present? - investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present? - investments = investments.search(params[:search]) if params[:search].present? + investments = investments.send(current_filter) if current_filter.present? + investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present? + investments = investments.search(params[:search]) if params[:search].present? + investments = investments.filter(params[:advanced_search]) if params[:advanced_search].present? investments end diff --git a/app/views/budgets/investments/index.html.erb b/app/views/budgets/investments/index.html.erb index 4c96d3319..6ca99cf14 100644 --- a/app/views/budgets/investments/index.html.erb +++ b/app/views/budgets/investments/index.html.erb @@ -18,7 +18,7 @@ <%= render '/budgets/investments/header' %> <% end %> - <% if params[:search].present? %> + <% if params[:search].present? || params[:advanced_search].present? %>
@@ -49,6 +49,8 @@
<% end %> + <%= render("shared/advanced_search", search_path: budget_investments_url(@budget)) %> + <%= render('shared/order_links', i18n_namespace: "budgets.investments.index") unless @current_filter == "unfeasible" %> <% @investments.each do |investment| %> diff --git a/spec/features/budgets/budgets_spec.rb b/spec/features/budgets/budgets_spec.rb index 71e0b9a4a..468827b61 100644 --- a/spec/features/budgets/budgets_spec.rb +++ b/spec/features/budgets/budgets_spec.rb @@ -227,8 +227,8 @@ feature 'Budgets' do login_as(level_two_user) visit budget_path(budget) - expect(page).to have_link "Create a budget investment" + end scenario "Unverified user" do diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index f9598c773..9bd9ae458 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -81,6 +81,341 @@ feature 'Budget Investments' do end end + context "Advanced search" do + + scenario "Search by text", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,title: "Get Schwifty") + bdgt_invest2 = create(:budget_investment, heading: heading,title: "Schwifty Hello") + bdgt_invest3 = create(:budget_investment, heading: heading,title: "Do not show me") + + visit budget_investments_path(budget) + + click_link "Advanced search" + fill_in "Write the text", with: "Schwifty" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + context "Search by author type" do + + scenario "Public employee", :js do + ana = create :user, official_level: 1 + john = create :user, official_level: 2 + + bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest3 = create(:budget_investment, heading: heading, author: john) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select Setting['official_level_1_name'], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Municipal Organization", :js do + ana = create :user, official_level: 2 + john = create :user, official_level: 3 + + bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest3 = create(:budget_investment, heading: heading, author: john) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select Setting['official_level_2_name'], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "General director", :js do + ana = create :user, official_level: 3 + john = create :user, official_level: 4 + + bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest3 = create(:budget_investment, heading: heading, author: john) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select Setting['official_level_3_name'], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "City councillor", :js do + ana = create :user, official_level: 4 + john = create :user, official_level: 5 + + bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest3 = create(:budget_investment, heading: heading, author: john) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select Setting['official_level_4_name'], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Mayoress", :js do + ana = create :user, official_level: 5 + john = create :user, official_level: 4 + + bdgt_invest1 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest2 = create(:budget_investment, heading: heading, author: ana) + bdgt_invest3 = create(:budget_investment, heading: heading, author: john) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select Setting['official_level_5_name'], from: "advanced_search_official_level" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + end + + context "Search by date" do + + context "Predefined date ranges" do + + scenario "Last day", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,created_at: 1.minute.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,created_at: 1.hour.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,created_at: 2.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Last 24 hours", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Last week", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,created_at: 1.day.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,created_at: 5.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,created_at: 8.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Last week", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Last month", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,created_at: 10.days.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,created_at: 20.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,created_at: 33.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Last month", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Last year", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,created_at: 300.days.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,created_at: 350.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,created_at: 370.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Last year", from: "js-advanced-search-date-min" + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + end + + scenario "Search by custom date range", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,created_at: 2.days.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,created_at: 3.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,created_at: 9.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Customized", from: "js-advanced-search-date-min" + fill_in "advanced_search_date_min", with: 7.days.ago + fill_in "advanced_search_date_max", with: 1.day.ago + click_button "Filter" + + expect(page).to have_content("There are 2 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).not_to have_content(bdgt_invest3.title) + end + end + + scenario "Search by custom invalid date range", :js do + bdgt_invest1 = create(:budget_investment, heading: heading,created_at: 2.days.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,created_at: 3.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,created_at: 9.days.ago) + + visit budget_investments_path(budget) + + click_link "Advanced search" + select "Customized", from: "js-advanced-search-date-min" + fill_in "advanced_search_date_min", with: 4000.years.ago + fill_in "advanced_search_date_max", with: "wrong date" + click_button "Filter" + + expect(page).to have_content("There are 3 investments") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + expect(page).to have_content(bdgt_invest2.title) + expect(page).to have_content(bdgt_invest3.title) + end + end + + scenario "Search by multiple filters", :js do + ana = create :user, official_level: 1 + john = create :user, official_level: 1 + + bdgt_invest1 = create(:budget_investment, heading: heading,title: "Get Schwifty", author: ana, created_at: 1.minute.ago) + bdgt_invest2 = create(:budget_investment, heading: heading,title: "Hello Schwifty", author: john, created_at: 2.days.ago) + bdgt_invest3 = create(:budget_investment, heading: heading,title: "Save the forest") + + visit budget_investments_path(budget) + + click_link "Advanced search" + fill_in "Write the text", with: "Schwifty" + select Setting['official_level_1_name'], from: "advanced_search_official_level" + select "Last 24 hours", from: "js-advanced-search-date-min" + + click_button "Filter" + + expect(page).to have_content("There is 1 investment") + + within("#budget-investments") do + expect(page).to have_content(bdgt_invest1.title) + end + end + + scenario "Maintain advanced search criteria", :js do + visit budget_investments_path(budget) + click_link "Advanced search" + + fill_in "Write the text", with: "Schwifty" + select Setting['official_level_1_name'], from: "advanced_search_official_level" + select "Last 24 hours", from: "js-advanced-search-date-min" + + click_button "Filter" + + expect(page).to have_content("investments cannot be found") + + within "#js-advanced-search" do + expect(page).to have_selector("input[name='search'][value='Schwifty']") + expect(page).to have_select('advanced_search[official_level]', selected: Setting['official_level_1_name']) + expect(page).to have_select('advanced_search[date_min]', selected: 'Last 24 hours') + end + end + + scenario "Maintain custom date search criteria", :js do + visit budget_investments_path(budget) + click_link "Advanced search" + + select "Customized", from: "js-advanced-search-date-min" + fill_in "advanced_search_date_min", with: 7.days.ago.to_date + fill_in "advanced_search_date_max", with: 1.day.ago.to_date + click_button "Filter" + + expect(page).to have_content("investments cannot be found") + + within "#js-advanced-search" do + expect(page).to have_select('advanced_search[date_min]', selected: 'Customized') + expect(page).to have_selector("input[name='advanced_search[date_min]'][value*='#{7.days.ago.strftime('%Y-%m-%d')}']") + expect(page).to have_selector("input[name='advanced_search[date_max]'][value*='#{1.day.ago.strftime('%Y-%m-%d')}']") + end + end + + end + end end context("Filters") do diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index 7fbb46039..4c8ade0f5 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -514,6 +514,23 @@ describe Budget::Investment do describe "search" do + context "attributes" do + + it "searches by title" do + budget_investment = create(:budget_investment, title: 'save the world') + results = described_class.search('save the world') + expect(results).to eq([budget_investment]) + end + + it "searches by author name" do + author = create(:user, username: 'Danny Trejo') + budget_investment = create(:budget_investment, author: author) + results = described_class.search('Danny') + expect(results).to eq([budget_investment]) + end + + end + context "tags" do it "searches by tags" do investment = create(:budget_investment, tag_list: 'Latina')