Browse Source

spiked the search using id and tennant code

pull/608/head
JG 3 years ago
parent
commit
6af1c60ad9
  1. 6
      app/controllers/case_logs_controller.rb
  2. 4
      app/models/case_log.rb
  3. 2
      spec/features/log_spec.rb
  4. 2
      spec/requests/case_logs_controller_spec.rb

6
app/controllers/case_logs_controller.rb

@ -12,6 +12,12 @@ class CaseLogsController < ApplicationController
@pagy, @case_logs = pagy(filtered_case_logs(current_user.case_logs)) @pagy, @case_logs = pagy(filtered_case_logs(current_user.case_logs))
search_param = params["search-field"]
if search_param
@pagy, @case_logs = pagy(@case_logs.search_by(search_param))
end
respond_to do |format| respond_to do |format|
format.html format.html
format.csv do format.csv do

4
app/models/case_log.rb

@ -51,6 +51,10 @@ class CaseLog < ApplicationRecord
end end
} }
scope :search_by_id, ->(id) { where(id: id) }
scope :search_by_tenant_code, ->(code) { where(tenant_code: code) }
scope :search_by, ->(param) { search_by_id(param.to_i).or(search_by_tenant_code(param)) }
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenant_code propcode].freeze OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenant_code propcode].freeze
RENT_TYPE_MAPPING = { 0 => 1, 1 => 2, 2 => 2, 3 => 3, 4 => 3, 5 => 3 }.freeze RENT_TYPE_MAPPING = { 0 => 1, 1 => 2, 2 => 2, 3 => 3, 4 => 3, 5 => 3 }.freeze

2
spec/features/log_spec.rb

@ -22,7 +22,7 @@ RSpec.describe "Log Features" do
end end
it "displays log matching the search" do it "displays log matching the search" do
fill_in("search-field", with: log.id ) fill_in("search-field", with: log.id)
click_button("Search") click_button("Search")
expect(page).to have_content(log.id) expect(page).to have_content(log.id)

2
spec/requests/case_logs_controller_spec.rb

@ -187,7 +187,7 @@ RSpec.describe CaseLogsController, type: :request do
context "with a search bar" do context "with a search bar" do
let!(:logs) { FactoryBot.create_list(:case_log, 5) } let!(:logs) { FactoryBot.create_list(:case_log, 5) }
it "shows case logs mathing the search word" do it "shows case logs matching the search word" do
get "/logs?search-field=#{logs.first.id}", headers: headers, params: {} get "/logs?search-field=#{logs.first.id}", headers: headers, params: {}
expect(page).to have_content(logs.first.id) expect(page).to have_content(logs.first.id)
expect(page).not_to have_content(logs.last.id) expect(page).not_to have_content(logs.last.id)

Loading…
Cancel
Save