Commit Graph

1306 Commits

Author SHA1 Message Date
Rafael Mendonça França
8ecc5ab1d8 Start Rails 5.1 development 🎉 2016-05-10 03:46:56 -03:00
Rafael Mendonça França
fbdcf5221a Preparing for 5.0.0.rc1 release 2016-05-06 16:54:40 -05:00
Christian Blais
17141481d9 Change RangeError to a more specific ActiveModel::RangeError
The should make it easier for apps to rescue ActiveModel specific
errors without the need to wrap all method calls with a generic
rescue RangeError.
2016-05-03 14:46:49 -04:00
lvl0nax
517cf249c3
Chomp: prefer String#chomp where we can for a clarity boost
Closes #24766, #24767

Signed-off-by: Jeremy Daer <jeremydaer@gmail.com>
2016-04-29 13:43:15 -07:00
eileencodes
f7a986012a Prep Rails 5 beta 4 2016-04-27 15:48:47 -05:00
Mohit Natoo
51d6a59329 - [ci skip] regardless is usually followed by of and not by if .. or not. 2016-04-21 00:04:05 +05:30
Bogdan Gusiev
80b9e5d7bd Fixed bug introduced in #24519. Makes build green again 2016-04-13 21:13:33 +03:00
Bogdan Gusiev
afb1f32e79 Use keyword arguments to cleanup without droping performance 2016-04-13 02:03:51 +03:00
Sen-Zhang
051d859880 prevent 'attribute_changed?' from returning nil 2016-04-11 19:33:52 -07:00
Jeremy Daer
20ffb63c2e Merge pull request #24511 from lihanli/activemodel-dirty-attribute-changed
speed up ActiveModel::Dirty#attribute_changed?
2016-04-11 15:14:44 -07:00
Lihan Li
73e2dbe651 set default parameter to nil to speed up attribute_changed?
Benchmark results:

Warming up --------------------------------------
            old code    32.176k i/100ms
            new code    34.837k i/100ms
Calculating -------------------------------------
            old code      1.595M (± 3.5%) i/s -      7.947M
            new code      1.942M (± 3.9%) i/s -      9.685M
2016-04-11 17:46:34 -04:00
Vipul A M
ac027338e4 Pass over all Rails 5 warnings, to make sure:
- we are ending sentences properly
- fixing of space issues
- fixed continuity issues in some sentences.

Reverts 8fc97d198e .
This change reverts making sure we add '.' at end of deprecation sentences.
This is to keep sentences within Rails itself consistent and with a '.' at the end.
2016-04-12 02:26:56 +05:30
PareshGupta
57f5f51bd2 fix length validation error typo 2016-04-07 15:37:17 +05:30
Prathamesh Sonpatki
7bdb4b5598 Allow passing record being validated to error message generator
- Pass object to I18n helper so that when calling message proc, it will
  pass that object as argument to the proc and we can generate custom
  error messages based on current record being validated.
- Based on https://github.com/rails/rails/issues/856.

[Łukasz Bandzarewicz, Prathamesh Sonpatki]
2016-04-05 12:55:02 +05:30
Kasper Timm Hansen
044baef101 Merge pull request #24299 from kitop/activemodel-errors-include-fix
Do not create a hash key when calling ActiveModel::Errors#include?
2016-03-25 13:24:41 +01:00
Sean Griffin
c7d3bd48df Apply scale before precision when coercing floats to decimal
Since precision is always larger than scale, it can actually change
rounding behavior. Given a precision of 5 and a scale of 3, when you
apply the precision of 5 to `1.25047`, the result is `1.2505`, which
when the scale is applied would be `1.251` instead of the expected
`1.250`.

This issue appears to only occur with floats, as scale doesn't apply to
other numeric types, and the bigdecimal constructor actually ignores
precision entirely when working with strings. There's no way we could
handle this for the "unknown object which responds to `to_d`" case, as
we can't assume an interface for applying the scale.

Fixes #24235
2016-03-24 16:09:19 -06:00
Esteban Pastorino
9848c4632f Do not create a hash key when calling ActiveModel::Errors#include?
From: https://github.com/rails/rails/issues/24279

Problem:
By doing `record.errors.include? :foo`, it adds a new key to the
@messages hash that defaults to an empty array.

This happens because of a combination of these 2 commits:
b97035df64
(Added in Rails 4.1)
and
6ec8ba16d8 (diff-fdcf8b65b5fb954372c6fe1ddf284c78R76)
(Rails 5.0)

By adding the default proc that returns an array for non-existing keys,
ruby adds that key to the hash.

Solution:
Change `#include?` to check with `has_key?` and then check if that value is
`present?`.

Add test case for ActiveModels::Errors#include?
2016-03-24 16:29:22 -04:00
ojab
3ece0d89d4 Use Range#cover? for Date inclusion validator 2016-03-24 11:36:07 +00:00
yuuji.yaginuma
163df5d6a0 use same name to type object
Follow up to #24079
2016-03-09 20:47:45 +09:00
eileencodes
826420b5fc Prep release for Rails 5 beta3 2016-02-24 10:27:02 -05:00
eileencodes
2c02bc0a47 Revert changes to validations from PR #18612
In order to fix issue #17621 we added a check to validations that
determined if a record should be validated. Based on the existing tests
and behavior we wanted we determined the best way to do that was by
checking if `!record.peristed? || record.changed? || record.marked_for_destruction?`

This change didn't make it into a release until now. When #23790 was
opened we realized that `valid?` and `invalid?` were broken and did not
work on persisted records because of the `!record.persisted?`.

While there is still a bug that #17621 brought up, this change was too
drastic and should not be a RC blocker. I will work on fixing this so
that we don't break `valid?` but also aren't validating parent records
through child records if that parent record is validate false. This
change removes the code changes to validate and the corresponding tests.
It adds tests for two of the bugs found since Rails 5 beta2 release.

Fixes #17621
2016-02-23 15:21:46 -05:00
Santiago Pastorino
e8d58bb299 Merge pull request #23743 from maclover7/rm-unused-parameter
Remove unused parameter from method
2016-02-21 21:45:22 -03:00
eileencodes
6f15b276cb Always validate record if validating a virtual attribute
Fixes #23645

When you're using an `attr_accessor` for a record instead of an
attribute in the database there's no way for the record to know if it
has `changed?` unless you tell it `attribute_will_change!("attribute")`.

The change made in 27aa4dd updated validations to check if a record was
`changed?` or `marked_for_destruction?` or not `persisted?`. It did not
take into account virtual attributes that do not affect the model's
dirty status.

The only way to fix this is to always validate the record if the
attribute does not belong to the set of attributes the record expects
(in `record.attributes`) because virtual attributes will not be in that
hash.

I think we should consider deprecating this particular behavior in the
future and requiring that the user mark the record dirty by noting that
the virtual attribute will change. Unfortunately this isn't easy because
we have no way of knowing that you did the "right thing" in your
application by marking it dirty and will get the deprecation warning
even if you are doing the correct thing.

For now this restores expected behavior when using a virtual attribute
by always validating the record, as well as adds tests for this case.

I was going to add the `!record.attributes.include?(attribute)` to the
`should_validate?` method but `uniqueness` cannot validate a virtual
attribute with nothing to hold on to the attribute. Because of this
`should_validate?` was about to become a very messy method so I decided
to split them up so we can handle it specifically for each case.
2016-02-20 09:15:39 -05:00
Jon Moss
67ba041a32 Remove unused parameter from method
The `attribute` parameter is not used inside the `normalize_detail`
method. This does not need to go through a deprecation cycle, since the
method is private.
2016-02-17 16:09:54 -05:00
Mehmet Emin İNAÇ
e38ced376f Add documentation about method to describe how it works [ci skip] 2016-02-04 20:03:24 +02:00
Sean Griffin
49f6ce63f3 Preparing for Rails 5.0.0.beta2 2016-02-01 14:37:52 -07:00
Aaron Patterson
6dfab475ca Merge branch '5-0-beta-sec'
* 5-0-beta-sec:
  bumping version
  fix version update task to deal with .beta1.1
  Eliminate instance level writers for class accessors
  allow :file to be outside rails root, but anything else must be inside the rails view directory
  Don't short-circuit reject_if proc
  stop caching mime types globally
  use secure string comparisons for basic auth username / password
2016-01-25 11:25:11 -08:00
Aaron Patterson
908c011395 bumping version 2016-01-25 10:22:15 -08:00
Aaron Patterson
4642d68d80 Eliminate instance level writers for class accessors
Instance level writers can have an impact on how the Active Model /
Record objects are saved.  Specifically, they can be used to bypass
validations.  This is a problem if mass assignment protection is
disabled and specific attributes are passed to the constructor.

CVE-2016-0753
2016-01-22 15:02:46 -08:00
Sean Griffin
302e92359c Refactor tz aware types, add support for PG ranges
This is an alternate implementation to #22875, that generalizes a lot of
the logic that type decorators are going to need, in order to have them
work with arrays, ranges, etc. The types have the ability to map over a
value, with the default implementation being to just yield that given
value. Array and Range give more appropriate definitions.

This does not automatically make ranges time zone aware, as they need to
be added to the `time_zone_aware` types config, but we could certainly
make that change if we feel it is appropriate. I do think this would be
a breaking change however, and should at least have a deprecation cycle.

Closes #22875.

/cc @matthewd
2016-01-08 14:11:45 -07:00
Andrey Novikov
0a0ffb64d8 Take UTC offset into account when assigning string value to time attribute. 2016-01-05 18:35:04 +03:00
Rashmi Yadav
1b608a695c Update copyright notices to 2016 [ci skip] 2015-12-31 18:27:19 +02:00
Robert Eshleman
57fb74e081 Convert non-Numeric values to Floats 2015-12-22 16:01:50 -05:00
Robert Eshleman
9c330798b0 Fix Regression in Numericality Validations
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]
2015-12-22 14:27:25 -05:00
eileencodes
7eae0bb88e Change alpha to beta1 to prep for release of Rails 5
🎉 🍻
2015-12-18 12:14:09 -05:00
Vokhmin Alexey V
61e5081404 ActiveRecord::Base#becomes should copy the errors 2015-12-14 19:02:20 +03:00
Sean Griffin
574f255629 Use a bind param for LIMIT and OFFSET
We currently generate an unbounded number of prepared statements when
`limit` or `offset` are called with a dynamic argument. This changes
`LIMIT` and `OFFSET` to use bind params, eliminating the problem.

`Type::Value#hash` needed to be implemented, as it turns out we busted
the query cache if the type object used wasn't exactly the same object.

This drops support for passing an `Arel::Nodes::SqlLiteral` to `limit`.
Doing this relied on AR internals, and was never officially supported
usage.

Fixes #22250.
2015-12-14 08:40:02 -07:00
Yasuo Honda
589cef086f Avoid dummy_time_value to add "2000-01-01" twice 2015-11-30 20:04:46 +00:00
Sean Griffin
de9b870f40 Merge pull request #21000 from twalpole/find_or_parameter_issues
Update and fix forbidden attributes test issues caused by AC::Parameters change
2015-11-23 14:58:05 -07:00
Yves Senn
96cc2e8335 Merge pull request #22333 from harrykiselev/patch-3
[ci skip] Update dirty.rb: documentation fix.
2015-11-21 11:21:20 +01:00
Harry V. Kiselev
ea0617e789 Update dirty.rb: documentation fix.
ActiveModel::Dirty module documentation fix.
2015-11-19 00:45:54 +03:00
Jerry D'Antonio
23b6f65fd1 Require only necessary concurrent-ruby classes. 2015-11-04 21:12:28 -05:00
Thomas Walpole
85f7d955f3 Update and fix forbidden attributes tests
Add AC::Parameters tests for WhereChain#not
2015-11-03 11:34:07 -08:00
Sean Griffin
328ec26cbe Merge pull request #19851 from repinel/numericality-validation2
Use the post-type-cast version of the attribute to validate numericality
2015-10-20 17:05:44 -06:00
Sean Griffin
d6919c524a All strings returned by ImmutableString should be frozen
I seriously don't even know why we handle booleans, but those strings
should technically be frozen. Additionally, we don't need to actually
check the class in the mutable string type, since the `cast_value`
function will always return a string.
2015-10-15 09:55:30 -07:00
Sean Griffin
34321e4a43 Add an immutable string type to opt out of string duping
This type adds an escape hatch to apps for which string duping causes
unacceptable memory growth. The reason we are duping them is in order to
detect mutation, which was a feature added to 4.2 in #15674. The string
type was modified to support this behavior in #15788.

Memory growth is really only a concern for string types, as it's the
only mutable type where the act of coersion does not create a new object
regardless (as we're usually returning an object of a different class).

I do feel strongly that if we are going to support detecting mutation,
we should do it universally for any type which is mutable. While it is
less common and ideomatic to mutate strings than arrays or hashes, there
shouldn't be rules or gotchas to understanding our behavior.

However, I also appreciate that for apps which are using a lot of string
columns, this would increase the number of allocations by a large
factor. To ensure that we keep our contract, if you'd like to opt out of
mutation detection on strings, you'll also be option out of mutation of
those strings.

I'm not completely married to the thought that strings coming out of
this actually need to be frozen -- and I think the name is correct
either way, as the purpose of this is to provide a string type which
does not detect mutation.

In the new implementation, I'm only overriding `cast_value`. I did not
port over the duping in `serialize`. I cannot think of a reason we'd
need to dup the string there, and the tests pass without it.
Unfortunately that line was introduced at a time where I was not nearly
as good about writing my commit messages, so I have no context as to
why I added it. Thanks past Sean. You are a jerk.
2015-10-15 09:50:37 -07:00
Roman Pramberger
e8c2f0bebe use ActiveModel::Naming module instead of Model [ci skip]
Use the documented module instead of ActiveModel::Model.
This makes the example more focused.
2015-10-06 10:46:14 +02:00
Sean Griffin
7e6d1f13b1 Merge pull request #21809 from yui-knk/fix_doc_am_serialization
[ci skip] Fix explanation of `ActiveModel::Serialization`
2015-10-02 09:40:53 -04:00
Guo Xiang Tan
7d0b1e4847 Fix AC::Parameters not being sanitized for query methods. 2015-10-02 16:26:16 +08:00
yui-knk
1fdb98c033 [ci skip] Fix explanation of ActiveModel::Serialization
This explanation was change by https://github.com/rails/rails/commit/7a27de2b.
This change reversed the including module (`ActiveModel::Serializers::JSON`)
and the included module (`ActiveModel::Serialization`) by mistake.
2015-10-02 13:20:19 +09:00