aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
Commit message (Collapse)AuthorAgeFilesLines
* Allow quoted identifier string as safe SQL stringRyuta Kamizono2019-06-061-53/+0
| | | | | | | | | | | | | Currently `posts.title` is regarded as a safe SQL string, but `"posts"."title"` (it is a result of `quote_table_name("posts.title")`) is regarded as an unsafe SQL string even though a result of `quote_table_name` should obviously be regarded as a safe SQL string, since the column name matcher doesn't respect quotation, it is a little annoying. This changes the column name matcher to allow quoted identifiers as safe SQL string, now all results of the `quote_table_name` are regarded as safe SQL string.
* Allow symbol (i.e. quoted identifier) as safe SQL stringRyuta Kamizono2019-05-281-3/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `pluck(:id)` / `order(:id)` are very common use case, and passed symbol (i.e. quoted identifier) is obviously safe argument, but `:id.to_s.split(/\s*,\s*/).all? { |part| permit.match?(part) }` is useless and a bit expensive operation for each such safe symbols (will make extra 2 mutable strings, 1 array, 1 proc). This avoids the expensive operation to such safe symbols, it makes `pluck(:id)` / `order(:id)` itself about 9% faster. https://gist.github.com/kamipo/11d428b57f3629a72ae89c6f21721326 Before (93e640735e9363672b770b8d1c5a35f9e464f806): ``` Warming up -------------------------------------- users.pluck(:id) 1.217k i/100ms users.order(:id).to_sql 1.848k i/100ms Calculating ------------------------------------- users.pluck(:id) 12.239k (± 8.2%) i/s - 60.850k in 5.013839s users.order(:id).to_sql 19.111k (± 7.5%) i/s - 96.096k in 5.064450s ``` After (this change): ``` Warming up -------------------------------------- users.pluck(:id) 1.293k i/100ms users.order(:id).to_sql 2.036k i/100ms Calculating ------------------------------------- users.pluck(:id) 13.257k (± 6.9%) i/s - 65.943k in 5.002568s users.order(:id).to_sql 20.957k (± 7.6%) i/s - 105.872k in 5.086102s ```
* Extract `readonly_attribute?`Ryuta Kamizono2019-05-271-5/+1
|
* PERF: 20% faster pk attribute accessRyuta Kamizono2019-04-221-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I've realized that `user.id` is 20% slower than `user.name` in the benchmark (https://github.com/rails/rails/pull/35987#issuecomment-483882480). The reason that performance difference is that `self.class.primary_key` method call is a bit slow. Avoiding that method call will make almost attribute access faster and `user.id` will be completely the same performance with `user.name`. Before (02b5b8cb): ``` Warming up -------------------------------------- user.id 140.535k i/100ms user['id'] 96.549k i/100ms user.name 158.110k i/100ms user['name'] 94.507k i/100ms user.changed? 19.003k i/100ms user.saved_changes? 25.404k i/100ms Calculating ------------------------------------- user.id 2.231M (± 0.9%) i/s - 11.243M in 5.040066s user['id'] 1.310M (± 1.3%) i/s - 6.565M in 5.012607s user.name 2.683M (± 1.2%) i/s - 13.439M in 5.009392s user['name'] 1.322M (± 0.9%) i/s - 6.615M in 5.003239s user.changed? 201.999k (±10.9%) i/s - 1.007M in 5.091195s user.saved_changes? 258.214k (±17.1%) i/s - 1.245M in 5.007421s ``` After (this change): ``` Warming up -------------------------------------- user.id 158.364k i/100ms user['id'] 106.412k i/100ms user.name 158.644k i/100ms user['name'] 107.518k i/100ms user.changed? 19.082k i/100ms user.saved_changes? 24.886k i/100ms Calculating ------------------------------------- user.id 2.768M (± 1.1%) i/s - 13.936M in 5.034957s user['id'] 1.507M (± 2.1%) i/s - 7.555M in 5.017211s user.name 2.727M (± 1.5%) i/s - 13.643M in 5.004766s user['name'] 1.521M (± 1.3%) i/s - 7.634M in 5.018321s user.changed? 200.865k (±11.1%) i/s - 992.264k in 5.044868s user.saved_changes? 269.652k (±10.5%) i/s - 1.344M in 5.077972s ```
* Change deprecation message for dangerous query method to be disallowed in ↵Abhay Nikam2019-04-201-1/+1
| | | | Rails 6.1
* Revert renamed `GeneratedAttributeMethods`Ryuta Kamizono2019-04-191-3/+3
| | | | | | | This partly reverts 8ca3c286a768038f6b391fd3bfbdfcc299876a1f. `.is_a?(GeneratedAttributeMethodsBuilder)` doesn't make sense to me, the module class's `new` creates just an instance of the module class itself.
* 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
|