Commit Graph

17 Commits

Author SHA1 Message Date
Sean Griffin
b63d532f1e Don't assume all hashes are from multiparameter assignment in composed_of
So this bug is kinda funky. The code path is basically "if we weren't passed an
instance of the class we compose to, and we have a converter, call that".
Ignoring the hash case for a moment, everything after that was roughly intended
to be the "else" clause, meaning that we are expected to have an instance of
the class we compose to. Really, we should be blowing up in that case, as we
can give a much better error message than what they user will likely get (e.g.
`NameError: No method first for String` or something). Still, Ruby is duck
typed, so if the object you're assigning responds to the same methods as the
type you compose to, knock yourself out.

The hash case was added in 36e9be8 to remove a bunch of special cased code from
multiparameter assignment. I wrongly assumed that the only time we'd get a hash
there is in that case. Multiparameter assignment will construct a very specific
hash though, where the keys are integers, and we will have a set of keys
covering `1..part.size` exactly. I'm pretty sure this could actually be passed
around as an array, but that's a different story. Really I should convert this
to something like `class MultiParameterAssignment < Hash; end`, which I might
do soon. However for a change that I'm willing to backport to 4-2-stable, this
is what I want to go with for the time being.

Fixes #25978
2016-08-05 09:52:09 -04:00
Sean Griffin
ff4986b917 Ensure hashes can be passed to attributes using composed_of
This behavior was broken by 36e9be85. When the value is assigned
directly, either through mass assignment or directly assigning a hash,
the hash gets passed through to this writer method directly. While this
is intended to handle certain cases, when an explicit converter has been
provided, we should continue to use that instead. The positioning of the
added guard caused the new behavior to override that case.

Fixes #25210
2016-05-31 10:03:26 -04:00
Anupam Choudhury
220887eeab Removed unnecessary class 2013-04-06 02:17:38 +05:30
Akira Matsuda
7bc224dda0 remove meaningless AS::FrozenObjectError 2013-01-02 10:04:58 +09:00
Francesco Rodriguez
47cce917f2 fix order dependent test in AggregationsTest
Ensure class variable is set to nil.

It prevents the following test to fail:

    def test_do_not_run_the_converter_when_nil_was_set
      customers(:david).non_blank_gps_location = nil
      assert_nil Customer.gps_conversion_was_run
    end

Check https://github.com/rails/rails/blob/master/activerecord/test/models/customer.rb#L7
for more information.
2012-08-26 16:32:08 -05:00
Rafael Mendonça França
f4d818d51e Revert "Removing composed_of from ActiveRecord."
This reverts commit 14fc8b34521f8354a17e50cd11fa3f809e423592.

Reason: we need to discuss a better path from this removal.

Conflicts:
	activerecord/lib/active_record/reflection.rb
	activerecord/test/cases/base_test.rb
	activerecord/test/models/developer.rb
2012-07-27 19:25:14 -03:00
Steve Klabnik
14fc8b3452 Removing composed_of from ActiveRecord.
This feature adds a lot of complication to ActiveRecord for dubious
value. Let's talk about what it does currently:

class Customer < ActiveRecord::Base
  composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
end

Instead, you can do something like this:

    def balance
      @balance ||= Money.new(value, currency)
    end

    def balance=(balance)
      self[:value] = balance.value
      self[:currency] = balance.currency
      @balance = balance
    end

Since that's fairly easy code to write, and doesn't need anything
extra from the framework, if you use composed_of today, you'll
have to add accessors/mutators like that.

Closes #1436
Closes #2084
Closes #3807
2012-06-18 14:53:03 -04:00
Yves Senn
fa5f037551 allow the :converter Proc form composed_of to return nil
This makes it possible to filter invalid input values before they are passed
into the value-object (like empty strings). This behaviour is only relevant
if the :allow_nil options is set to true. Otherwise you will get
the resulting NoMethodError.
2012-05-03 17:11:42 +02:00
Xavier Noria
fb6b805620 code gardening: we have assert_(nil|blank|present), more concise, with better default failure messages - let's use them 2010-08-17 03:32:11 +02:00
Neeraj Singh
df6aa8e246 removing extra whitespaces
Signed-off-by: José Valim <jose.valim@gmail.com>
2010-07-18 15:13:14 +02:00
Emilio Tagua
fdb7f84eb1 Remove deprecated block usage in composed_of. 2010-06-24 11:17:05 -03:00
Neeraj Singh
b462952886 Use better assertion methods for testing
[#4645 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
2010-05-19 10:18:36 +02:00
Jeremy Kemper
e8550ee032 Cherry-pick core extensions 2009-05-13 12:00:15 -07:00
Michael Koziarski
9460d45988 Add missing assert_deprecated calls to prevent spam during test runs 2008-09-11 11:21:22 +02:00
Rob Anderton
2cee51d5c1 Added :constructor and :converter options to composed_of and deprecated the conversion block
Signed-off-by: Michael Koziarski <michael@koziarski.com>
2008-09-10 18:28:47 +02:00
Jeremy Kemper
39814fcce0 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8681 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
2008-01-21 17:20:51 +00:00
Jeremy Kemper
42b39ae3f2 Move tests to cases
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8660 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
2008-01-18 07:30:42 +00:00