aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations
Commit message (Collapse)AuthorAgeFilesLines
* 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.
* | Refactor callback setup in to use lambda instead of evalPablo Herrero2014-09-301-2/+4
| |
* | Reduce allocations when running AR callbacks.Pete Higgins2014-09-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Inspired by @tenderlove's work in c363fff29f060e6a2effe1e4bb2c4dd4cd805d6e, this reduces the number of strings allocated when running callbacks for ActiveRecord instances. I measured that using this script: ``` require 'objspace' require 'active_record' require 'allocation_tracer' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 result = ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end result.sort.each do |k,v| p k => v end puts "total: #{result.values.map(&:first).inject(:+)}" ``` When I run this against master and this branch I get this output: ``` pete@balloon:~/projects/rails/activerecord$ git checkout master M Gemfile Switched to branch 'master' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks M Gemfile Switched to branch 'remove-dynamic-send-on-built-in-callbacks' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after 39d38 < {["/home/pete/projects/rails/activesupport/lib/active_support/callbacks.rb", 81]=>[40, 0, 0, 0, 0, 0]} 42c41 < total: 630 --- > total: 590 ``` In addition to this, there are two micro-optimizations present: * Using `block.call if block` vs `yield if block_given?` when the block was being captured already. ``` pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb require 'benchmark/ips' def block_capture_with_yield &block yield if block_given? end def block_capture_with_call &block block.call if block end def no_block_capture yield if block_given? end Benchmark.ips do |b| b.report("block_capture_with_yield") { block_capture_with_yield } b.report("block_capture_with_call") { block_capture_with_call } b.report("no_block_capture") { no_block_capture } end pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb Calculating ------------------------------------- block_capture_with_yield 124979 i/100ms block_capture_with_call 138340 i/100ms no_block_capture 136827 i/100ms ------------------------------------------------- block_capture_with_yield 5703108.9 (±2.4%) i/s - 28495212 in 4.999368s block_capture_with_call 6840730.5 (±3.6%) i/s - 34169980 in 5.002649s no_block_capture 5821141.4 (±2.8%) i/s - 29144151 in 5.010580s ``` * Defining and calling methods instead of using send. ``` pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb require 'benchmark/ips' class Foo def tacos nil end end my_foo = Foo.new Benchmark.ips do |b| b.report('send') { my_foo.send('tacos') } b.report('call') { my_foo.tacos } end pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb Calculating ------------------------------------- send 97736 i/100ms call 151142 i/100ms ------------------------------------------------- send 2683730.3 (±2.8%) i/s - 13487568 in 5.029763s call 8005963.9 (±2.7%) i/s - 40052630 in 5.006604s ``` The result of this is making typical ActiveRecord operations slightly faster: https://gist.github.com/phiggins/e46e51dcc7edb45b5f98
* | Update documentation to match change in #5942 [ci skip]edogawaconan2014-08-241-1/+1
|/
* `only_integer` of `NumericalityValidator` now allows procs and symbolsRobin Mehner2014-06-221-1/+13
|
* add missing parentheses to validates_with documentation [skip ci]Steve Agalloco2014-04-041-1/+1
|
* Fix some validators when used on model instanceEric Hutzelman2014-02-261-0/+2
| | | | | | | | Now that Validator #setup is called from the initializer, we need a reference to the model's class to be passed in to allow the validators to continue functioning when used at the instance level. Closes #14134.
* revises references to :allow_(nil|blank) in some docs [ci skip] [Steven Yang ↵Xavier Noria2014-01-269-24/+12
| | | | | | & Xavier Noria] Closes #11247.
* doc proc/lambda arg on inclusion validation. Closes #13689. [ci skip]Yves Senn2014-01-131-1/+2
|
* Adding missing backslashes in active_model files so as to avoid unwanted ↵aditya-kapoor2013-12-272-2/+2
| | | | links in rdoc [ci skip]
* fix email regex example code [ci skip]Angelo capilleri2013-12-031-1/+1
| | | | different from the regex in EmailValidator
* Remove short circuit return in favor of simple conditionalCarlos Antonio da Silva2013-11-151-4/+7
|
* Invert conditional to avoid double checking for RegexpCarlos Antonio da Silva2013-11-151-11/+12
|
* Only check that the option exists once instead of doing on each conditionalCarlos Antonio da Silva2013-11-151-8/+9
|
* Remove argument that is accessible as attributeCarlos Antonio da Silva2013-11-151-3/+3
|
* Cache regexp source on format validation to avoid allocating new objectsCarlos Antonio da Silva2013-11-151-2/+2
| | | | | | | | | Example: >> r = /some-regexp/ => /some-regexp/ >> r.source.object_id == r.source.object_id => false
* Make code simpler to read by using a case statementCarlos Antonio da Silva2013-11-151-2/+6
|
* Avoid a new hash allocationCarlos Antonio da Silva2013-11-151-1/+1
|
* Use a simple conditional rather than short circuit with nextCarlos Antonio da Silva2013-11-151-2/+3
|
* Simplify number parsing logic in numericality validationCarlos Antonio da Silva2013-11-151-10/+3
|
* Avoid creation of extra hash with merge just to set a valueCarlos Antonio da Silva2013-11-151-1/+3
|
* Let validates_inclusion_of accept Time and DateTime rangesAkira Matsuda2013-10-231-5/+11
| | | | fixes 4.0.0 regression introduced in 0317b93c17a46d7663a8c36edc26ad0ba3d75f85
* remove evals from AM::Validations::CallbacksSteven Yang2013-06-301-1/+3
| | | | follow the same refactor at a63a964a5d1ed02cf0df1b1a33a96ed2a9fa987b
* provide a more sementicthe local variables name for ↵Steven Yang2013-06-291-8/+8
| | | | | | | ActiveModel::Validations::Clusivity#include? method the original name `exclusion` is a bit confusing when using with the method `inclusion_method` rename it to a more logic neutral name.
* Use Range#cover? for Numeric ranges (tests via endpoints) and use ↵Charles Bergeron2013-05-272-4/+5
| | | | | | Range#include? for non-numeric ranges added changelog message
* deprecate Validator#setup (to get rid of a respond_to call). validators do ↵Nick Sutterer2013-05-233-3/+12
| | | | their setup in their constructor now.
* deprecating string based terminatorsAaron Patterson2013-05-141-1/+4
|
* Convert ActiveModel to 1.9 hash syntax.Patrick Robertson2013-05-019-15/+15
| | | | | I also attempted to fix other styleguide violations such as { a: :b } over {a: :b} and foo(b: 'bar') over foo( b: 'bar' ).
* `validates_confirmation_of` does not override writer methods.Yves Senn2013-03-041-1/+5
|
* Reduce number of Strings a bitAkira Matsuda2013-01-072-3/+3
|
* Revert the change at ActiveModel::Errors#add_on_blank and fix in theRafael Mendonça França2012-12-261-2/+2
| | | | | | | | | | | right place. The EachValidator#validate already handle :allow_blank and :allow_nil, correctly. Closes #8622. Fix #8621.
* Use :present as key for the absence validatior messageRafael Mendonça França2012-12-211-1/+1
|
* Remove ActiveModel::Errors#add_on_present method.Rafael Mendonça França2012-12-211-2/+2
| | | | | | | | We don't need to define a new method in ActiveMode::Errors for each validatior. See https://github.com/rails/rails/commit/d72a07f1d1478db9daed847eadb35bfd840674f6#commitcomment-2325333
* Add `ActiveModel::Validations::AbsenceValidator`, a validator to check the ↵Roberto Vasquez Angel2012-12-151-0/+31
| | | | | | absence of attributes. Add `ActiveModel::Errors#add_on_present` method. Adds error messages to present attributes.
* Merge pull request #7282 from xHire/validates_length_of_fixRafael Mendonça França2012-11-261-2/+14
|\ | | | | | | | | | | | | Length validation handles correctly nil. Fix #7180 Conflicts: activemodel/CHANGELOG.md
| * Length validation handles correctly nil. Fix #7180Michal Zima2012-11-261-2/+14
| | | | | | | | When nil or empty string are not allowed, they are not valid.
* | cleanup, removed dispensable `require` statements from `ActiveModel`Yves Senn2012-11-251-2/+0
| |
* | cleanup, remove broken whitespaceYves Senn2012-11-251-2/+2
|/
* Set hash value instead of using merge!Carlos Antonio da Silva2012-11-041-1/+1
|
* minor edits and remove mixed titles in AM::Validations docs [ci skip]Francesco Rodriguez2012-10-259-21/+13
|