Commit b15f777
committed
Integrate with Active Model Attributes
The `schema { ... }` interface pre-dates the Active Model Attributes API
(defined as early as [v5.2.0][]), but clearly draws inspiration from
Active Record's Database Schema and Attribute casting (which was
extracted into `ActiveModel::Attributes`).
However, the type information captured in `schema { ... }` blocks or
assigned as `Hash` arguments to `schema=` is purely inert metadata.
Proposal
---
This commit aims to integrate with [ActiveModel::Model][] and
[ActiveModel::Attributes][]. Through the introduction of both modules,
subclasses of `ActiveResource::Schema` can benefit from type casting
attributes and constructing instances with default values.
This commit makes minimally incremental changes, prioritizing backwards
compatibility. The reliance on `#respond_to_missing?` and
`#method_missing` is left largely unchanged. Similarly, the `Schema`
interface continues to provide metadata about its attributes through the
`Schema#attr` method (instead of reading from
`ActiveModel::Attributes#attribute_names` or
`ActiveModel::Attributes.attribute_types`).
API Changes
---
To cast values to their specified types, declare the Schema with the
`:cast_values` set to true.
```ruby
class Person < ActiveResource::Base
schema cast_values: true do
integer 'age'
end
end
p = Person.new
p.age = "18"
p.age # => 18
```
To configure inheriting resources to cast values, set the `cast_values`
class attribute:
```ruby
class ApplicationResource < ActiveResource::Base
self.cast_values = true
end
class Person < ApplicationResource
schema do
integer 'age'
end
end
p = Person.new
p.age = "18"
p.age # => 18
```
To set all resources application-wide to cast values, set
`config.active_resource.cast_values`:
```ruby
# config/application.rb
config.active_resource.cast_values = true
```
[v5.2.0]: https://api.rubyonrails.org/v5.2.0/classes/ActiveModel/Attributes/ClassMethods.html
[ActiveModel::Model]: https://api.rubyonrails.org/classes/ActiveModel/Model.html
[ActiveModel::Attributes]: https://api.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html1 parent b7728b1 commit b15f777
File tree
4 files changed
+301
-44
lines changed- lib/active_resource
- test/cases
- base
4 files changed
+301
-44
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
383 | 389 | | |
384 | 390 | | |
385 | 391 | | |
| |||
430 | 436 | | |
431 | 437 | | |
432 | 438 | | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
439 | 461 | | |
440 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
441 | 478 | | |
442 | | - | |
| 479 | + | |
| 480 | + | |
443 | 481 | | |
444 | 482 | | |
445 | 483 | | |
| |||
479 | 517 | | |
480 | 518 | | |
481 | 519 | | |
| 520 | + | |
482 | 521 | | |
483 | 522 | | |
484 | 523 | | |
| |||
1308 | 1347 | | |
1309 | 1348 | | |
1310 | 1349 | | |
| 1350 | + | |
1311 | 1351 | | |
1312 | 1352 | | |
1313 | 1353 | | |
| |||
1341 | 1381 | | |
1342 | 1382 | | |
1343 | 1383 | | |
| 1384 | + | |
1344 | 1385 | | |
1345 | 1386 | | |
1346 | 1387 | | |
| |||
1380 | 1421 | | |
1381 | 1422 | | |
1382 | 1423 | | |
1383 | | - | |
| 1424 | + | |
1384 | 1425 | | |
1385 | 1426 | | |
1386 | 1427 | | |
1387 | 1428 | | |
1388 | | - | |
| 1429 | + | |
1389 | 1430 | | |
1390 | 1431 | | |
1391 | 1432 | | |
| |||
1596 | 1637 | | |
1597 | 1638 | | |
1598 | 1639 | | |
1599 | | - | |
| 1640 | + | |
1600 | 1641 | | |
1601 | 1642 | | |
1602 | 1643 | | |
| |||
1614 | 1655 | | |
1615 | 1656 | | |
1616 | 1657 | | |
| 1658 | + | |
1617 | 1659 | | |
1618 | 1660 | | |
1619 | 1661 | | |
| |||
1673 | 1715 | | |
1674 | 1716 | | |
1675 | 1717 | | |
1676 | | - | |
| 1718 | + | |
1677 | 1719 | | |
1678 | 1720 | | |
1679 | 1721 | | |
| |||
1684 | 1726 | | |
1685 | 1727 | | |
1686 | 1728 | | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
1687 | 1733 | | |
1688 | 1734 | | |
1689 | 1735 | | |
| |||
1836 | 1882 | | |
1837 | 1883 | | |
1838 | 1884 | | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
1839 | 1901 | | |
1840 | 1902 | | |
1841 | 1903 | | |
1842 | | - | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
1843 | 1907 | | |
1844 | 1908 | | |
1845 | 1909 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
5 | 9 | | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
11 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
12 | 25 | | |
13 | 26 | | |
14 | 27 | | |
| |||
22 | 35 | | |
23 | 36 | | |
24 | 37 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | 38 | | |
29 | | - | |
30 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
31 | 44 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
46 | 53 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
58 | 86 | | |
59 | 87 | | |
60 | 88 | | |
0 commit comments