Commit Graph

1691 Commits

Author SHA1 Message Date
Akira Matsuda
dcecbb4234 File encoding is defaulted to utf-8 in Ruby >= 2.1 2015-09-18 17:05:05 +09:00
Semyon Pupkov
aef6b79967 Fix typo in activemodel changelog 2015-09-08 23:37:17 +05:00
Dmitry Polushkin
e3d99e239d Validate multiple contexts on valid? and invalid? at once.
Example:

```ruby
class Person
  include ActiveModel::Validations

  attr_reader :name, :title
  validates_presence_of :name, on: :create
  validates_presence_of :title, on: :update
end

person = Person.new
person.valid?([:create, :update])    # => true
person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
```
2015-09-07 22:42:50 +01:00
Rafael Mendonça França
7b9b0b531f Revert "Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once"
This reverts commit 51dd2588433457960cca592d5b5dac6e0537feac, reversing
changes made to ecb4e4b21b3222b823fa24d4a0598b1f2f63ecfb.

This broke Active Record tests
2015-09-07 17:16:54 -03:00
Rafael Mendonça França
51dd258843 Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once
Validate multiple contexts on `valid?` and `invalid?` at once
2015-09-07 16:46:18 -03:00
Prakash Laxkar
3ee1f7b40b Removed unused config file 2015-09-03 08:40:03 +05:30
Aditya Kapoor
69f3f81e6c Add missing test for #17351 2015-09-01 17:35:50 +05:30
Carlos Antonio da Silva
8ce0fdb5c4 Fix failure introduced by #17351 due to the new mocks implementation
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.
2015-09-01 08:53:29 -03:00
Jashank Jeremy
09f64873cd Fix syntax error introduced by #17351. 2015-09-01 19:11:26 +10:00
Rafael Mendonça França
57393957e1 Merge pull request #17351 from akshat-sharma/master
Add case_sensitive option for confirmation validation
2015-09-01 02:42:43 -03:00
Akshat Sharma
2438a1cf4e Add case_sensitive option for confirmation validation
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
2015-09-01 10:42:51 +05:30
Robin Dupret
7a27de2bb0 Tiny documentation improvements [ci skip] 2015-08-28 15:33:48 +02:00
Ronak Jangir
8d7bf97798 Removed duplicate requiring minitest/mock as it is already required in method_call_assertions 2015-08-26 19:43:54 +05:30
Gaurav Sharma
71ed339203 discard xml Serialization documentation that is no longer available [ci skip] 2015-08-22 04:06:14 +05:30
Marcin Olichwirowicz
3c6fc5892f Rename match_attribute_method? to matched_attribute_method
`match_attribute_method?` is a bit confusing because it suggest
that a return value is a boolean which is not true.
2015-08-12 00:23:12 +02:00
Zachary Scott
f7ebdb1ac5 Remove XML Serialization from core.
This includes the following classes:

- ActiveModel::Serializers::Xml
- ActiveRecord::Serialization::XmlSerializer
2015-08-07 11:01:48 -04:00
Dmitry Polushkin
86e3b047ba Validate multiple contexts on valid? and invalid? at once.
Example:

```ruby
class Person
  include ActiveModel::Validations

  attr_reader :name, :title
  validates_presence_of :name, on: :create
  validates_presence_of :title, on: :update
end

person = Person.new
person.valid?([:create, :update])    # => true
person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
```
2015-07-30 10:05:29 +01:00
yuuji.yaginuma
0a20e48d52 pass the correct argument to mock on a test of validates_length_of 2015-07-28 22:59:42 +09:00
schneems
5bb1d4d288 Freeze string literals when not mutated.
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](b0e2da69f0/lib/let_it_go/core_ext/string.rb (L37)) please [give me a pull request to the appropriate file](b0e2da69f0/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)
2015-07-19 17:45:10 -05:00
Guo Xiang Tan
beb07fbfae Revert "Revert "Reduce allocations when running AR callbacks.""
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}"
```
2015-07-16 01:02:15 +08:00
Zamith
a9ab7e85fd Removes unnecessary comments from i18n validations tests [ci skip]
These comments do not add a lot to the readability, grepability or
overall understanding of the tests, therefore I believe they can be
safely removed.
2015-07-11 12:29:46 +01:00
Zamith
ec7771e7f6 Remove the reference to mocha in activemodel
Activemodel is no longer dependent on mocha, so we can make the comments
more generic.
2015-07-11 02:08:45 +01:00
Kasper Timm Hansen
8a389ffc10 Use private method call assertions in Active Model tests.
Also fix Minitest constant reference.
2015-07-10 23:45:42 +02:00
Rafael Mendonça França
f6f3772c17 Merge pull request #20803 from TheBlasfem/marking_serialization_class
marking serialization class in Readme
2015-07-08 00:45:37 -03:00
Julio Lopez
a18b24f71f marking serialization class 2015-07-07 21:58:22 -05:00
Yves Senn
4cc5917dce docs, clarify the meanaing of return values from validation methods.
[ci skip]

Closes #20792.

Custom validation methods are implemented in terms of
callbacks. The `validate` callback chain can't be halted using return
values of individual callbacks.
2015-07-07 14:39:58 +02:00
Yves Senn
b8962664ad docs, remove accidental :nodoc: of ActiveModel::Validations::ClassMethods methods.
[ci skip]

While this :nodoc: did hide the constant it also removed the following
methods from the API docs:

- #attribute_method?
- #clear_validators!
- #validate
- #validators
- #validators_on

Those are public API and should be visible.

Issue was caused by dee4fbc

/cc @zzak
2015-07-07 14:16:03 +02:00
Robin Dupret
bc71e43252 Separate the constraint and other options [ci skip]
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]
2015-07-01 16:20:07 +02:00
Radan Skoric
e5a78aaaf8 Improve Validation Helpers' documentation comments and tests 2015-06-27 16:34:16 +02:00
Robin Dupret
e09129c94c A few documentation fixes [ci skip] 2015-06-23 17:55:37 +02:00
Mehmet Emin İNAÇ
5b36015830 Add nodoc to the Validations::Helpers [ci skip] 2015-06-22 20:06:42 +03:00
Yves Senn
ebc4c607a7 docs, ✂️ wrongly placed heading. [ci skip]
The heading "Active Model Length Validator" was shown on the
"ActiveModel::Validations" page without any text following it.
2015-06-22 15:52:47 +02:00
Roque Pinel
bfd4e6a0bc Move the validations HelperMethods to its own file
Closes #11209

[Roque Pinel & Steven Yang]
2015-06-21 19:56:53 -04:00
Yves Senn
9946788775 select the AR adapter through bin/test. 2015-06-11 14:24:56 +02:00
Yves Senn
54d84cbb77 use our runner (bin/test) for framework components.
This adds a script `bin/test` to most Rails framework components. The
script uses the rails minitest plugin to augment the runner.
See https://github.com/rails/rails/pull/19571 for details about the
plugin.

I did not yet add `bin/test` for activerecord, activejob and railties.
These components rely on specific setup performed in the rake-tasks.
2015-06-11 14:12:15 +02:00
Rafael Mendonça França
96bb004fc6 Revert "Add code example for include option of AM::Serialization#serializable_hash"
This reverts commit 3d949f34816d6eca0a6b59cfa08d91f36e8e64dd.

This was already documented in other PR.
2015-06-10 08:50:39 -03:00
Radan Skoric
3d949f3481 Add code example for include option of AM::Serialization#serializable_hash 2015-06-09 17:41:14 -03:00
Anton Davydov
3f92a8247c Fix typo in AM I18n validation test name [skip ci] 2015-06-09 01:23:20 +03:00
Robin Dupret
b10c3866d7 Tiny documentation edits [ci skip] 2015-06-07 16:01:09 +02:00
Robin Dupret
911f189f88 Merge pull request #20004 from rusikf/patch-1
add docs to include option at ActiveModel::Serialization [ci skip]
2015-06-07 15:53:53 +02:00
Rafael Mendonça França
109e71d2bb Require yaml for isolation test
It was removed when we removed mocha at
5a6ae7f7539216931f2b3f4aa53394ac4136c74e
2015-05-29 18:22:39 -03:00
unknown
3833d4529f formatting changes 2015-05-29 16:27:23 -04:00
Roque Pinel
5a6ae7f753 Remove use of mocha from Active Model 2015-05-28 18:35:17 -05:00
Arun Agrawal
21b6b68f63 Remove unused package tasks
We are using `all:build` now.
2015-05-28 09:06:10 +02:00
Yves Senn
852460852c Merge pull request #20262 from arunagw/aa-remove-broken-unused-release-task
Remove broken and unused release task
2015-05-27 09:14:19 +02:00
karanarora
0750330941 Spelling/typo/grammatical fixes [ci skip]
spelling fix [ci skip]

example to be consistent [ci skip]

grammatical fix

typo fixes [ci skip]
2015-05-23 03:01:33 +05:30
Arun Agrawal
4194d052d8 Remove broken and unused release task
- We do release with release.rb
- There is no `rake/gemcutter`
2015-05-22 14:30:30 +02:00
claudiob
0080a886b2 [ci skip] Don’t encourage sudo gem install
I think we are better off leaving `sudo` outside of the documented
way of installing gems (`activerecord`, `actionpack`, …).

We don’t want newbies to think that `sudo` is required or, even worse, than
they actually have to type `[sudo] gem install`.

In most scenarios, `sudo` is not needed to install gems, and people who do
need it, probably already know about it.

What do you think? 😁
2015-05-12 14:51:19 -07:00
rusikf
47896b3d08 add docs to include option at ActiveModel::Serialization#serializable_hash [ci skip] 2015-05-11 13:10:30 +03:00
claudiob
58fc63fd96 Stop skipping a test that now works on Rubinius
The test was skipped because of an issue that, in the meantime,
has been fixed: https://github.com/rubinius/rubinius/issues/3328.

Using the latest Rubinius (the one currently on Travis CI), this
is the result:

```sh
$ ruby --version
rubinius 2.5.3 (2.1.0 2482b093 2015-05-10 3.5.1 JI) [x86_64-darwin14.3.0]
```

**Before this PR**

```sh
$ ruby -Itest test/cases/attribute_assignment_test.rb
Run options: --seed 58569

.....S...

Finished in 0.048278s, 186.4203 runs/s, 269.2738 assertions/s.

9 runs, 13 assertions, 0 failures, 0 errors, 1 skips

You have skipped tests. Run with --verbose for details.
```

**After this PR**
$ ruby -Itest test/cases/attribute_assignment_test.rb
Run options: --seed 35720

.........

Finished in 0.029441s, 305.6961 runs/s, 475.5273 assertions/s.

9 runs, 14 assertions, 0 failures, 0 errors, 0 skips
```
2015-05-10 16:24:46 -07:00