aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/inheritance_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Add AR::Base.base_class? predicateBogdan Gusiev2018-04-021-0/+9
|
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-14/+14
|
* Fix inheritance object creation from relationRyuta Kamizono2017-12-131-0/+39
| | | | | | | | | | We need to pass scope attributes to `klass.new` to detect subclass. Otherwise `subclass_from_attributes` can't detect subclass which is had in scope attributes. Fixes #18062. Closes #18227. Closes #30720.
* Clarify base_class tests on abstract STI vs concrete STIYukio Mizuta2017-08-121-4/+9
|
* Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* Merge pull request #29732 from kirs/frozen-activerecordRafael França2017-07-211-0/+2
|\ | | | | Use frozen-string-literal in ActiveRecord
| * Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| |
* | Revert "Extract `bind_param` and `bind_attribute` into `ActiveRecord::TestCase`"Sean Griffin2017-07-211-0/+1
|/ | | | | | | | This reverts commit b6ad4052d18e4b29b8a092526c2beef013e2bf4f. This is not something that the majority of Active Record should be testing or care about. We should look at having fewer places rely on these details, not make it easier to rely on them.
* Make preload query to preparableRyuta Kamizono2017-07-071-1/+1
| | | | | | | Currently preload query cannot be prepared statements even if `prepared_statements: true` due to array handler in predicate builder doesn't support making bind params. This makes preload query to preparable by don't passing array value if possible.
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Fix `default_scoped` with defined `default_scope` on STI modelRyuta Kamizono2017-05-311-1/+6
| | | | | | | This regression is caused by d1249c1. If STI model is defined `default_scope`, `base_rel` is not respected. I fixed to merge `base_rel` in that case.
* Define path with __dir__bogdanvlviv2017-05-231-1/+1
| | | | | | ".. with __dir__ we can restore order in the Universe." - by @fxn Related to 5b8738c2df003a96f0e490c43559747618d10f5f
* Should escape meta characters in regexpRyuta Kamizono2017-05-071-1/+1
|
* Cache results of computing model typeKonstantin Lazarev2017-01-031-7/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We faced a significant performance decrease when we started using STI without storing full namespaced class name in type column (because of PostgreSQL length limit for ENUM types). We realized that the cause of it is the slow STI model instantiation. Problematic method appears to be `ActiveRecord::Base.compute_type`, which is used to find the right class for STI model on every instantiation. It builds an array of candidate types and then iterates through it calling `safe_constantize` on every type until it finds appropriate constant. So if desired type isn't the first element in this array there will be at least one unsuccessful call to `safe_constantize`, which is very expensive, since it's defined in terms of `begin; rescue; end`. This commit is an attempt to speed up `compute_type` method simply by caching results of previous calls. ```ruby class MyCompany::MyApp::Business::Accounts::Base < ApplicationRecord self.table_name = 'accounts' self.store_full_sti_class = false end class MyCompany::MyApp::Business::Accounts::Free < Base end class MyCompany::MyApp::Business::Accounts::Standard < Base # patch .compute_type there end puts '======================= .compute_type =======================' Benchmark.ips do |x| x.report("original method") do MyCompany::MyApp::Business::Accounts::Free.send :compute_type, 'Free' end x.report("with types cached") do MyCompany::MyApp::Business::Accounts::Standard.send :compute_type, 'Standard' end x.compare! end ``` ``` ======================= .compute_type ======================= with types cached: 1529019.4 i/s original method: 2850.2 i/s - 536.46x slower ``` ```ruby 5_000.times do |i| MyCompany::MyApp::Business::Accounts::Standard.create!(name: "standard_#{i}") end 5_000.times do |i| MyCompany::MyApp::Business::Accounts::Free.create!(name: "free_#{i}") end puts '====================== .limit(100).to_a =======================' Benchmark.ips do |x| x.report("without .compute_type patch") do MyCompany::MyApp::Business::Accounts::Free.limit(100).to_a end x.report("with .compute_type patch") do MyCompany::MyApp::Business::Accounts::Standard.limit(100).to_a end x.compare! end ``` ``` ====================== .limit(100).to_a ======================= with .compute_type patch: 360.5 i/s without .compute_type patch: 24.7 i/s - 14.59x slower ```
* Add three new rubocop rulesRafael Mendonça França2016-08-161-3/+3
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* applies remaining conventions across the projectXavier Noria2016-08-061-1/+0
|
* modernizes hash syntax in activerecordXavier Noria2016-08-061-27/+27
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-61/+61
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Remove log-related stuff from ActiveSupport::DependenciesAaron Ang2016-03-151-5/+0
| | | | | In this patch, all log-related stuff in `ActiveSupport::Dependencies` is removed because the logging is no longer useful.
* Do not use default attributes for STI when instantiating a subclassSean Griffin2016-01-271-0/+4
| | | | | | | | | | The commit which originally added this behavior did not consider that doing `Subclass.new` does not actually populate the `type` field in the attributes (though perhaps it should). We simply need to not use the defaults for STI related things unless we are instantiating the base class. Fixes #23285.
* Fix test failure on PostgreSQL by sorting the result before comparisonPrathamesh Sonpatki2016-01-191-4/+5
|
* run `type` column through attribtues API type casting.Yves Senn2016-01-191-0/+75
| | | | | | | Closes #21986. This makes it possible to write custom types that define a different mapping for STI columns.
* don't rely on the columns hash to get defaults. follow-up to #17169.Yves Senn2015-12-021-0/+24
| | | | | | This will also get the defaults from attribute definitions like: attribute :type, :string, default: "SomethingElse"
* Merge pull request #17169 from kuldeepaggarwal/fix-STI-default-typeYves Senn2015-12-021-0/+21
|\ | | | | | | STI cast new instances to `default type` on initialize.
| * STI cast new instances to `default type` on initialize.Kuldeep Aggarwal2015-12-021-0/+20
|/ | | | fixes #17121
* Move some AR test cases to inheritance_test.rbyui-knk2015-10-311-1/+80
| | | | | | | | These methods are defined in inheritance.rb * `abstract_class?` * `descends_from_active_record?` * `compute_type`
* Revert "Merge pull request #21994 from mtodd/inherit-scopes"Rafael Mendonça França2015-10-271-2/+2
| | | | | | | This reverts commit 60c9701269f5b412849f1a507df61ba4735914d7, reversing changes made to 6a25202d9ea3b4a7c9f2d6154b97cf8ba58403db. Reason: Broken build
* Make inherited scope test failMatt Todd2015-10-261-2/+2
| | | | | | | | | | | | | | This triggers the JoinDependency work to reflect on the associations and trigger an error as follows: ActiveRecord::ConfigurationError: Association named 'account' was not found on Company; perhaps you misspelled it? Fix Company.of_first_firm joins association name Should be `Company.joins(:accounts)` not `Company.joins(:account)`. Do the same for Client.of_first_firm
* invalid sti error message contains the full class name.Yves Senn2015-05-131-0/+11
| | | | | | | This can resolve confusing situation when a top level constant exists but a namespaced version is identified. Related to #19531.
* Merge branch 'sti-subclass-from-attributes' of ↵Yves Senn2015-05-131-0/+7
|\ | | | | | | | | | | | | https://github.com/agrobbin/rails into agrobbin-sti-subclass-from-attributes Conflicts: activerecord/CHANGELOG.md
| * allow setting of a demodulized class name when using STIAlex Robbin2015-05-111-0/+8
|/ | | | | | | | | | | | | | | | | | | | | If your STI class looks like this: ```ruby class Company < ActiveRecord::Base self.store_full_sti_class = false class GoodCo < Company end class BadCo < Company end end ``` The expectation (which is valid) is that the `type` in the database is saved as `GoodCo` or `BadCo`. However, another expectation should be that setting `type` to `GoodCo` would correctly instantiate the object as a `Company::GoodCo`. That second expectation is what this should fix.
* tests, extract helpers to modify global state.Yves Senn2015-05-071-46/+55
| | | | | | | | | Make sure that tests do not hardcode the default value. For example `test_instantiation_doesnt_try_to_require_corresponding_file` always restored the configuration to `true` regardless of what it's original value was. Extract a helper to make the global modification consistent across tests.
* Always reset changed attributes in becomesMiklos Fazekas2015-02-041-0/+6
| | | | | | | | | | When ```becomes``` changes @attributes it should also change @changed_attributes. Otherwise we'll experience a kind of split head situation where attributes are coming from ```self```, but changed_attributes is coming from ```klass.new```. This affects the inheritance_colmn as it's changed by new for example. Fixes #16881
* Test association was eager loaded, rather than reaching into internalsSean Griffin2015-01-261-2/+2
|
* Go through normal `where` logic when preloading associationsSean Griffin2014-12-261-1/+1
| | | | | | | | | | This will allow eager type casting to take place as needed. There doesn't seem to be any particular reason that the `in` statement was forced for single values, and the commit message where it was introduced gives no context. See https://github.com/rails/rails/commit/d90b4e2615e8048fdeffc6dffe3246704adee01f
* Improve the performance of reading attributesSean Griffin2014-11-181-1/+1
| | | | | | | We added a comparison to "id", and call to `self.class.primary_key` a *lot*. We also have performance hits from `&block` all over the place. We skip the check in a new method, in order to avoid breaking the behavior of `read_attribute`
* Remove dead test code for unsupported adaptersSean Griffin2014-05-171-8/+0
|
* Use teardown helper method.Guo Xiang Tan2014-03-141-1/+1
| | | | | | | | Follow-Up to https://github.com/rails/rails/pull/14348 Ensure that SQLCounter.clear_log is called after each test. This is a step to prevent side effects when running tests. This will allow us to run them in random order.
* Ensure AR #second, #third, etc. finders work through associationsJason Meller2014-01-211-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit fixes two regressions introduced in cafe31a078 where newly created finder methods #second, #third, #forth, and #fifth caused a NoMethodError error on reload associations and where we were pulling the wrong element out of cached associations. Examples: some_book.authors.reload.second # Before # => NoMethodError: undefined method 'first' for nil:NilClass # After # => #<Author id: 2, name: "Sally Second", ...> some_book.first.authors.first some_book.first.authors.second # Before # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 1, name: "Freddy First", ...> # After # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 2, name: "Sally Second", ...> Fixes #13783.
* Don't try to get the subclass if the inheritance column doesn't existUjjwal Thaakar2014-01-141-1/+8
| | | | | | | The `subclass_from_attrs` method is called even if the column specified by the `inheritance_column` setting doesn't exist. This prevents setting associations via the attributes hash if the association name clashes with the value of the setting, typically `:type`. This worked previously in Rails 3.2.
* fix bug in becomes! when changing from base to subclass. Closes #13272.Yves Senn2014-01-131-0/+11
|
* Change all "can not"s to the correct "cannot".T.J. Schuck2014-01-031-2/+2
|
* Port test from cf1904f to avoid future regressionPrem Sichanugrist2013-10-031-1/+5
| | | | Related issue: #11939, #12084
* Fix #new with an STI object with complex inheritanceNate Berkopec2013-04-021-0/+4
|
* move tests out from base_test.rbTakehiro Adachi2013-03-181-0/+14
| | | | | These tests should be in inheritance_test.rb since its testing a feature which is implemented in inheritance.rb
* add an missing assertion to inheritance_test.rbTakehiro Adachi2013-03-111-0/+1
| | | | | | assertion for https://github.com/rails/rails/blob/ad624345e54bd20802de67b2b5c9ef29ecf5 d5f4/activerecord/lib/active_record/inheritance.rb#L32
* Fix ActiveRecord `subclass_from_attrs` when eager_load is false.Dmitry Vorotilin2013-03-061-0/+11
| | | | | It cannot find subclass because all classes are loaded automatically when it needs.
* User assert_kind_of, invert assert_equal expectationsCarlos Antonio da Silva2012-11-291-3/+3
|
* Added STI support to init and building associationsJason Rush2012-11-291-0/+23
| | | | | | | | Allows you to do BaseClass.new(:type => "SubClass") as well as parent.children.build(:type => "SubClass") or parent.build_child to initialize an STI subclass. Ensures that the class name is a valid class and that it is in the ancestors of the super class that the association is expecting.