aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/predicate_builder.rb
Commit message (Collapse)AuthorAgeFilesLines
...
* Eagerly cast range values in the predicate builderSean Griffin2014-12-261-1/+1
| | | | | | | | | A custom object is required for this, as you cannot build a range object out of `Arel::Nodes::Quoted` objects. Depends on the changes introduced in https://github.com/rails/arel/commit/cf03bd45e39def057a2f63e42a3391b7d750dece /cc @mrgilman
* Perform casting of single values within the predicate builderSean Griffin2014-12-261-4/+4
| | | | | | | | | | | As part of the larger refactoring to remove type casting from Arel, we need to do the casting of values eagerly. The predicate builder is the closest place that knows about the Active Record class, and can therefore have the type information. /cc @mrgilman [Sean Griffin & Melanie Gilman]
* Remove `klass` and `arel_table` as a dependency of `PredicateBuilder`Sean Griffin2014-12-261-20/+8
| | | | | | | | | | | | | | | This class cares far too much about the internals of other parts of Active Record. This is an attempt to break out a meaningful object which represents the needs of the predicate builder. I'm not fully satisfied with the name, but the general concept is an object which represents a table, the associations to/from that table, and the types associated with it. Many of these exist at the `ActiveRecord::Base` class level, not as properties of the table itself, hence the need for another object. Currently it provides these by holding a reference to the class, but that will likely change in the future. This allows the predicate builder to remain wholy concerned with building predicates. /cc @mrgilman
* Refactor association handling in `PredicateBuilder`Sean Griffin2014-12-261-21/+4
| | | | | | | | | | I'm attempting to remove `klass` as a dependency of the predicate builder, in favor of an object that better represents what we're using it for. The only part of this which doesn't fit nicely into that picture is the check for an association being polymorphic. Since I'm not yet sure what that is going to look like, I've moved this logic into another class in an attempt to separate things that will change from things that won't.
* Re-use the predicate builder in the `ArrayHandler`Sean Griffin2014-12-261-5/+5
| | | | | | | | | | This reduces the number of places which will need to care about single value or range specific logic as we introduce type casting. The array handler is only responsible for producing `in` statements. /cc @mrgilman [Sean Griffin & Melanie Gilman]
* Change `PredicateBuilder` handler methods to instance methodsSean Griffin2014-12-261-30/+25
| | | | | | | | | | | | This will allow us to pass the predicate builder into the constructor of these handlers. The procs had to be changed to objects, because the `PredicateBuilder` needs to be marshalable. If we ever decide to make `register_handler` part of the public API, we should come up with a better solution which allows procs. /cc @mrgilman [Sean Griffin & Melanie Gilman]
* Deprecate `Class` handler in `PredicateBuilder`Melanie Gilman2014-12-041-3/+9
| | | | Users should pass strings to queries instead of classes
* Refactor `build_from_hash` to convert dot notation to hash firstMelanie Gilman2014-12-021-29/+34
| | | | | | | | | | This ensures that we're handling all forms of nested tables the same way. We're aware that the `convert_dot_notation_to_hash` method will cause a performance hit, and we intend to come back to it once we've refactored some of the surrounding code. [Melissa Xie & Melanie Gilman]
* Refactor `PredicateBuilder` from singleton to instanceMelanie Gilman2014-12-021-14/+22
|
* Stop using `Arel::Table.engine`Sean Griffin2014-11-291-2/+2
| | | | | | | | | | | | | We never actually make use of it on the table, since we're constructing the select manager manually. It looks like if we ever actually were grabbing it from the table, we're grossly misusing it since it's meant to vary by AR class. Its existence on `Arel::Table` appears to be purely for convenience methods that are never used outside of tests. However, in production code it just complicates construction of the tables on the rails side, and the plan is to remove it from `Arel::Table` entirely. I'm not convinced it needs to live on `SelectManager`, etc either.
* Use `#between`, rather than `#in` for passing Ranges to ArelSean Griffin2014-10-301-1/+1
| | | | Passing ranges to `#in` has been deprecated in Arel.
* No need to call to_sym hereGodfrey Chan2014-09-201-2/+2
| | | | | The hash is now string-keyed, and [_]reflect_on_association calls `to_s` on the argument anyway.
* Merge pull request #15210 from arthurnn/fix_hbtm_reflectionArthur Neves2014-05-241-2/+2
| | | | | | | | | Fix habtm reflection Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/counter_cache.rb activerecord/lib/active_record/reflection.rb activerecord/test/cases/reflection_test.rb
* Give real privacy to class methods in AR::PredicateBuilderHector Satre2014-05-011-7/+8
|
* Better support for `where()` conditions that use an association name.Martin Emde2013-12-161-3/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using the name of an association in `where` previously worked only if the value was a single `ActiveRecrd::Base` object. e.g. Post.where(author: Author.first) Any other values, including `nil`, would cause invalid SQL to be generated. This change supports arguments in the `where` query conditions where the key is a `belongs_to` association name and the value is `nil`, an `Array` of `ActiveRecord::Base` objects, or an `ActiveRecord::Relation` object. # Given the Post model class Post < ActiveRecord::Base belongs_to :author end # nil value finds records where the association is not set Post.where(author: nil) # SELECT "posts".* FROM "posts" WHERE "posts"."author_id" IS NULL # Array values find records where the association foreign key # matches the ids of the passed ActiveRecord models, resulting # in the same query as Post.where(author_id: [1,2]) authors_array = [Author.find(1), Author.find(2)] Post.where(author: authors_array) # ActiveRecord::Relation values find records using the same # query as Post.where(author_id: Author.where(last_name: "Emde")) Post.where(author: Author.where(last_name: "Emde")) Polymorphic `belongs_to` associations will continue to be handled appropriately, with the polymorphic `association_type` field added to the query to match the base class of the value. This feature previously only worked when the value was a single `ActveRecord::Base`. class Post < ActiveRecord::Base belongs_to :author, polymorphic: true end Post.where(author: Author.where(last_name: "Emde")) # Generates a query similar to: Post.where(author_id: Author.where(last_name: "Emde"), author_type: "Author")
* check class hierarchy with is_a? in PredicateBuilder.expandMikhail Dieterle2013-08-271-1/+1
| | | | add changelog entry for #11945
* Add ability to specify how a class is converted to Arel predicatesgrif2013-07-281-35/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This adds the ability for rails apps or gems to have granular control over how a domain object is converted to sql. One simple use case would be to add support for Regexp. Another simple case would be something like the following: class DateRange < Struct.new(:start, :end) def include?(date) (start..end).cover?(date) end end class DateRangePredicate def call(attribute, range) attribute.in(range.start..range.end) end end ActiveRecord::PredicateBuilder.register_handler(DateRange, DateRangePredicate.new) More complex cases might include taking a currency object and converting it from EUR to USD before performing the query. By moving the existing handlers to this format, we were also able to nicely refactor a rather nasty method in PredicateBuilder.
* resolve aliases before passing the hash to the predicate builderAaron Patterson2013-07-021-4/+10
|
* we don't need to to_s the columnAaron Patterson2013-07-011-1/+1
|
* the data structure used to store attribute aliases should not be exposedAaron Patterson2013-07-011-2/+2
|
* Handle aliased attributes in ActiveRecord::Relation.Godfrey Chan2013-05-011-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | When using symbol keys, ActiveRecord will now translate aliased attribute names to the actual column name used in the database: With the model class Topic alias_attribute :heading, :title end The call Topic.where(heading: 'The First Topic') should yield the same result as Topic.where(title: 'The First Topic') This also applies to ActiveRecord::Relation::Calculations calls such as `Model.sum(:aliased)` and `Model.pluck(:aliased)`. This will not work with SQL fragment strings like `Model.sum('DISTINCT aliased')`. Github #7839 *Godfrey Chan*
* stop calling to_sym when building arel nodes [CVE-2013-1854]Aaron Patterson2013-03-151-1/+1
|
* Revert "Merge pull request #9207 from dylanahsmith/mysql-quote-numeric"Steve Klabnik2013-02-271-5/+0
| | | | | This reverts commit 408227d9c5ed7de26310d72a1a99c1ee02311c63, reversing changes made to dca0b57d03deffc933763482e615c3cf0b9a1d97.
* Use IN operator like arel for empty hash in where clauserobertomiranda2013-02-091-1/+1
|
* Reverting e170014113 (Change behaviour with empty hash in where clause)Guillermo Iguaran2013-02-081-1/+1
|
* Reverting 16f6f25 (Change behaviour with empty array in where clause)Guillermo Iguaran2013-02-081-2/+0
|
* Change behaviour with empty array in where clauserobertomiranda2013-02-081-0/+2
|
* Change behaviour with empty hash in where clauserobertomiranda2013-02-081-1/+1
|
* active_record: Quote numeric values compared to string columns.Dylan Smith2013-02-071-0/+5
|
* reduce the number of queries on IN clauses, fix relation queries in `where`Aaron Patterson2013-01-241-1/+1
|
* stop converting strings to symbolsAaron Patterson2013-01-241-1/+1
|
* Refactor predicate builder when receiving empty hashCarlos Antonio da Silva2013-01-171-4/+4
| | | | | | | There's no need to create a new arel table or reflect on the column association if the value is empty, these attributes are not used. Also no need to concat a new array, just append the query value.
* 1.9 Syntax related changesAvnerCohen2012-11-101-2/+2
|
* Remove ActiveRecord::ModelJon Leighton2012-10-261-3/+3
| | | | | | | | | | In the end I think the pain of implementing this seamlessly was not worth the gain provided. The intention was that it would allow plain ruby objects that might not live in your main application to be subclassed and have persistence mixed in. But I've decided that the benefit of doing that is not worth the amount of complexity that the implementation introduced.
* rename AR::Model::Tag to AR::Tag - fixes #7714Francesco Rodriguez2012-09-201-1/+1
|
* fix querying with an empty hashDamien Mathieu2012-09-191-2/+6
| | | | Closes #6960
* Pass in the model class rather than engineJon Leighton2012-09-131-5/+5
| | | | | | | | | In some circumstances engine was Arel::Table.engine which for separate reasons was an ActiveRecord::Model::DeprecationProxy, which caused a deprecation warning. In any case, we want the actual model class here, since we want to use it to infer information about associations.
* Refactor to remove some duplicationJon Leighton2012-09-121-37/+20
|
* Fix nested association referencesJon Leighton2012-09-121-3/+4
| | | | | Previously the reflection would be looked up on the wrong class. However the test passed because the examples referred back to themselves.
* Accept belongs_to assoc. keys in ActiveRecord queriesbeerlington2012-09-111-4/+48
| | | | | | | | | | | | | Allows you to specify the model association key in a belongs_to relationship instead of the foreign key. The following queries are now equivalent: Post.where(:author_id => Author.first) Post.where(:author => Author.first) PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure) PriceEstimate.where(:estimate_of => treasure)
* Remove conditional committed by accidentSantiago Pastorino2012-06-091-1/+1
|
* Use each_with_object instead of each hereSantiago Pastorino2012-06-091-1/+1
|
* predicate builder should not recurse for determining where columns.Aaron Patterson2012-05-301-1/+1
| | | | | | Thanks to Ben Murphy for reporting this CVE-2012-2661
* CollectionProxy < RelationJon Leighton2012-05-111-3/+3
| | | | | | | | | | | | | | | | | | | | | This helps bring the interfaces of CollectionProxy and Relation closer together, and reduces the delegation backflips we need to perform. For example, first_or_create is defined thus: class ActiveRecord::Relation def first_or_create(...) first || create(...) end end If CollectionProxy < Relation, then post.comments.first_or_create will hit the association's #create method which will actually add the new record to the association, just as post.comments.create would. With the previous delegation, post.comments.first_or_create expands to post.comments.scoped.first_or_create, where post.comments.scoped has no knowledge of the association.
* Remove Arel::Relation constant from PredicateBuilder.Juanjo Bazán2012-03-271-2/+2
|
* Refactor and cleanup in some ActiveRecord modulesCarlos Antonio da Silva2012-03-031-14/+13
| | | | | | | | | | | * Avoid double hash lookups in AR::Reflection when reflecting associations/aggregations * Minor cleanups: use elsif, do..end, if..else instead of unless..else * Simplify DynamicMatchers#respond_to? * Use "where" instead of scoped with conditions hash * Extract `scoped_by` method pattern regexp to constant * Extract noisy class_eval from method_missing in dynamic matchers * Extract readonly check, avoid calling column#to_s twice in persistence * Refactor predicate builder, remove some variables
* automatically add references when we canJon Leighton2012-01-161-0/+12
|
* refactor AR::PredicateBuilder.build_from_hashAkira Matsuda2011-12-291-35/+37
|
* where(foo: [1, nil]) becomes "WHERE foo = 1 OR foo IS NULL"Akira Matsuda2011-12-281-1/+4
| | | | was "WHERE foo IN (1) OR foo IS NULL" before
* no need to compact an already compacted ArrayAkira Matsuda2011-12-281-1/+1
|