Commit Graph

1441 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
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
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
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
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
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
Akira Matsuda
ee5cfc01a5 [Active Model] require => require_relative 2017-07-01 18:38:04 +09:00
Ryuta Kamizono
ab0e455881 Fix call-seq typo s/==/<=>/ [ci skip]
Fixes #29512.
2017-06-21 11:52:02 +09:00
shotat
b2999d631f add frozen string literal comment 2017-06-15 11:45:15 +09:00
shotat
2396f79fec freeze string 2017-06-14 17:49:54 +09:00
shotat
285cba022c enhance active model assignment 2017-06-14 10:31:12 +09:00
Viktor Fonic
ea2850b96a Docs: Fix output representation [ci skip]
The output of two string attributes is displayed differently in the docs. Standardize the output by always showing it as a comment.
2017-05-31 10:21:51 +08:00
David Heinemeier Hansson
1c275d812f Add option for class_attribute default (#29270)
* Allow a default value to be declared for class_attribute

* Convert to using class_attribute default rather than explicit setter

* Removed instance_accessor option by mistake

* False is a valid default value

* Documentation
2017-05-29 18:01:50 +02:00
Bradley Priest
d83b8e6510 Fix regression in Numericality validator where extra decimal places on
a user input for a decimal column were ignored by numerically validations
2017-05-27 21:58:35 +08:00
T.J. Schuck
f4f6c512b9 Fix broken RDoc formatting
[ci skip]
2017-05-26 18:33:38 -04:00
bogdanvlviv
40bdbce191
Define path with __dir__
".. with __dir__ we can restore order in the Universe." - by @fxn

Related to 5b8738c2df003a96f0e490c43559747618d10f5f
2017-05-23 00:53:51 +03:00
dixpac
4f39556577 Improving docs for callbacks execution order [ci skip]
When define callbacks latest definition on the same callback/method
overwrites previous ones.
2017-05-21 18:45:59 +02:00
stve
e80d9f411d
fix ActiveModel::Validator#kind code examples [ci skip] 2017-05-02 22:52:01 -04:00
stve
0a5e87d16b
remove uniqueness validators from ActiveModel examples 2017-05-02 22:27:50 -04:00
Ryuta Kamizono
c7f12f1529 Fix regexp in the doc [ci skip]
Follow up of #17148.
2017-04-24 20:46:00 +09:00