aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
Commit message (Collapse)AuthorAgeFilesLines
* Give GeneratedAttributeMethods module a nameChris Salzberg2019-03-131-3/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently GeneratedAttributeMethods is a module builder class, an instance of which is included in every AR class. OTOH, GeneratedAssociatedMethods is assigned to a constant under the model namespace. This is inconsistent and looks strange in the list of ancestors. There is no particular reason *not* to assign a constant for this (very important) module under the model namespace, so that's what this commit does. Previous to this change, ancestors for an AR class looked like this: ``` => [User (call 'User.connection' to establish a connection), User::GeneratedAssociationMethods, #<ActiveRecord::AttributeMethods::GeneratedAttributeMethods:0x000055ace0f05b08>, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, #<ActiveRecord::AttributeMethods::GeneratedAttributeMethods:0x000055ace093c460>, ActiveRecord::Base, ... ``` With this change, they look like this: ``` => [User (call 'User.connection' to establish a connection), User::GeneratedAssociationMethods, User::GeneratedAttributeMethods, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, ApplicationRecord::GeneratedAttributeMethods, ActiveRecord::Base, ... ``` The previously named `GeneratedAttributeMethods` module builder class is renamed `GeneratedAttributeMethodsBuilder` to emphasize that this is not a module but a class.
* Fix inspect with non-primary key id attributeEugene Kenny2018-11-061-1/+1
| | | | | | | | | | | The `read_attribute` method always returns the primary key when asked to read the `id` attribute, even if the primary key isn't named `id`, and even if another attribute named `id` exists. For the `inspect`, `attribute_for_inspect` and `pretty_print` methods, this behaviour is undesirable, as they're used to examine the internal state of the record. By using `_read_attribute` instead, we'll get the real value of the `id` attribute.
* Merge pull request #34208 from yskkin/inspect_with_parameter_filterRyuta Kamizono2018-10-261-8/+11
|\ | | | | Implement AR#inspect using ParameterFilter
| * Implement AR#inspect using ParamterFilter.Yoshiyuki Kinjo2018-10-191-8/+11
| | | | | | | | | | | | | | | | | | AR instance support `filter_parameters` since #33756. Though Regex or Proc is valid as `filter_parameters`, they are not supported as AR#inspect. I also add :mask option and #filter_params to `ActiveSupport::ParameterFilter#new` to implement this.
* | ActiveRecord#respond_to? No longer allocates stringsschneems2018-10-151-11/+4
|/ | | | | | | | | | | | | | | | | This is an alternative to https://github.com/rails/rails/pull/34195 The active record `respond_to?` method needs to do two things if `super` does not say that the method exists. It has to see if the "name" being passed in represents a column in the table. If it does then it needs to pass it to `has_attribute?` to see if the key exists in the current object. The reason why this is slow is that `has_attribute?` needs a string and most (almost all) objects passed in are symbols. The only time we need to allocate a string in this method is if the column does exist in the database, and since these are a limited number of strings (since column names are a finite set) then we can pre-generate all of them and use the same string. We generate a list hash of column names and convert them to symbols, and store the value as the string name. This allows us to both check if the "name" exists as a column, but also provides us with a string object we can use for the `has_attribute?` call. I then ran the test suite and found there was only one case where we're intentionally passing in a string and changed it to a symbol. (However there are tests where we are using a string key, but they don't ship with rails). As re-written this method should never allocate unless the user passes in a string key, which is fairly uncommon with `respond_to?`. This also eliminates the need to special case every common item that might come through the method via the `case` that was originally added in https://github.com/rails/rails/commit/f80aa5994603e684e3fecd3f53bfbf242c73a107 (by me) and then with an attempt to extend in https://github.com/rails/rails/pull/34195. As a bonus this reduces 6,300 comparisons (in the CodeTriage app homepage) to 450 as we also no longer need to loop through the column array to check for an `include?`.
* Improve model attribute accessor method names for backtracesDylan Thacker-Smith2018-10-121-9/+0
| | | | | | | | Ruby uses the original method name, so will show the __temp__ method name in the backtrace. However, in the common case the method name is compatible with the `def` keyword, so we can avoid the __temp__ method name in that case to improve the name shown in backtraces or TracePoint#method_id.
* Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-291-2/+2
| | | | | | | | | | | | | | | | | | | | | Since Rails 6.0 will support Ruby 2.4.1 or higher `# frozen_string_literal: true` magic comment is enough to make string object frozen. This magic comment is enabled by `Style/FrozenStringLiteralComment` cop. * Exclude these files not to auto correct false positive `Regexp#freeze` - 'actionpack/lib/action_dispatch/journey/router/utils.rb' - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb' It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333 Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed. * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required - 'actionpack/test/controller/test_case_test.rb' - 'activemodel/test/cases/type/string_test.rb' - 'activesupport/lib/active_support/core_ext/string/strip.rb' - 'activesupport/test/core_ext/string_ext_test.rb' - 'railties/test/generators/actions_test.rb'
* Extract `Arel.arel_node?` helper methodRyuta Kamizono2018-09-281-3/+1
|
* Refactor `attributes_for_{create,update}` to avoid an extra allocationRyuta Kamizono2018-08-311-2/+4
| | | | Use `delete_if` instead of `reject` to avoid an extra allocation.
* Remove `attributes_with_values_for_{create,update}` for internal useRyuta Kamizono2018-08-301-8/+0
| | | | | `attributes_with_values_for_update` is no longer used since ae2d36c, and `attributes_with_values_for_create` is internally used only one place.
* Permit list usage cleanup and clearer documentationKevin Deisz2018-08-271-6/+8
|
* Convert remaining usage of whitelist and blacklistKevin Deisz2018-08-241-2/+2
|
* Convert over the rest of the whitelist referencesKevin Deisz2018-08-241-6/+6
|
* PERF: avoid allocating column names where possibleSam2018-06-061-1/+1
| | | | | | | | | | | When requesting columns names from database adapters AR:Result would dup/freeze column names, this prefers using fstrings which cuts down on repeat allocations Attributes that are retained keep these fstrings around for the long term Note, this has the highest impact on "short" result sets, eg: Topic.first where you can void allocating the number of columns * String.
* Add AR::Base.base_class? predicateBogdan Gusiev2018-04-021-1/+1
|
* update comment to reflect new supported patterns [ci skip]Xavier Noria2018-03-061-0/+4
|
* whitelist NULLS { FIRST | LAST } in order clausesXavier Noria2018-03-061-1/+8
|
* Refactor `_substitute_values` to be passed attribute names and valuesRyuta Kamizono2018-03-051-13/+7
|
* Use private attr_readerRyuta Kamizono2018-02-231-5/+2
| | | | | Since #32028, Rails 6 requires Ruby 2.3+. No longer needed workaround for Ruby 2.2 "private attribute?" warning.
* Remove internal `typecasted_attribute_value` methodRyuta Kamizono2018-01-251-5/+1
| | | | It is useless since 90c8be76a7d00475be5ff4db2eeedde5cc936c2d.
* try using regexesBen Toews2017-11-091-32/+16
|
* always allow Arel::Attributes::Attribute alsoBen Toews2017-11-091-1/+3
|
* remove :enabled optionBen Toews2017-11-091-2/+0
|
* beef up deprecation warningBen Toews2017-11-091-3/+7
|
* allow Arel.sql() for pluckBen Toews2017-11-091-1/+37
|
* add config to check arguments to unsafe AR methodsBen Toews2017-11-091-0/+16
|
* removed unnecessary returnsShuhei Kitagawa2017-10-281-1/+1
|
* Include Mutex_m into module class instead of extending instanceChris Salzberg2017-10-081-2/+4
|
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Remove unused requiresRyuta Kamizono2017-07-161-3/+0
|
* 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
|
* Remove returning true in internal callbacksRyuta Kamizono2017-05-141-1/+0
| | | | | `display_deprecation_warning_for_false_terminator` was removed since 3a25cdc.
* Privatize unneededly protected methods in Active RecordAkira Matsuda2017-01-051-8/+8
|
* This method is never called since 8e633e505880755e7e366ccec2210bbe2b5436e7Akira Matsuda2017-01-051-7/+0
|
* Fix broken comments indentation caused by rubocop auto-correct [ci skip]Ryuta Kamizono2016-09-141-5/+5
| | | | | | All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772. But comments was still kept absolute position. This commit aligns comments with method definitions for consistency.
* revises most Lint/EndAlignment offensesXavier Noria2016-08-071-3/+3
| | | | Some case expressions remain, need to think about those ones.
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-41/+41
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-5/+5
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* fix respond_to? documentationKivanio Barbosa2016-07-041-7/+7
|
* Do not specal case inspecting associated arrays of over 10 elements, ↵Kevin McPhillips2016-06-291-7/+3
| | | | preventing infinite looping in some cases.
* [] and read_attribute are not aliases [ci skip]Benjamin Quorning2016-05-241-2/+0
| | | | | | The `#[]` method *used to be* an alias of `#read_attribute`, but since Rails 4 (10f6f90d9d1bbc9598bffea90752fc6bd76904cd), it will raise an exception for missing attributes. Saying that it is an alias is confusing.
* Support for unified Integer class in Ruby 2.4+Jeremy Daer2016-05-181-1/+1
| | | | | | | | Ruby 2.4 unifies Fixnum and Bignum into Integer: https://bugs.ruby-lang.org/issues/12005 * Forward compat with new unified Integer class in Ruby 2.4+. * Backward compat with separate Fixnum/Bignum in Ruby 2.2 & 2.3. * Drops needless Fixnum distinction in docs, preferring Integer.
* Remove unused class AttributeMethodCachePareshGupta2016-02-021-24/+0
|
* add `ActiveRecord::Base.has_attribute?`Yves Senn2015-12-021-0/+12
| | | | `has_attribute?` method to check wether a given attribute has been defined.
* Require only necessary concurrent-ruby classes.Jerry D'Antonio2015-11-041-1/+1
|
* applies new doc guidelines to Active Record.Yves Senn2015-10-141-3/+3
| | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* Correcting `ActiveRecord::DangerousAttributeError` error message [ci skip]amitkumarsuroliya2015-09-261-1/+1
|
* Fix ActiveRecord `instance_method_already_implemented` docs [ci skip]amitkumarsuroliya2015-09-241-1/+1
|
* Replaced `ThreadSafe::Map` with successor `Concurrent::Map`.Jerry D'Antonio2015-09-191-2/+2
| | | | | | | The thread_safe gem is being deprecated and all its code has been merged into the concurrent-ruby gem. The new class, Concurrent::Map, is exactly the same as its predecessor except for fixes to two bugs discovered during the merge.