Commit Graph

1972 Commits

Author SHA1 Message Date
Ryuta Kamizono
a954e1e817 Merge pull request #29018 from willbryant/missing_attributes_after_save
fix the dirty tracking code's save hook overwriting missing attribute…
2018-01-03 05:14:18 +09:00
Ryuta Kamizono
8af6f1ccd4 Refactor to Array(options[:on]) only once in defining validations 2018-01-01 06:18:39 +09:00
Yoshiyuki Hirano
b20354afcc Bump license years for 2018 2017-12-31 22:36:55 +09:00
Yoshiyuki Hirano
470d0e459f Fix validation callbacks on multiple context
I found a bug that validation callbacks don't fire on multiple context.
So I've fixed it.

Example:

```ruby
class Dog
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks

  attr_accessor :history

  def initialize
    @history = []
  end

  before_validation :set_before_validation_on_a, on: :a
  before_validation :set_before_validation_on_b, on: :b
  after_validation :set_after_validation_on_a, on: :a
  after_validation :set_after_validation_on_b, on: :b

  def set_before_validation_on_a; history << "before_validation on a"; end
  def set_before_validation_on_b; history << "before_validation on b"; end
  def set_after_validation_on_a;  history << "after_validation on a" ; end
  def set_after_validation_on_b;  history << "after_validation on b" ; end
end
```

Before:

```
d = Dog.new
d.valid?([:a, :b])
d.history # []
```

After:

```
d = Dog.new
d.valid?([:a, :b])
d.history # ["before_validation on a", "before_validation on b", "after_validation on a", "after_validation on b"]
```
2017-12-20 00:27:19 +09:00
Yasuo Honda
bd4211ea13 Suppress warning: BigDecimal.new is deprecated in Active Model
`BigDecimal.new` has been deprecated in BigDecimal 1.3.3
 which will be a default for Ruby 2.5.

Refer ruby/bigdecimal@5337373

* This commit has been made as follows:

```ruby
$ cd activemodel/
$ git grep -l BigDecimal.new | grep \.rb | xargs sed -i -e "s/BigDecimal.new/BigDecimal/g"
```

* This commit has been tested with these Ruby versions:

```
ruby 2.5.0dev (2017-12-15 trunk 61262) [x86_64-linux]
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-linux]
```
2017-12-14 19:00:00 +00:00
Tom Copeland
683c8fd277 Fix doc typo [ci skip] 2017-12-12 16:34:19 -05:00
Lonre Wang
a6031e40ba
Update validates.rb 2017-12-10 22:17:48 +07:00
Rafael Mendonça França
2837d0f334
Preparing for 5.2.0.beta2 release 2017-11-28 14:41:02 -05:00
Fatos Morina
37cf9b3466 Fix typos and add a few suggestions 2017-11-28 19:27:43 +01:00
Sean Griffin
95b86e57a6 Change how AttributeSet::Builder receives its defaults
There are two concerns which are both being combined into one here, but
both have the same goal. There are certain attributes which we want to
always consider initialized. Previously, they were handled separately.
The primary key (which is assumed to be backed by a database column)
needs to be initialized, because there is a ton of code in Active Record
that assumes `foo.id` will never raise. Additionally, we want attributes
which aren't backed by a database column to always be initialized, since
we would never receive a database value for them.

Ultimately these two concerns can be combined into one. The old
implementation hid a lot of inherent complexity, and is hard to optimize
from the outside. We can simplify things significantly by just passing
in a hash.

This has slightly different semantics from the old behavior, in that
`Foo.select(:bar).first.id` will return the default value for the
primary key, rather than `nil` unconditionally -- however, the default
value is always `nil` in practice.
2017-11-27 14:06:51 -07:00
Rafael Mendonça França
cceeeb6e57
Preparing for 5.2.0.beta1 release 2017-11-27 14:50:03 -05:00
Rafael França
233d6a2b56
Merge pull request #31117 from renuo/fix_errors_added
fix bug on added? method
2017-11-13 16:13:54 -05:00
Alessandro Rodi
15cb4efadb fix bug on added? method
fix rubocop issues
2017-11-13 17:27:47 +01:00
Ryuta Kamizono
24b59434e6
Add missing autoload Type (#31123)
Attribute modules (`Attribute`, `Attributes`, `AttributeSet`) uses
`Type`, but referencing `Type` before the modules still fail.

```
% ./bin/test -w test/cases/attribute_test.rb -n test_with_value_from_user_validates_the_value
Run options: -n test_with_value_from_user_validates_the_value --seed 31876

E

Error:
ActiveModel::AttributeTest#test_with_value_from_user_validates_the_value:
NameError: uninitialized constant ActiveModel::AttributeTest::Type
    /Users/kamipo/src/github.com/rails/rails/activemodel/test/cases/attribute_test.rb:233:in `block in <class:AttributeTest>'

bin/test test/cases/attribute_test.rb:232

Finished in 0.002985s, 335.0479 runs/s, 335.0479 assertions/s.
1 runs, 1 assertions, 0 failures, 1 errors, 0 skips
```

Probably we need more autoloading at least `Type`.
2017-11-11 06:43:54 +09:00
Ryuta Kamizono
fc7a6c7381 Add missing require "active_support/core_ext/hash/indifferent_access"
https://travis-ci.org/rails/rails/jobs/300163454#L2236
2017-11-10 23:48:53 +09:00
yuuji.yaginuma
1cddc91a02 Add missing requires
Currently, executing the test with only `attribute_test.rb` results in an error.

```
./bin/test -w test/cases/attribute_test.rb
Run options: --seed 41205

# Running:

....E

Error:
ActiveModel::AttributeTest#test_attributes_do_not_equal_attributes_with_different_types:
NameError: uninitialized constant ActiveModel::AttributeTest::Type
    rails/activemodel/test/cases/attribute_test.rb:159:in `block in <class:AttributeTest>'

bin/test test/cases/attribute_test.rb:158
```

Added a missing require to fix this.
2017-11-10 19:26:32 +09:00
Ryuta Kamizono
2da92b7a94
Merge pull request #31114 from y-yagi/fix_ruby_warnings_in_active_model
Fix ruby warnings in Active Model
2017-11-10 17:48:23 +09:00
yuuji.yaginuma
5ed618e192 Fix "warning: assigned but unused variable - name" 2017-11-10 17:21:58 +09:00
yuuji.yaginuma
c59ab795ee Add missing requires
Currently, executing the test with only `attribute_set_test.rb` results in an error.

```
./bin/test -w test/cases/attribute_set_test.rb
Run options: --seed 33470

# Running:

E

Error:
ActiveModel::AttributeSetTest#test_#map_returns_a_new_attribute_set_with_the_changes_applied:
NameError: uninitialized constant ActiveModel::AttributeSetTest::AttributeSet
Did you mean?  ActiveModel::Attributes
               ActiveModel::Attribute
    activemodel/test/cases/attribute_set_test.rb:235:in `block in <class:AttributeSetTest>'

bin/test test/cases/attribute_set_test.rb:234
```

Added a missing require to fix this.

Also, I suspect that this is the cause of failures in CI.
Ref: https://travis-ci.org/rails/rails/jobs/299994708
2017-11-10 14:28:02 +09:00
yuuji.yaginuma
7d862ffb62 Fix "warning: instance variable @attributes not initialized" 2017-11-10 14:02:28 +09:00
Lisa Ugray
c3675f50d2 Move Attribute and AttributeSet to ActiveModel
Use these to back the attributes API.  Stop automatically including
ActiveModel::Dirty in ActiveModel::Attributes, and make it optional.
2017-11-09 14:29:39 -05:00
Rafael França
2e585e4040
Merge pull request #31061 from bogdanvlviv/test-if-unless-options-for-validations
Add cases to test combining validation conditions
2017-11-06 22:48:02 -05:00
bogdanvlviv
3ff2c15837
Add cases to test combining validation conditions
- Test condition that is defined by array of conditions
 - Test condition that is defined by combining :if and :unless
 - Test local condition that is defined by :if
 - Test local condition that is defined by :unless

See http://edgeguides.rubyonrails.org/active_record_validations.html#combining-validation-conditions
2017-11-06 23:07:51 +00:00
bogdanvlviv
7308991630
Execute ConfirmationValidator validation when _confirmation's value is false 2017-11-05 19:24:49 +00:00
Matt Rohrer
b8b089ef11
Allow passing a Proc or Symbol as an argument to length validator values
This brings the Length validator in line with the Numericality
validator, which currently supports Proc & Symbol arguments
2017-10-26 10:01:06 +02:00
Sean Griffin
b7912a3040 Merge pull request #30920 from lugray/attributes_to_am
Start bringing attributes API to AM
2017-10-23 14:48:46 -04:00
Rafael Mendonça França
c792354adc
Remove deprecated :if and :unless string filter for callbacks 2017-10-23 12:51:04 -04:00
Akira Matsuda
8c2ba84d63 [Active Model] require => require_relative
This basically reverts ee5cfc01a5797f854c8441539b0cae326a81b963
2017-10-21 22:48:27 +09:00
Lisa Ugray
7e9ded512d Start bringing attributes API to AM
This is the first PR of a WIP to bring the attributes API to
ActiveModel.  It is not yet ready for public API.

The `attributes_dirty_test.rb` file was created based on `dirty_test.rb`,
and the simplifications in the diff do much to motivate this change.

```
diff activemodel/test/cases/dirty_test.rb activemodel/test/cases/attributes_dirty_test.rb
3a4
> require "active_model/attributes"
5c6
< class DirtyTest < ActiveModel::TestCase
---
> class AttributesDirtyTest < ActiveModel::TestCase
7,41c8,12
<     include ActiveModel::Dirty
<     define_attribute_methods :name, :color, :size
<
<     def initialize
<       @name = nil
<       @color = nil
<       @size = nil
<     end
<
<     def name
<       @name
<     end
<
<     def name=(val)
<       name_will_change!
<       @name = val
<     end
<
<     def color
<       @color
<     end
<
<     def color=(val)
<       color_will_change! unless val == @color
<       @color = val
<     end
<
<     def size
<       @size
<     end
<
<     def size=(val)
<       attribute_will_change!(:size) unless val == @size
<       @size = val
<     end
---
>     include ActiveModel::Model
>     include ActiveModel::Attributes
>     attribute :name, :string
>     attribute :color, :string
>     attribute :size, :integer
```
2017-10-18 13:05:30 -04:00
Matthew Draper
2e6658ae51 Clarify intentions around method redefinitions
Don't use remove_method or remove_possible_method just before a new
definition: at best the purpose is unclear, and at worst it creates a
race condition.

Instead, prefer redefine_method when practical, and
silence_redefinition_of_method otherwise.
2017-09-01 14:27:13 +09:30
Yoshiyuki Hirano
a2f2d2617b Use tt in doc for ActiveRecord [ci skip] 2017-08-27 14:01:29 +09:00
Yoshiyuki Hirano
3a11e0586e Update MIT licenses link [ci skip] 2017-08-22 08:46:02 +09:00
Viktar Basharymau
53c98196e6 Simplify ActiveModel::Errors#generate_message
Besides making the code easier to read, this commit
also makes it faster:

* We don't eval `@base.class.respond_to?(:i18n_scope)` twice
* We only eval `@base.class.i18n_scope` once
* We don't call `flatten!` because it's not needed anymore
* We don't call `compact` because all elements are Symbols
2017-08-18 17:36:59 +03:00
Ryuta Kamizono
6089b3140d [ci skip] Postgres --> PostgreSQL 2017-08-08 18:20:44 +09:00
Rafael Mendonça França
ff657e73f0 Talk about bytes not characters
[ci skip]

Closes #30012
2017-07-31 17:42:43 -04:00
Rafael França
8fd69db783 Merge pull request #29788 from kamipo/remove_unused_mutex_m
Remove unused `Mutex_m` in Active Model
2017-07-17 13:54:37 -04:00
Sean Griffin
1519e976b2 Allow multiparameter assigned attributes to be used with text_field
Between 4.2 and 5.0 the behavior of how multiparameter attributes
interact with `_before_type_cast` changed. In 4.2 it returns the
post-type-cast value. After 5.0, it returns the hash that gets sent to
the type. This behavior is correct, but will cause an issue if you then
tried to render that value in an input like `text_field` or
`hidden_field`.

In this case, we want those fields to use the post-type-cast form,
instead of the `_before_type_cast` (the main reason it uses
`_before_type_cast` at all is to avoid losing data when casting a
non-numeric string to integer).

I've opted to modify `came_from_user?` rather than introduce a new
method for this as I want to avoid complicating that contract further,
and technically the multiparameter hash didn't come from assignment, it
was constructed internally by AR.

Close #27888.
2017-07-17 11:19:15 -04:00
Kir Shatrov
d7b1521db8 Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00
Ryuta Kamizono
12e6cba9cf Make generated_attribute_methods to private
Because `generated_attribute_methods` is an internal API.
2017-07-14 13:26:45 +09:00
Ryuta Kamizono
2992faa45b Remove unused Mutex_m in Active Model 2017-07-14 13:08:44 +09:00
Jahfer Husain
3650ca983c Add ActiveModel::Errors#merge!
ActiveModel::Errors#merge! allows ActiveModel::Errors to append errors from
a separate ActiveModel::Errors instance onto their own.

Example:

    person = Person.new
    person.errors.add(:name, :blank)

    errors = ActiveModel::Errors.new(Person.new)
    errors.add(:name, :invalid)

    person.errors.merge!(errors)
    puts person.errors.messages
    # => { name: ["can't be blank", "is invalid"] }
2017-07-07 14:32:59 -04:00
Lisa Ugray
7b2dfdeab6 Fix ActiveModel::Type::DateTime#serialize
`ActiveModel::Type::DateTime#serialize` should return a `Time` object
so that finding by a datetime column works correctly.
2017-07-05 13:10:15 -04:00
Xavier Noria
92c29d82eb Merge branch 'master' into require_relative_2017 2017-07-02 13:50:25 -07:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Matthew Draper
3420a14590 Merge pull request #29540 from kirs/rubocop-frozen-string
Enforce frozen string in Rubocop
2017-07-02 01:11:50 +09:30
Matthew Draper
afb66a5a59 Merge pull request #29506 from pat/frozen-string-literals
Make ActiveSupport frozen-string-literal friendly.
2017-07-02 01:07:12 +09:30
Akira Matsuda
ee5cfc01a5 [Active Model] require => require_relative 2017-07-01 18:38:04 +09:00
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Rafael França
cf8c46938b Merge pull request #29588 from greysteil/add-gemspec-links
Add source code and changelog links to gemspecs
2017-06-28 11:24:02 -04:00
Ryuta Kamizono
9fdebb17ff ✂️
[ci skip]
2017-06-28 22:21:03 +09:00