Class: Ransack::Adapters::ActiveRecord::Context

Inherits:
Context
  • Object
show all
Defined in:
lib/ransack/adapters/active_record/context.rb,
lib/ransack/adapters/active_record/3.0/context.rb,
lib/ransack/adapters/active_record/3.1/context.rb

Constant Summary collapse

JoinDependency =

Because the AR::Associations namespace is insane

::ActiveRecord::Associations::JoinDependency
JoinBase =
JoinDependency::JoinBase
JoinPart =
JoinDependency::JoinPart

Instance Attribute Summary

Attributes inherited from Context

#arel_visitor, #auth_object, #base, #engine, #klass, #object, #search, #search_key

Instance Method Summary collapse

Methods inherited from Context

#association_path, #bind, #contextualize, for, for_class, for_object, #ransackable_association?, #ransackable_attribute?, #searchable_associations, #searchable_attributes, #traverse, #unpolymorphize_association

Constructor Details

#initialize(object, options = {}) ⇒ Context

Redefine a few things that have changed with 3.2.



13
14
15
16
# File 'lib/ransack/adapters/active_record/context.rb', line 13

def initialize(object, options = {})
  super
  @arel_visitor = @engine.connection.visitor
end

Instance Method Details

#attribute_method?(str, klass = @klass) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ransack/adapters/active_record/3.0/context.rb', line 28

def attribute_method?(str, klass = @klass)
  exists = false

  if ransackable_attribute?(str, klass)
    exists = true
  elsif (segments = str.split(/_/)).size > 1
    remainder = []
    found_assoc = nil
    while !found_assoc && remainder.unshift(segments.pop) && segments.size > 0 do
      assoc, poly_class = unpolymorphize_association(segments.join('_'))
      if found_assoc = get_association(assoc, klass)
        exists = attribute_method?(remainder.join('_'), poly_class || found_assoc.klass)
      end
    end
  end

  exists
end

#evaluate(search, opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ransack/adapters/active_record/context.rb', line 30

def evaluate(search, opts = {})
  viz = Visitor.new
  relation = @object.where(viz.accept(search.base))
  if search.sorts.any?
    relation = relation.except(:order).order(viz.accept(search.sorts))
  end
  opts[:distinct] ? relation.uniq : relation
end

#klassify(obj) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ransack/adapters/active_record/3.0/context.rb', line 51

def klassify(obj)
  if Class === obj && ::ActiveRecord::Base > obj
    obj
  elsif obj.respond_to? :klass
    obj.klass
  elsif obj.respond_to? :active_record
    obj.active_record
  else
    raise ArgumentError, "Don't know how to klassify #{obj}"
  end
end

#table_for(parent) ⇒ Object



47
48
49
# File 'lib/ransack/adapters/active_record/3.0/context.rb', line 47

def table_for(parent)
  parent.table
end

#type_for(attr) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ransack/adapters/active_record/context.rb', line 18

def type_for(attr)
  return nil unless attr && attr.valid?
  name    = attr.arel_attribute.name.to_s
  table   = attr.arel_attribute.relation.table_name

  unless @engine.connection.table_exists?(table)
    raise "No table named #{table} exists"
  end

  @engine.connection.schema_cache.columns_hash[table][name].type
end