edge badge
Methods
E
S
Instance Protected methods
expand_hash_conditions_for_aggregates(attrs)

Accepts a hash of SQL conditions and replaces those attributes that correspond to a composed_of relationship with their expanded aggregate attribute values. Given:

class Person < ActiveRecord::Base
  composed_of :address, class_name: "Address",
    mapping: [%w(address_street street), %w(address_city city)]
end

Then:

{ address: Address.new("813 abc st.", "chicago") }
  # => { address_street: "813 abc st.", address_city: "chicago" }
# File activerecord/lib/active_record/sanitization.rb, line 51
def expand_hash_conditions_for_aggregates(attrs)
  expanded_attrs = {}
  attrs.each do |attr, value|
    if aggregation = reflect_on_aggregation(attr.to_sym)
      mapping = aggregation.mapping
      mapping.each do |field_attr, aggregate_attr|
        if mapping.size == 1 && !value.respond_to?(aggregate_attr)
          expanded_attrs[field_attr] = value
        else
          expanded_attrs[field_attr] = value.send(aggregate_attr)
        end
      end
    else
      expanded_attrs[attr] = value
    end
  end
  expanded_attrs
end
sanitize_conditions(condition, table_name = self.table_name)
Alias for: sanitize_sql
sanitize_sql(condition, table_name = self.table_name)
Also aliased as: sanitize_conditions
sanitize_sql_array(ary)

Accepts an array of conditions. The array has each value sanitized and interpolated into the SQL statement.

["name='%s' and group_id='%s'", "foo'bar", 4]  returns  "name='foo''bar' and group_id='4'"
# File activerecord/lib/active_record/sanitization.rb, line 91
def sanitize_sql_array(ary)
  statement, *values = ary
  if values.first.is_a?(Hash) && statement =~ /:\w+/
    replace_named_bind_variables(statement, values.first)
  elsif statement.include?('?')
    replace_bind_variables(statement, values)
  elsif statement.blank?
    statement
  else
    statement % values.collect { |value| connection.quote_string(value.to_s) }
  end
end
sanitize_sql_for_assignment(assignments, default_table_name = self.table_name)

Accepts an array, hash, or string of SQL conditions and sanitizes them into a valid SQL fragment for a SET clause.

{ name: nil, group_id: 4 }  returns "name = NULL , group_id='4'"
# File activerecord/lib/active_record/sanitization.rb, line 32
def sanitize_sql_for_assignment(assignments, default_table_name = self.table_name)
  case assignments
  when Array; sanitize_sql_array(assignments)
  when Hash;  sanitize_sql_hash_for_assignment(assignments, default_table_name)
  else        assignments
  end
end
sanitize_sql_for_conditions(condition, table_name = self.table_name)

Accepts an array or string of SQL conditions and sanitizes them into a valid SQL fragment for a WHERE clause.

["name='%s' and group_id='%s'", "foo'bar", 4]  returns  "name='foo''bar' and group_id='4'"
"name='foo''bar' and group_id='4'" returns "name='foo''bar' and group_id='4'"
Also aliased as: sanitize_sql
# File activerecord/lib/active_record/sanitization.rb, line 18
def sanitize_sql_for_conditions(condition, table_name = self.table_name)
  return nil if condition.blank?

  case condition
  when Array; sanitize_sql_array(condition)
  else        condition
  end
end
sanitize_sql_hash_for_assignment(attrs, table)

Sanitizes a hash of attribute/value pairs into SQL conditions for a SET clause.

{ status: nil, group_id: 1 }
  # => "status = NULL , group_id = 1"
# File activerecord/lib/active_record/sanitization.rb, line 73
def sanitize_sql_hash_for_assignment(attrs, table)
  c = connection
  attrs.map do |attr, value|
    value = type_for_attribute(attr.to_s).serialize(value)
    "#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
  end.join(', ')
end
sanitize_sql_like(string, escape_character = "\\")

Sanitizes a string so that it is safe to use within an SQL LIKE statement. This method uses escape_character to escape all occurrences of “", ”_“ and ”%“

# File activerecord/lib/active_record/sanitization.rb, line 83
def sanitize_sql_like(string, escape_character = "\\")
  pattern = Regexp.union(escape_character, "%", "_")
  string.gsub(pattern) { |x| [escape_character, x].join }
end