Commit Graph

1534 Commits

Author SHA1 Message Date
Daniele Di Bernardo
eb05774531 [Enh] Changed the visibility of the ActiveModel::Dirty#clear_attribute_changes method
In Rails 4.2 it is impossible to define a custom default value for a model's
attribute without making it appear as _changed?, especially when the model
is first initialized. Making this method publicly visible will allow such a behaviour,
without the need to use private APIs.
2014-11-06 19:56:20 +01:00
Adam89
52720b46ea Remove redundant require of file
This file was required inside 'test/validators/namespace/email_validator.rb'
that's already required here. Therefore I removed the redundant required.
2014-11-01 19:00:12 +00:00
Kuldeep Aggarwal
c551178f1b add missing space.[ci skip] 2014-11-01 22:18:05 +05:30
Godfrey Chan
4daebedcc4 Prepare for 4.2.0.beta4 release 2014-10-30 14:12:24 -07:00
Xavier Noria
e595d91ac2 edit pass over all warnings
This patch uniformizes warning messages. I used the most common style
already present in the code base:

* Capitalize the first word.

* End the message with a full stop.

* "Rails 5" instead of "Rails 5.0".

* Backticks for method names and inline code.

Also, converted a few long strings into the new heredoc convention.
2014-10-28 17:47:32 -07:00
Rafael Mendonça França
89397d09eb Prefix internal method with _
This will avoid naming clash with user defined methods
2014-10-25 17:12:19 -07:00
Garry Shutler
6c8cf21584 Add #key? to ActiveModel::Errors
Mirror Ruby's Hash#key?
2014-10-14 09:53:26 +01:00
Zachary Scott
44e6d91a07 Merge pull request #16409 from justinweiss/update_validation_context_documentation
Docs: Add a note on custom validation contexts. [ci skip]
2014-10-03 17:29:25 -07:00
Ryan Selk
9ded108e84 fix typo in in define_model_callbacks comment [ci skip] 2014-10-03 15:31:14 -06:00
Kuldeep Aggarwal
f50430ee3e add notes for define_model_callbacks [ci skip] 2014-10-03 23:04:01 +05:30
Michael Genereux
4e511001b4 Disallow appended newlines when parsing as integer
\Z allows appended newlines where \z does not.
2014-10-02 13:55:22 -07:00
Rafael Mendonça França
7b740f31cc Merge pull request #17088 from robin850/jruby-dev
Follow up to #16613
2014-10-01 22:56:08 -03:00
Pablo Herrero
291f64469f Refactor callback setup in to use lambda instead of eval 2014-09-30 09:25:04 -03:00
Pete Higgins
796cab4556 Reduce allocations when running AR callbacks.
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
2014-09-28 15:38:32 -07:00
Robin Dupret
1fac7b79f3 Follow up to #16613
Since we want this flag to be enabled anytime we are running the tests
under JRuby, let's enable this at the Rakefile level so people get the
performance boost on their local checkout.

Moreover, we avoid having to update this particular line anytime the
option changes on the JRuby side.

The only drawback is that we have to define it in every Rakefile but
there's no big deal, this is already the case for other options.
2014-09-28 12:04:06 +02:00
Rafael Mendonça França
4581d04477 Preparing for 4.2.0.beta2 release 2014-09-26 17:19:53 -03:00
Prathamesh Sonpatki
a7ed62987c Added test for exception message for validate method
- Test case for https://github.com/rails/rails/pull/16851
2014-09-23 14:36:15 +09:00
Godfrey Chan
ca67632674 Move the array to a constant 2014-09-23 01:59:43 +09:00
Prathamesh Sonpatki
a91b36f6eb Update error message for validate method
- Improve the error message by suggesting that the user may have
  intended to call validates instead of validate method.
2014-09-20 22:48:52 +09:00
Akshay Vishnoi
57a893b54a [ci skip] ActiveModel CHANGELOG docs fixes 2014-09-18 02:57:13 +05:30
Kuldeep Aggarwal
869a90512f use allow_blank option instead 2014-09-14 00:19:33 +05:30
Godfrey Chan
2b41343c34 Default to sorting user's test cases for now
Goals:

1. Default to :random for newly generated applications
2. Default to :sorted for existing applications with a warning
3. Only show the warning once
4. Only show the warning if the app actually uses AS::TestCase

Fixes #16769
2014-09-08 05:32:16 -07:00
Matthew Draper
7025d7769d For now, we will keep sorting the tests.
This reverts commits e969c928463e329fd6529ac59cad96385c538ffb and
bd2b3fbe54e750ba97469a7896e8d143d6dfd465.
2014-09-05 23:33:27 +09:30
Rafael Mendonça França
703a2e8da1 Remove example file
This documentation should be in the guides.

Closes #16691
2014-09-04 14:06:40 -03:00
Matthew Draper
2f52f96988 Leave all our tests as order_dependent! for now
We're seeing too many failures to believe otherwise.

This reverts commits bc116a55ca3dd9f63a1f1ca7ade3623885adcc57,
cbde413df3839e06dd14e3c220e9800af91e83ab,
bf0a67931dd8e58f6f878b9510ae818ae1f29a3a, and
2440933fe2c27b27bcafcd9019717800db2641aa.
2014-09-02 23:55:34 +09:30
Robin Dupret
84c0f73c8d Refer to the library name instead of the constant
When we are loading a component and we want to know its version, we are
actually not speaking about the constant but the library itself.

[ci skip]

[Godfrey Chan & Xavier Noria]
2014-08-30 11:58:23 +02:00
David Heinemeier Hansson
7475b43cdb Merge branch 'master' of github.com:rails/rails 2014-08-29 14:54:08 -07:00
Akira Matsuda
b23365fe5c Move model definition to test/models for test order indenendency 2014-08-28 16:56:53 +09:00
Akira Matsuda
bd2b3fbe54 No need to sort tests
Dir.glob result must be already sorted anyway
2014-08-28 14:41:00 +09:00
Yves Senn
bf44481524 Merge pull request #16661 from edogawaconan/doc-fix
Update documentation to match change in #5942 [ci skip]
2014-08-27 11:34:47 +02:00
Eileen M. Uchitelle
9fb16627a2 Merge pull request #16689 from ankit1910/improve-english
[ci skip] make assert messages consistent
2014-08-25 19:37:10 -04:00
Yukio Mizuta
097ca3f1f8 Use ActiveSupport::Concern instead of the traditinal way 2014-08-25 11:06:06 -07:00
ankit1910
5284f98d79 [ci skip] make assert messages consistent 2014-08-25 22:07:27 +05:30
edogawaconan
e57ad6dc03 Update documentation to match change in #5942 [ci skip] 2014-08-24 00:04:07 +09:00
Attila Domokos
1079bb6d7d Using each_with_object instead of reduce
This way no new object allocation is taking place. Thanks @jeremy for
the suggestion!
2014-08-22 08:08:12 -05:00
Attila Domokos
36895bd90f Replacing an each with reduce
The functionality has not changed, but the code is more elegant by
using `reduce` instead of `each`.

This way no accumulator needs to be declared, no explicit return is
needed.
2014-08-21 23:13:37 -05:00
David Heinemeier Hansson
6a23bf0f4c Preparing for 4.2.0.beta1 release 2014-08-19 19:32:51 -07:00
Rafael Mendonça França
306dc1a499 Check attributes passed to create_with and where
If the request parameters are passed to create_with and where they can
be used to do mass assignment when used in combination with
Relation#create.

Fixes CVE-2014-3514

Conflicts:
	activerecord/lib/active_record/relation/query_methods.rb
2014-08-18 14:07:37 -03:00
Rafael Mendonça França
8742bc9d5e No need to check model_name anymore 2014-08-17 23:02:27 -03:00
Rafael Mendonça França
d2d809868c Merge pull request #15889 from carnesmedia/model-name
Use #model_name on instances instead of classes
2014-08-17 23:01:13 -03:00
Godfrey Chan
008f3da383 Don't expose these new APIs yet (added in 877ea78 / #16189)
WARNING: don't use them! They might change or go away between future beta/RC/
patch releases!

Also added a CHANGELOG entry for this.
2014-08-16 23:09:10 -07:00
Sean Griffin
877ea784e4 Implement _was and changes for in-place mutations of AR attributes 2014-08-16 23:08:41 -07:00
Akira Matsuda
bc116a55ca AM, AP, AV, and AMo tests are already order_independent! 2014-08-13 21:25:10 +09:00
Rafael Mendonça França
e81f3c210e Nobody sucks so nobody should call this awful method name 2014-08-12 10:51:41 -03:00
Akira Matsuda
6ffb29d24e users_dont_suck_but_only_we_suck_and_only_our_tests_are_order_dependent!
Calling ActiveSupport::TestCase.i_suck_and_my_tests_are_order_dependent! in AS::TestCase makes
everyone's tests order dependent, which should never be done by the framework.
2014-08-12 19:37:04 +09:00
Carlos Antonio da Silva
9a0e0594ab Fix typo [ci skip] 2014-08-07 09:17:23 -03:00
Yevhene Shemet
f8dcb365df Allow password to contain spaces only. 2014-08-06 22:11:06 +03:00
Justin Weiss
2bb0abbec0 Add a note on custom validation contexts.
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]
2014-08-05 13:13:40 -07:00
Carlos Antonio da Silva
28f6b895c6 Call public methods rather than class_eval'ing 2014-08-05 08:18:39 -03:00
Rafael Mendonça França
74c31ac5fe Merge pull request #15959 from aditya-kapoor/remove-unneeded-cases
remove unneeded test model for ActiveModel test cases.
2014-07-29 18:08:07 -03:00