Browse Source
* feat: split lettings and sales filters * feat: remove logs filter * refactor: type -> filter_type * refactor: slight cleanup * feat: update csv download * feat: set org filters to mirror primary sales and lettings * refactor: linting * feat: fix filter types in org controller - can be changed to org specific labels in future if needed rather than copying filters from primary sales/lettings tabs * refactor: initalize filter_type * refactor: lint * refactor: combine filter_manager into filter_service * refactor: rename to filter manager * refactor: DRY session name filter logic * feat: share session filter name logic * feat: further refactor * refactor: simplify * feat: revert change to filter managerpull/1693/head v0.3.28
natdeanlewissoftwire
2 years ago
committed by
GitHub
18 changed files with 175 additions and 130 deletions
@ -1,23 +0,0 @@
|
||||
module Modules::LogsFilter |
||||
def filtered_logs(logs, search_term, filters) |
||||
all_orgs = params["organisation_select"] == "all" |
||||
FilterService.filter_logs(logs, search_term, filters, all_orgs, current_user) |
||||
end |
||||
|
||||
def load_session_filters(specific_org: false) |
||||
current_filters = session[:logs_filters] |
||||
new_filters = current_filters.present? ? JSON.parse(current_filters) : {} |
||||
current_user.logs_filters(specific_org:).each do |filter| |
||||
new_filters[filter] = params[filter] if params[filter].present? |
||||
end |
||||
params["organisation_select"] == "all" ? new_filters.except("organisation") : new_filters |
||||
end |
||||
|
||||
def session_filters(specific_org: false) |
||||
@session_filters ||= load_session_filters(specific_org:) |
||||
end |
||||
|
||||
def set_session_filters |
||||
session[:logs_filters] = @session_filters.to_json |
||||
end |
||||
end |
@ -1,9 +1,9 @@
|
||||
module Modules::SearchFilter |
||||
def filtered_collection(base_collection, search_term = nil) |
||||
FilterService.filter_by_search(base_collection, search_term) |
||||
FilterManager.filter_by_search(base_collection, search_term) |
||||
end |
||||
|
||||
def filtered_users(base_collection, search_term = nil) |
||||
FilterService.filter_by_search(base_collection, search_term).includes(:organisation) |
||||
FilterManager.filter_by_search(base_collection, search_term).includes(:organisation) |
||||
end |
||||
end |
||||
|
@ -0,0 +1,78 @@
|
||||
class FilterManager |
||||
attr_reader :current_user, :session, :params, :filter_type |
||||
|
||||
def initialize(current_user:, session:, params:, filter_type:) |
||||
@current_user = current_user |
||||
@session = session |
||||
@params = params |
||||
@filter_type = filter_type |
||||
end |
||||
|
||||
def self.filter_by_search(base_collection, search_term = nil) |
||||
if search_term.present? |
||||
base_collection.search_by(search_term) |
||||
else |
||||
base_collection |
||||
end |
||||
end |
||||
|
||||
def self.filter_logs(logs, search_term, filters, all_orgs, user) |
||||
logs = filter_by_search(logs, search_term) |
||||
|
||||
filters.each do |category, values| |
||||
next if Array(values).reject(&:empty?).blank? |
||||
next if category == "organisation" && all_orgs |
||||
|
||||
logs = logs.public_send("filter_by_#{category}", values, user) |
||||
end |
||||
logs = logs.order(created_at: :desc) |
||||
if user.support? |
||||
if logs.first&.lettings? |
||||
logs.all.includes(:owning_organisation, :managing_organisation) |
||||
else |
||||
logs.all.includes(:owning_organisation) |
||||
end |
||||
else |
||||
logs |
||||
end |
||||
end |
||||
|
||||
def serialize_filters_to_session(specific_org: false) |
||||
session[session_name_for(filter_type)] = session_filters(specific_org:).to_json |
||||
end |
||||
|
||||
def session_filters(specific_org: false) |
||||
@session_filters ||= deserialize_filters_from_session(specific_org) |
||||
end |
||||
|
||||
def deserialize_filters_from_session(specific_org) |
||||
current_filters = session[session_name_for(filter_type)] |
||||
new_filters = current_filters.present? ? JSON.parse(current_filters) : {} |
||||
if @filter_type.include?("logs") |
||||
current_user.logs_filters(specific_org:).each do |filter| |
||||
new_filters[filter] = params[filter] if params[filter].present? |
||||
end |
||||
end |
||||
params["organisation_select"] == "all" ? new_filters.except("organisation") : new_filters |
||||
end |
||||
|
||||
def filtered_logs(logs, search_term, filters) |
||||
all_orgs = params["organisation_select"] == "all" |
||||
FilterManager.filter_logs(logs, search_term, filters, all_orgs, current_user) |
||||
end |
||||
|
||||
def bulk_upload |
||||
id = (logs_filters["bulk_upload_id"] || []).reject(&:blank?)[0] |
||||
@bulk_upload ||= current_user.bulk_uploads.find_by(id:) |
||||
end |
||||
|
||||
private |
||||
|
||||
def logs_filters |
||||
JSON.parse(session[session_name_for(filter_type)] || "{}") || {} |
||||
end |
||||
|
||||
def session_name_for(filter_type) |
||||
"#{filter_type}_filters" |
||||
end |
||||
end |
@ -1,48 +0,0 @@
|
||||
class FilterService |
||||
def self.filter_by_search(base_collection, search_term = nil) |
||||
if search_term.present? |
||||
base_collection.search_by(search_term) |
||||
else |
||||
base_collection |
||||
end |
||||
end |
||||
|
||||
def self.filter_logs(logs, search_term, filters, all_orgs, user) |
||||
logs = filter_by_search(logs, search_term) |
||||
|
||||
filters.each do |category, values| |
||||
next if Array(values).reject(&:empty?).blank? |
||||
next if category == "organisation" && all_orgs |
||||
|
||||
logs = logs.public_send("filter_by_#{category}", values, user) |
||||
end |
||||
logs = logs.order(created_at: :desc) |
||||
if user.support? |
||||
if logs.first&.lettings? |
||||
logs.all.includes(:owning_organisation, :managing_organisation) |
||||
else |
||||
logs.all.includes(:owning_organisation) |
||||
end |
||||
else |
||||
logs |
||||
end |
||||
end |
||||
|
||||
attr_reader :current_user, :session |
||||
|
||||
def initialize(current_user:, session:) |
||||
@current_user = current_user |
||||
@session = session |
||||
end |
||||
|
||||
def bulk_upload |
||||
id = (logs_filters["bulk_upload_id"] || []).reject(&:blank?)[0] |
||||
@bulk_upload ||= current_user.bulk_uploads.find_by(id:) |
||||
end |
||||
|
||||
private |
||||
|
||||
def logs_filters |
||||
JSON.parse(session[:logs_filters] || "{}") || {} |
||||
end |
||||
end |
Loading…
Reference in new issue