Module: Models::SearchableView::ClassMethods

Defined in:
app/concerns/models/searchable_view.rb

Overview

ActiveSupport::Concern mixin: class methods.

Instance Method Summary collapse

Instance Method Details

#append_rep_condition(rep_key, reps) ⇒ ActiveRecord::Relation

A helper method for classic rep conditions

Builds a WHERE clause matching rows whose rep_key column is one
of the given reps, treating the pseudo-rep 'Unassigned' as
rep_key IS NULL.

Parameters:

  • rep_key (Symbol, String)

    rep id column to filter on

  • reps (Array<Integer, String>)

    rep ids; may include 'Unassigned'

Returns:

  • (ActiveRecord::Relation)

    rows matching any of the conditions



66
67
68
69
70
71
72
# File 'app/concerns/models/searchable_view.rb', line 66

def append_rep_condition(rep_key, reps)
  rep_ids = [reps.dup].flatten.compact.uniq
  rep_conditions = []
  rep_conditions << "#{rep_key} IS NULL" if rep_ids.delete('Unassigned')
  rep_conditions << "#{rep_key} IN (#{rep_ids.map(&:to_i).join(',')})" if rep_ids.present?
  where(rep_conditions.map { |c| "(#{c})" }.join(' OR '))
end

#main_resource_classString

Name of the underlying resource class, derived from the view name.

Returns:

  • (String)

    e.g. "Customer" for ViewCustomer



77
78
79
# File 'app/concerns/models/searchable_view.rb', line 77

def main_resource_class
  name.match(/View(.+)/)[1]
end

#main_resource_tableString

Tableized name of the underlying resource class.

Returns:

  • (String)

    e.g. "customers" for ViewCustomer



84
85
86
# File 'app/concerns/models/searchable_view.rb', line 84

def main_resource_table
  main_resource_class.tableize
end

#ransackable_scopes(_auth_object = nil) ⇒ Array<Symbol>

Expose rep includes scopes to Ransack so they are callable via ransack(query_params).
Each scope is generated conditionally based on which rep columns the view has.

Parameters:

  • _auth_object (Object, nil) (defaults to: nil)

    unused Ransack auth object

Returns:

  • (Array<Symbol>)

    scope names Ransack may call



100
101
102
103
104
105
# File 'app/concerns/models/searchable_view.rb', line 100

def ransackable_scopes(_auth_object = nil)
  %i[primary_sales_rep_id_includes secondary_sales_rep_id_includes
     local_sales_rep_id_includes service_rep_id_includes
     technical_support_rep_id_includes opportunity_local_sales_rep_id_includes
     customer_local_sales_rep_id_includes]
end

#select_sort_columnsArray<Array(String, String)>

Humanized column labels paired with column names, for sort pickers.

Returns:

  • (Array<Array(String, String)>)

    [label, column_name] pairs sorted by column name



91
92
93
# File 'app/concerns/models/searchable_view.rb', line 91

def select_sort_columns
  column_names.sort.map { |cn| [human_attribute_name(cn), cn] }
end