Class: ActivitySearch
- Inherits:
-
Search
- Object
- ActiveRecord::Base
- ApplicationRecord
- Search
- ActivitySearch
- Includes:
- Models::SearchAddressable
- Defined in:
- app/models/activity_search.rb
Overview
== Schema Information
Table name: searches
Database name: primary
id :integer not null, primary key
name :string(255)
persist :boolean default(FALSE)
pinned :boolean default(FALSE), not null
query_params :jsonb
result_set_size :integer default(0)
selected_columns :string is an Array
set_limit :integer default(25)
sort_column :string(255)
sort_columns :string default([]), not null, is an Array
sort_direction :string(255) default("ASC")
type :string(255) not null
created_at :datetime
updated_at :datetime
employee_id :integer
Indexes
employee_id_pinned (employee_id,pinned)
employee_id_type (employee_id,type)
Constant Summary
Constants inherited from Search
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary
Attributes inherited from Search
#composite_columns, #pagy_count, #pagy_limit, #pagy_page, #sql_select_columns
Class Method Summary collapse
Instance Method Summary collapse
- #address_id_fields ⇒ Object
- #apply_custom_criteria(results) ⇒ Object
- #mass_assign_campaign(params, cur_user = nil) ⇒ Object
- #mass_spread(params, cur_user = nil) ⇒ Object
-
#mass_update(params, cur_user = nil) ⇒ Object
Mass actions section #.
-
#normalize_for_mass_update(resource, params) ⇒ Object
Called by SearchResourceUpdateWorker for each activity before saving.
- #pinned_query ⇒ Object
Methods included from Models::SearchAddressable
Methods inherited from Search
#all_composite_columns, allowed_role_ids, #append_custom_column, #append_ouput_column, #append_to_sql_select_columns, #applicable_sort_terms, #apply_array_match, #apply_criteria, #apply_customer_cross_reference, #apply_distance_search, #apply_select, #apply_sort, #available_events, available_output_columns, #available_output_columns, base_search_class_name, #cleanup_search_results, composite_column, #composite_column, database_columns, default_columns, default_sort, #discard_excess, #effective_name, #effective_short_name, #employee, #enqueue_navbar_pinned_refresh, #fast_count, favorites, friendly_column_name, #get_pinned_results, #get_sort_term, global_favorite, has_role_for_search?, #human_query_params, instantiate_query_template, #instantiate_resource_updater, #limit_options, main_resource_class, main_resource_table, mass_actions_for_select, #mass_export, maximum_unpersisted_queries, options, options_classes, #output_columns_for_select, #pagy_from_search, #perform, #perform_selected, #prepend_custom_column, #prepend_output_column, query_favorites_templates, #ransack_probe_params, recent_searches_for_employee_id, #record_list, #refresh_pinned_results, #remove_other_pins, search_name, #search_name, #search_results, search_type, search_type_humanized, select_sort_columns, #select_sort_columns, #select_statement, #set_defaults, #set_sort_term, #sort_column1, #sort_column1=, #sort_column2, #sort_column2=, #sort_column3, #sort_column3=, #sort_columns_for_select, #sort_columns_to_hash, #target_geocoding, unpersisted_queries_quote_reached?, #unrecord_list, #validated_selected_columns, view_resource_class, view_resource_klass, #view_resource_klass, view_resource_table, visible?, with_search_results
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Class Method Details
.custom_criteria_keys ⇒ Object
84 85 86 |
# File 'app/models/activity_search.rb', line 84 def self.custom_criteria_keys %w[activity_type_id activity_result_type_id assigned_resource_id] end |
Instance Method Details
#address_id_fields ⇒ Object
80 81 82 |
# File 'app/models/activity_search.rb', line 80 def address_id_fields [['Primary Address', 'primary_address_id']] end |
#apply_custom_criteria(results) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/activity_search.rb', line 88 def apply_custom_criteria(results) results = apply_customer_cross_reference(results, :party_id) results = apply_array_match(results, :activity_type_id, nil, 'Note', 'Any') do |values| values += ActivityType.sales_activities.ids if values.delete('AnySales') values end results = apply_array_match(results, :activity_result_type_id, nil, 'Open', 'Any') do |values| values += ActivityResultType.completed.ids if values.delete('AnyCompletion') values end apply_array_match(results, :assigned_resource_id, nil, 'unassigned', 'anybody') end |
#mass_assign_campaign(params, cur_user = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/activity_search.rb', line 63 def mass_assign_campaign(params, cur_user = nil) campaign_id = params.dig(:campaign, :campaign_id) return { status: :error, message: 'Campaign not selected' } if campaign_id.blank? jid = ::SearchResourceUpdateWorker.perform_async( 'search_id' => id, 'action_params' => { 'resource_params' => { 'campaign_id' => campaign_id } }, 'user_id' => cur_user&.id, 'locale' => I18n.locale.to_s ) { status: :ok, job_id: jid } end |
#mass_spread(params, cur_user = nil) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/activity_search.rb', line 137 def mass_spread(params, cur_user = nil) return { status: :error, message: 'Please specify a start date and number of activities per day to schedule' } \ unless params[:start_date].present? && params[:max_per_day].to_i > 0 jid = MassSearch::ActivitySpreadWorker.perform_async( 'search_id' => id, 'action_params' => { 'start_date' => params[:start_date], 'max_per_day' => params[:max_per_day] }, 'user_id' => cur_user&.id, 'locale' => I18n.locale.to_s ) { status: :ok, job_id: jid } end |
#mass_update(params, cur_user = nil) ⇒ Object
Mass actions section #
107 108 109 110 111 112 113 114 115 |
# File 'app/models/activity_search.rb', line 107 def mass_update(params, cur_user = nil) jid = ::SearchResourceUpdateWorker.perform_async( 'search_id' => id, 'action_params' => { 'resource_params' => (params[:resource] || {}).to_h }, 'user_id' => cur_user&.id, 'locale' => I18n.locale.to_s ) { status: :ok, job_id: jid } end |
#normalize_for_mass_update(resource, params) ⇒ Object
Called by SearchResourceUpdateWorker for each activity before saving.
Normalises result type sentinel values, resolves "assign to primary rep"
shorthand, and defers ChainRunner to the async handler.
Returns false to skip the record and count it as an error.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/models/activity_search.rb', line 121 def normalize_for_mass_update(resource, params) resource.activity_result_type_id = nil if resource.activity_result_type_id.to_i < 0 if params[:assigned_resource_id].to_s == '0' rep = resource.try(:party).try(:customer).try(:primary_sales_rep) unless rep Rails.logger.warn "normalize_for_mass_update: no primary rep for activity #{resource.id} party #{resource.try(:party_id)}" return false end resource.assigned_resource_id = rep.id end resource.halt_chain = true true end |
#pinned_query ⇒ Object
76 77 78 |
# File 'app/models/activity_search.rb', line 76 def pinned_query search_results.joins('left join activities on activities.id = search_results.resource_id').includes(resource: %i[activity_type party]) end |