aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations
Commit message (Collapse)AuthorAgeFilesLines
* validate_each in NumericalityValidator is never called in this case.Guillermo Iguaran2016-08-281-2/+0
| | | | | NumericalityValidator#validate_each is never called when allow_nil is true and the value is nil because it is already skipped in EachValidator#validate.
* revises most Lint/EndAlignment offensesXavier Noria2016-08-071-5/+5
| | | | Some case expressions remain, need to think about those ones.
* applies remaining conventions across the projectXavier Noria2016-08-067-7/+0
|
* normalizes indentation and whitespace across the projectXavier Noria2016-08-066-91/+91
|
* applies new string literal convention in activemodel/libXavier Noria2016-08-065-6/+6
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* systematic revision of =~ usage in AMoXavier Noria2016-07-241-2/+3
|
* 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.
* - removing redundant 'happens' in documentation [ci skip]Mohit Natoo2016-05-191-4/+2
|
* Chomp: prefer String#chomp where we can for a clarity boostlvl0nax2016-04-291-5/+1
| | | | | | Closes #24766, #24767 Signed-off-by: Jeremy Daer <jeremydaer@gmail.com>
* fix length validation error typoPareshGupta2016-04-071-1/+1
|
* Use Range#cover? for Date inclusion validatorojab2016-03-241-3/+4
|
* Convert non-`Numeric` values to FloatsRobert Eshleman2015-12-221-1/+1
|
* Fix Regression in Numericality ValidationsRobert Eshleman2015-12-221-2/+9
| | | | | | | | | | | | A regression (#22744) introduced in 7500dae caused certain numericality validations to raise an error when run against an attribute with a string value. Previously, these validations would successfully run against string values because the value was cast to a numeric class. This commit resolves the regression by converting string values to floats before performing numericality comparison validations. [fixes #22744]
* Merge pull request #19851 from repinel/numericality-validation2Sean Griffin2015-10-201-12/+11
|\ | | | | Use the post-type-cast version of the attribute to validate numericality
| * Conditionally convert the raw_value received by the numeric validator.Roque Pinel2015-07-111-12/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes the issue where you may be comparing (using a numeric validator such as `greater_than`) numbers of a specific Numeric type such as `BigDecimal`. Previous behavior took the numeric value to be validated and unconditionally converted to Float. For example, due to floating point precision, this can cause issues when comparing a Float to a BigDecimal. Consider the following: ``` validates :sub_total, numericality: { greater_than: BigDecimal('97.18') } ``` If the `:sub_total` value BigDecimal.new('97.18') was validated against the above, the following would be valid since `:sub_total` is converted to a Float regardless of its original type. The result therefore becomes Kernel.Float(97.18) > BigDecimal.new('97.18') The above illustrated behavior is corrected with this patch by conditionally converting the value to validate to float. Use the post-type-cast version of the attribute to validate numericality [Roque Pinel & Trevor Wistaff]
* | `validates_acceptance_of` shouldn't require a database connectionSean Griffin2015-09-251-4/+51
| | | | | | | | | | | | | | | | | | | | | | | | | | The implementation of `attribute_method?` on Active Record requires establishing a database connection and querying the schema. As a general rule, we don't want to require database connections for any class macro, as the class should be able to be loaded without a database (e.g. for things like compiling assets). Instead of eagerly defining these methods, we do it lazily the first time they are accessed via `method_missing`. This should not cause any performance hits, as it will only hit `method_missing` once for the entire class.
* | Fix the AS::Callbacks terminator regression from 4.2.3Roque Pinel2015-09-221-0/+1
| | | | | | | | | | | | Rails 4.2.3 AS::Callbacks will not halt chain if `false` is returned. That is the behavior of specific callbacks like AR::Callbacks and AM::Callbacks.
* | AMo typosAkira Matsuda2015-09-221-2/+2
| |
* | Fix failure introduced by #17351 due to the new mocks implementationCarlos Antonio da Silva2015-09-011-1/+1
| | | | | | | | | | | | | | It was not expecting the new `case_insensitive` option to be passed to `generate_message`, instead of fixing the test we can just not pass this option down since it is specific to the confirmation validator and not necessary for the error message.
* | Fix syntax error introduced by #17351.Jashank Jeremy2015-09-011-1/+1
| |
* | Merge pull request #17351 from akshat-sharma/masterRafael Mendonça França2015-09-011-4/+16
|\ \ | | | | | | | | | Add case_sensitive option for confirmation validation
| * | Add case_sensitive option for confirmation validationAkshat Sharma2015-09-011-4/+16
| | | | | | | | | | | | | | | | | | | | | Case :- 1. In case of email confirmation one needs case insensitive comparison 2. In case of password confirmation one needs case sensitive comparison [ci skip] Update Guides for case_sensitive option in confirmation validation
* | | Freeze string literals when not mutated.schneems2015-07-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I wrote a utility that helps find areas where you could optimize your program using a frozen string instead of a string literal, it's called [let_it_go](https://github.com/schneems/let_it_go). After going through the output and adding `.freeze` I was able to eliminate the creation of 1,114 string objects on EVERY request to [codetriage](codetriage.com). How does this impact execution? To look at memory: ```ruby require 'get_process_mem' mem = GetProcessMem.new GC.start GC.disable 1_114.times { " " } before = mem.mb after = mem.mb GC.enable puts "Diff: #{after - before} mb" ``` Creating 1,114 string objects results in `Diff: 0.03125 mb` of RAM allocated on every request. Or 1mb every 32 requests. To look at raw speed: ```ruby require 'benchmark/ips' number_of_objects_reduced = 1_114 Benchmark.ips do |x| x.report("freeze") { number_of_objects_reduced.times { " ".freeze } } x.report("no-freeze") { number_of_objects_reduced.times { " " } } end ``` We get the results ``` Calculating ------------------------------------- freeze 1.428k i/100ms no-freeze 609.000 i/100ms ------------------------------------------------- freeze 14.363k (± 8.5%) i/s - 71.400k no-freeze 6.084k (± 8.1%) i/s - 30.450k ``` Now we can do some maths: ```ruby ips = 6_226k # iterations / 1 second call_time_before = 1.0 / ips # seconds per iteration ips = 15_254 # iterations / 1 second call_time_after = 1.0 / ips # seconds per iteration diff = call_time_before - call_time_after number_of_objects_reduced * diff * 100 # => 0.4530373333993266 miliseconds saved per request ``` So we're shaving off 1 second of execution time for every 220 requests. Is this going to be an insane speed boost to any Rails app: nope. Should we merge it: yep. p.s. If you know of a method call that doesn't modify a string input such as [String#gsub](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37) please [give me a pull request to the appropriate file](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37), or open an issue in LetItGo so we can track and freeze more strings. Keep those strings Frozen ![](https://www.dropbox.com/s/z4dj9fdsv213r4v/let-it-go.gif?dl=1)
* | | Revert "Revert "Reduce allocations when running AR callbacks.""Guo Xiang Tan2015-07-161-1/+1
| |/ |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3. Before: Calculating ------------------------------------- 22.000 i/100ms ------------------------------------------------- 229.700 (± 0.4%) i/s - 1.166k Total Allocated Object: 9939 After: Calculating ------------------------------------- 24.000 i/100ms ------------------------------------------------- 246.443 (± 0.8%) i/s - 1.248k Total Allocated Object: 7939 ``` begin require 'bundler/inline' rescue LoadError => e $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' raise e end gemfile(true) do source 'https://rubygems.org' # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3' gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7' gem 'arel', github: 'rails/arel' gem 'sqlite3' gem 'benchmark-ips' end require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection('sqlite3::memory:') ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.boolean :admin t.timestamps null: false end end class User < ActiveRecord::Base default_scope { where(admin: true) } end admin = true 1000.times do attributes = { name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", email: "foobar@email.com", admin: admin } User.create!(attributes) admin = !admin end GC.disable Benchmark.ips(5, 3) do |x| x.report { User.all.to_a } end key = if RUBY_VERSION < '2.2' :total_allocated_object else :total_allocated_objects end before = GC.stat[key] User.all.to_a after = GC.stat[key] puts "Total Allocated Object: #{after - before}" ```
* | Separate the constraint and other options [ci skip]Robin Dupret2015-07-011-3/+8
| | | | | | | | | | | | | | | | | | | | Only one constraint option can be used at a time (except for the minimum and maximum ones that can eventually be combined). However, other options can be used with them (e.g. the validation failure message). So let's make the distinction between these two different options categories. [Yves Senn, Matthew Draper & Robin Dupret]
* | Improve Validation Helpers' documentation comments and testsRadan Skoric2015-06-273-7/+10
| |
* | A few documentation fixes [ci skip]Robin Dupret2015-06-231-1/+1
| |
* | Add nodoc to the Validations::Helpers [ci skip]Mehmet Emin İNAÇ2015-06-221-1/+1
| |
* | docs, :scissors: wrongly placed heading. [ci skip]Yves Senn2015-06-221-2/+0
| | | | | | | | | | The heading "Active Model Length Validator" was shown on the "ActiveModel::Validations" page without any text following it.
* | Move the validations HelperMethods to its own fileRoque Pinel2015-06-212-10/+13
| | | | | | | | | | | | Closes #11209 [Roque Pinel & Steven Yang]
* | Merge pull request #19448 from tgxworld/fix_activesupport_callbacks_clash_on_runRafael Mendonça França2015-04-061-1/+1
|\ \ | | | | | | Fix AS::Callbacks raising an error when `:run` callback is defined.
| * | Revert "Reduce allocations when running AR callbacks."Guo Xiang Tan2015-03-221-1/+1
| | | | | | | | | | | | This reverts commit 796cab45561fce268aa74e6587cdb9cae3bb243e.
* | | fix typo in deprecation message. [Robin Dupret]Yves Senn2015-04-051-1/+1
| | |
* | | Deprecate the `:tokenizer` option to `validates_length_of`Sean Griffin2015-03-291-8/+30
|/ / | | | | | | | | | | | | | | As demonstrated by #19570, this option is severely limited, and satisfies an extremely specific use case. Realistically, there's not much reason for this option to exist. Its functionality can be trivially replicated with a normal Ruby method. Let's deprecate this option, in favor of the simpler solution.
* | Merge pull request #17144 from skojin/patch-doc-validation-format-z-regexpRafael Mendonça França2015-02-201-1/+1
|\ \ | | | | | | fix mistype in doc about \z regexp
| * | fix mistype in doc about \z regexpSergey Kojin2014-10-021-1/+1
| | | | | | | | | | | | replace \Z with regular \z
* | | Tiny documentation edits [ci skip]Robin Dupret2015-02-151-4/+6
| | |
* | | Merge pull request #16381 from kakipo/validate-length-tokenizerRafael Mendonça França2015-02-131-7/+11
|\ \ \ | | | | | | | | | | | | Allow symbol as values for `tokenizer` of `LengthValidator`
| * | | Allow symbol as values for `tokenize` of `LengthValidator`kakipo2014-08-031-7/+11
| | | |
* | | | allow '1' or true for acceptance validation.mo khan2015-01-101-2/+6
| | | |
* | | | Merge pull request #17227 from claudiob/explicitly-abort-callbacksRafael Mendonça França2015-01-031-3/+2
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Introduce explicit way of halting callback chains by throwing :abort. Deprecate current implicit behavior of halting callback chains by returning `false` in apps ported to Rails 5.0. Completely remove that behavior in brand new Rails 5.0 apps. Conflicts: railties/CHANGELOG.md
| * | | | Deprecate `false` as the way to halt AM validation callbacksclaudiob2015-01-021-3/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before this commit, returning `false` in an ActiveModel validation callback such as `before_validation` would halt the callback chain. After this commit, the behavior is deprecated: will still work until the next release of Rails but will also display a deprecation warning. The preferred way to halt a callback chain is to explicitly `throw(:abort)`.
| * | | | Throw :abort halts default CallbackChainsclaudiob2015-01-021-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit changes arguments and default value of CallbackChain's :terminator option. After this commit, Chains of callbacks defined **without** an explicit `:terminator` option will be halted as soon as a `before_` callback throws `:abort`. Chains of callbacks defined **with** a `:terminator` option will maintain their existing behavior of halting as soon as a `before_` callback matches the terminator's expectation. For instance, ActiveModel's callbacks will still halt the chain when a `before_` callback returns `false`.
* | | | | Use Active Model, not ActiveModel in plain Englishclaudiob2015-01-021-1/+1
|/ / / / | | | | | | | | | | | | | | | | | | | | | | | | Also prevents the word "Model" from linking to the documentation of ActiveModel::Model because that's not intended. [ci skip]
* | | | Ensure numericality validations work with mutationSean Griffin2014-12-011-0/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The detection of in-place changes caused a weird unexpected issue with numericality validations. That validator (out of necessity) works on the `_before_type_cast` version of the attribute, since on an `:integer` type column, a non-numeric string would type cast to 0. However, strings are mutable, and we changed strings to ensure that the post type cast version of the attribute was a different instance than the before type cast version (so the mutation detection can work properly). Even though strings are the only mutable type for which a numericality validation makes sense, special casing strings would feel like a strange change to make here. Instead, we can make the assumption that for all mutable types, we should work on the post-type-cast version of the attribute, since all cases which would return 0 for non-numeric strings are immutable. Fixes #17852
* | | | add missing space.[ci skip]Kuldeep Aggarwal2014-11-011-1/+1
| | | |
* | | | Prefix internal method with _Rafael Mendonça França2014-10-251-1/+1
| |_|/ |/| | | | | | | | This will avoid naming clash with user defined methods
* | | Merge pull request #16409 from ↵Zachary Scott2014-10-032-5/+10
|\ \ \ | | | | | | | | | | | | | | | | justinweiss/update_validation_context_documentation Docs: Add a note on custom validation contexts. [ci skip]
| * | | Add a note on custom validation contexts.Justin Weiss2014-08-052-5/+10
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | The documentation on `:on` for validations was inconsistent, and most only referenced the `:create` and `:update` contexts. I fixed those to be consistent with the documentation on `AM::Validations.validates`, which seemed to have the best docs. [ci skip]
* | / Disallow appended newlines when parsing as integerMichael Genereux2014-10-021-1/+1
| |/ |/| | | \Z allows appended newlines where \z does not.