module RecordSelect::Conditions

Protected Instance Methods

build_record_select_conditions(tokens, operator, search_pattern) click to toggle source
# File lib/record_select/conditions.rb, line 56
def build_record_select_conditions(tokens, operator, search_pattern)
  where_clauses = record_select_config.search_on.collect { |sql| "#{sql} #{operator} ?" }
  phrase = "(#{where_clauses.join(' OR ')})"
  sql = ([phrase] * tokens.length).join(' AND ')
  
  tokens = tokens.collect { |token| [search_pattern.sub('?', token)] * record_select_config.search_on.length }.flatten
  [sql, *tokens]
end
full_text_search?() click to toggle source
# File lib/record_select/conditions.rb, line 52
def full_text_search?
  record_select_config.full_text_search? && params[:rs_mode].blank? || params[:rs_mode] == 'contains'
end
record_select_condition_for_column(column, value) click to toggle source

generates an SQL condition for the given column/value

# File lib/record_select/conditions.rb, line 91
def record_select_condition_for_column(column, value)
  model = record_select_config.model
  if value.is_a? Array
    {column.name => value}
  elsif value.blank? and column.null
    {column.name => nil}
  elsif [:string, :text].include? column.type
    column_name = model.quoted_table_name + '.' + model.connection.quote_column_name(column.name)
    ["LOWER(#{column_name}) LIKE ?", value]
  else
    {column.name => record_select_type_cast(column, value)}
  end
end
record_select_conditions() click to toggle source

returns the combination of all conditions. conditions come from:

  • current search (params)

  • intelligent url params (e.g. params if first_name is a model column)

  • specific conditions supplied by the developer

# File lib/record_select/conditions.rb, line 9
def record_select_conditions
  conditions = []

  [
    record_select_conditions_from_search,
    *record_select_conditions_from_params,
    record_select_conditions_from_controller
  ].compact
end
record_select_conditions_from_controller() click to toggle source

an override method. here you can provide custom conditions to define the selectable records. useful for situational restrictions.

# File lib/record_select/conditions.rb, line 21
def record_select_conditions_from_controller; end
record_select_conditions_from_params() click to toggle source

generate conditions from the url parameters (e.g. users/browse?group_id=5)

# File lib/record_select/conditions.rb, line 66
def record_select_conditions_from_params
  conditions = []
  ignored_columns = %w[controller action page search update]
  params.each do |field, value|
    next if field.in? ignored_columns
    column = record_select_config.model.columns_hash[field]
    conditions << record_select_condition_for_column(column, value) if column
  end
  conditions
end
record_select_includes() click to toggle source

another override method. define any association includes you want for the finder search.

# File lib/record_select/conditions.rb, line 25
def record_select_includes; end
record_select_like_operator() click to toggle source
# File lib/record_select/conditions.rb, line 27
def record_select_like_operator
  @like_operator ||= ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
end
record_select_select() click to toggle source

define special list of selected fields, mainly to define extra fields that can be used for specialized sorting.

# File lib/record_select/conditions.rb, line 34
def record_select_select
end
record_select_type_cast(column, value) click to toggle source
# File lib/record_select/conditions.rb, line 77
def record_select_type_cast(column, value)
  if Rails.version < '4.2'
    column.type_cast value
  elsif Rails.version < '5.0'
    column.type_cast_from_user value
  elsif column.type.respond_to? :cast # jruby-jdbc and rails 5
    column.type.cast value
  else
    cast_type = ActiveModel::Type.lookup column.type
    cast_type ? cast_type.cast(value) : value
  end
end