Commit Graph

1523 Commits

Author SHA1 Message Date
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
c363fff29f, 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 e969c92846 and
bd2b3fbe54.
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 bc116a55ca,
cbde413df3,
bf0a67931d, and
2440933fe2.
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
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
Chun-wei Kuo 524f7b494a Fix example code of `EachValidator` [ci skip]
We have to specify the `:title` option to really use the
`TitleValidator` defined above.
2014-07-28 18:35:28 +08:00
Rafael Mendonça França 08754f12e6 Merge branch 'rm-remove-mocha'
Conflicts:
	actionpack/test/abstract_unit.rb
2014-07-19 18:17:13 -03:00
Rafael Mendonça França fd6aaaa0c3 Stop requiring mocha automatically
We are planning to remove mocha from our test suite because of
performance problems. To make this possible we should stop require mocha
on ActionSupport::TestCase.

This should not affect applications since users still need to add mocha
to Gemfile and this already load mocha.

Added FIXME notes to place that still need mocha removal
2014-07-19 17:35:12 -03:00
Aaron Patterson be9f868cb6 %i doesn't work on 1.9 2014-07-17 16:08:38 -07:00
Matthew Draper e213b37fc1 Merge pull request #16210 from sonnym/assert_valid_keys_in_validate
Check for valid options in validate method
2014-07-18 07:10:49 +09:30
sonnym 0950d409b0 check for valid options in validate method
This change prevents a certain class of user error which results when
mistakenly using the `validate` class method instead of the `validates`
class method.

Only apply when all arguments are symbols, because some validations use
the `validate` method and pass in additional options, namely the
`LenghValidator` via the `ActiveMode::Validations::validates_with`
method.
2014-07-17 17:32:28 -04:00
Rafael Mendonça França 1a300b6748 Make restore_attributes public
Also make it accept a list of attributes to be changed. This will make
possible to restore only a subset of the changed attributes.

Closes #16203
2014-07-17 14:55:28 -03:00
Rafael Mendonça França 41fb06fa47 Deprecate `reset_#{attribute}` in favor of `restore_#{attribute}`.
These methods may cause confusion with the `reset_changes` that
behaves differently
of them.

Also rename undo_changes to restore_changes to match this new set of
methods.
2014-07-15 18:09:38 -03:00
Rafael Mendonça França 66d0a01535 Deprecate ActiveModel::Dirty#reset_changes in favor of #clear_changes_information
This method name is causing confusion with the `reset_#{attribute}`
methods. While `reset_name` set the value of the name attribute for the
previous value the `reset_changes` only discard the changes and previous
changes.
2014-07-15 16:04:31 -03:00
Santosh Wadghule c890f4f8b2 [ci skip] Little bit doc code improvement. 2014-07-14 10:09:14 +05:30
Godfrey Chan 9eb15ed6a0 Only automatically include validations when enabled
This is a follow up to #16024.
2014-07-02 15:07:57 -07:00