diff options
author | Lisa Ugray <lisa.ugray@shopify.com> | 2017-10-12 14:13:45 -0400 |
---|---|---|
committer | Lisa Ugray <lisa.ugray@shopify.com> | 2017-10-18 13:05:30 -0400 |
commit | 7e9ded512dd58a923a3be038843708dbba4215f6 (patch) | |
tree | cf31a52147933fb1275d4e7e7a1ae2bb59418f12 /activemodel/test/cases/attributes_test.rb | |
parent | ded41a9b84299f536bc136d78069a50a2e867768 (diff) | |
download | rails-7e9ded512dd58a923a3be038843708dbba4215f6.tar.gz rails-7e9ded512dd58a923a3be038843708dbba4215f6.tar.bz2 rails-7e9ded512dd58a923a3be038843708dbba4215f6.zip |
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
```
Diffstat (limited to 'activemodel/test/cases/attributes_test.rb')
-rw-r--r-- | activemodel/test/cases/attributes_test.rb | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb new file mode 100644 index 0000000000..064cba40e3 --- /dev/null +++ b/activemodel/test/cases/attributes_test.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require "cases/helper" +require "active_model/attributes" + +module ActiveModel + class AttributesTest < ActiveModel::TestCase + class ModelForAttributesTest + include ActiveModel::Model + include ActiveModel::Attributes + + attribute :integer_field, :integer + attribute :string_field, :string + attribute :decimal_field, :decimal + attribute :string_with_default, :string, default: "default string" + attribute :date_field, :string, default: -> { Date.new(2016, 1, 1) } + attribute :boolean_field, :boolean + end + + class ChildModelForAttributesTest < ModelForAttributesTest + end + + class GrandchildModelForAttributesTest < ChildModelForAttributesTest + attribute :integer_field, :string + end + + test "properties assignment" do + data = ModelForAttributesTest.new( + integer_field: "2.3", + string_field: "Rails FTW", + decimal_field: "12.3", + boolean_field: "0" + ) + + assert_equal 2, data.integer_field + assert_equal "Rails FTW", data.string_field + assert_equal BigDecimal.new("12.3"), data.decimal_field + assert_equal "default string", data.string_with_default + assert_equal Date.new(2016, 1, 1), data.date_field + assert_equal false, data.boolean_field + + data.integer_field = 10 + data.string_with_default = nil + data.boolean_field = "1" + + assert_equal 10, data.integer_field + assert_nil data.string_with_default + assert_equal true, data.boolean_field + end + + test "dirty" do + data = ModelForAttributesTest.new( + integer_field: "2.3", + string_field: "Rails FTW", + decimal_field: "12.3", + boolean_field: "0" + ) + + assert_equal false, data.changed? + + data.integer_field = "2.1" + + assert_equal false, data.changed? + + data.string_with_default = "default string" + + assert_equal false, data.changed? + + data.integer_field = "5.1" + + assert_equal true, data.changed? + assert_equal true, data.integer_field_changed? + assert_equal({ "integer_field" => [2, 5] }, data.changes) + end + + test "nonexistent attribute" do + assert_raise ActiveModel::UnknownAttributeError do + ModelForAttributesTest.new(nonexistent: "nonexistent") + end + end + + test "children inherit attributes" do + data = ChildModelForAttributesTest.new(integer_field: "4.4") + + assert_equal 4, data.integer_field + end + + test "children can override parents" do + data = GrandchildModelForAttributesTest.new(integer_field: "4.4") + + assert_equal "4.4", data.integer_field + end + end +end |