Class: Ransack::Adapters::ActiveRecord::Context
- 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,
lib/ransack/adapters/active_record/3.2/context.rb
Constant Summary collapse
- JoinDependency =
Because the AR::Associations namespace is insane
::ActiveRecord::Associations::JoinDependency
- JoinPart =
JoinDependency::JoinPart
- JoinBase =
JoinDependency::JoinBase
Instance Attribute Summary
Attributes inherited from Context
#arel_visitor, #auth_object, #base, #engine, #klass, #object, #search, #search_key
Instance Method Summary collapse
- #alias_tracker ⇒ Object
- #attribute_method?(str, klass = @klass) ⇒ Boolean
- #evaluate(search, opts = {}) ⇒ Object
-
#initialize(object, options = {}) ⇒ Context
constructor
Redefine a few things for ActiveRecord 3.2.
-
#join_associations ⇒ Object
All dependent JoinAssociation items used in the search query.
-
#join_sources ⇒ Object
All dependent Arel::Join nodes used in the search query.
- #klassify(obj) ⇒ Object
- #relation_for(object) ⇒ Object
- #table_for(parent) ⇒ Object
- #type_for(attr) ⇒ Object
Methods inherited from Context
#association_path, #bind, #chain_scope, #contextualize, for, for_class, for_object, #ransackable_association?, #ransackable_attribute?, #ransackable_scope?, #scope_arity, #searchable_associations, #searchable_attributes, #sortable_attributes, #traverse, #unpolymorphize_association
Constructor Details
#initialize(object, options = {}) ⇒ Context
Redefine a few things for ActiveRecord 3.2.
16 17 18 19 |
# File 'lib/ransack/adapters/active_record/3.0/context.rb', line 16 def initialize(object, = {}) super @arel_visitor = @engine.connection.visitor end |
Instance Method Details
#alias_tracker ⇒ Object
131 132 133 |
# File 'lib/ransack/adapters/active_record/context.rb', line 131 def alias_tracker @join_dependency.alias_tracker end |
#attribute_method?(str, klass = @klass) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ransack/adapters/active_record/context.rb', line 43 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(Constants::UNDERSCORE) ) if found_assoc = get_association(assoc, klass) exists = attribute_method?( remainder.join(Constants::UNDERSCORE), poly_class || found_assoc.klass ) end end end exists end |
#evaluate(search, opts = {}) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/ransack/adapters/active_record/context.rb', line 34 def evaluate(search, opts = {}) viz = Visitor.new relation = @object.where(viz.accept(search.base)) if search.sorts.any? relation = relation.except(:order).reorder(viz.accept(search.sorts)) end opts[:distinct] ? relation.distinct : relation end |
#join_associations ⇒ Object
All dependent JoinAssociation items used in the search query
116 117 118 119 |
# File 'lib/ransack/adapters/active_record/context.rb', line 116 def join_associations raise NotImplementedError, "ActiveRecord 4.1 and later does not use join_associations. Use join_sources." end |
#join_sources ⇒ Object
All dependent Arel::Join nodes used in the search query
This could otherwise be done as ‘@object.arel.join_sources`, except that ActiveRecord’s build_joins sets up its own JoinDependency. This extracts what we need to access the joins using our existing JoinDependency to track table aliases.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ransack/adapters/active_record/context.rb', line 96 def join_sources base = if ::ActiveRecord::VERSION::MAJOR >= 5 Arel::SelectManager.new(@object.table) else Arel::SelectManager.new(@object.engine, @object.table) end joins = @join_dependency.join_constraints(@object.joins_values) joins.each do |aliased_join| base.from(aliased_join) end base.join_sources end |
#klassify(obj) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ransack/adapters/active_record/context.rb', line 70 def klassify(obj) if Class === obj && ::ActiveRecord::Base > obj obj elsif obj.respond_to? :klass obj.klass elsif obj.respond_to? :base_klass obj.base_klass else raise ArgumentError, "Don't know how to klassify #{obj}" end end |
#relation_for(object) ⇒ Object
19 20 21 |
# File 'lib/ransack/adapters/active_record/context.rb', line 19 def relation_for(object) object.all end |
#table_for(parent) ⇒ Object
66 67 68 |
# File 'lib/ransack/adapters/active_record/context.rb', line 66 def table_for(parent) parent.table end |
#type_for(attr) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ransack/adapters/active_record/context.rb', line 23 def type_for(attr) return nil unless attr && attr.valid? name = attr.arel_attribute.name.to_s table = attr.arel_attribute.relation.table_name connection = attr.klass.connection unless connection.table_exists?(table) raise "No table named #{table} exists" end connection.schema_cache.columns_hash(table)[name].type end |