model_view 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +102 -1
- data/lib/model_view.rb +4 -2
- data/lib/model_view/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71429c06ee7aa9db651f524b4c91ad9c339ae689
|
4
|
+
data.tar.gz: f44a76b5e2fe0847f6ea86da4729278fa9296b14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '01014719b0d56562ff129f4dd5f1a8eb2f54210b46bd365b89abf4e8ff0360035edbcb3a00fc6515aa222b061ee1f57259c1fce34c8cbd5d215669bf98f718ed'
|
7
|
+
data.tar.gz: 76f2f35fc561f8afaa2195f79f1257df20c1c68d4f2d7d9d861b0a8cc72e39985bfae54d4ac77cdcb34883818e720ddd9d7584973e1aba488c94b2d75b028c78
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,2 +1,103 @@
|
|
1
1
|
# model_view
|
2
|
-
Composable serialisation for models
|
2
|
+
## Composable serialisation for models
|
3
|
+
|
4
|
+
ModelView let's you define views for your models in one place.
|
5
|
+
|
6
|
+
### Why ModelView?
|
7
|
+
|
8
|
+
At OfferZen, most of our rails models can be presented in al least two ways. For example, an Interview Request
|
9
|
+
is presented differently to a candidate than to a company. But, there is also a lot of fields that get presented
|
10
|
+
the same to both candidates and companies. We found ourselves duplicating a lot of our serialisation code in our
|
11
|
+
controllers and the code started to get out of hand.
|
12
|
+
|
13
|
+
ModelView works with Rails, but Rails is definitely not a requirement
|
14
|
+
|
15
|
+
### How to use ModelView
|
16
|
+
|
17
|
+
1) Include the yem in your gemfile
|
18
|
+
```ruby
|
19
|
+
gem "model_view", '~> 0.1'
|
20
|
+
```
|
21
|
+
2) Create a view class (we put these in `app/model_views`)
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class PersonView
|
25
|
+
extend ModelView
|
26
|
+
|
27
|
+
end
|
28
|
+
```
|
29
|
+
3) Define fields
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
field :name
|
33
|
+
|
34
|
+
field(:first_name) { |person| person.name.split(' ').first }
|
35
|
+
|
36
|
+
field :is_current_user, context[:current_user] do |person|
|
37
|
+
person.name.split(' ').first
|
38
|
+
end
|
39
|
+
```
|
40
|
+
4) Let ModelView serialise an instance
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
person = Person.find(1)
|
44
|
+
PersonView.as_hash(person, context: {current_user: current_user})
|
45
|
+
=> {
|
46
|
+
name: "Billy Bob",
|
47
|
+
first_name: "Billy",
|
48
|
+
is_current_user: false
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Scopes
|
53
|
+
|
54
|
+
Scopes allows you to create serialisation snippets that can be composed
|
55
|
+
|
56
|
+
Example:
|
57
|
+
```ruby
|
58
|
+
field :id
|
59
|
+
|
60
|
+
scope :demographics do
|
61
|
+
field :name
|
62
|
+
field(:first_name) { |person| person.name.split(' ').first }
|
63
|
+
field(:last_name) { |person| person.name.split(' ').first }
|
64
|
+
end
|
65
|
+
|
66
|
+
scope :status do
|
67
|
+
field :is_current_user, context[:current_user] do |person|
|
68
|
+
person.name.split(' ').first
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
scope :all do
|
73
|
+
extend_scope :demographics
|
74
|
+
extend_scope :status
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
person = Person.find(1)
|
80
|
+
|
81
|
+
PersonView.as_hash(person, context: {current_user: current_user})
|
82
|
+
=> {
|
83
|
+
id: 1
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
PersonView.as_hash(person, context: {current_user: current_user, scope: :demographics})
|
88
|
+
=> {
|
89
|
+
id: 1,
|
90
|
+
name: "Billy Bob",
|
91
|
+
first_name: "Billy",
|
92
|
+
last_name: "Bob",
|
93
|
+
}
|
94
|
+
|
95
|
+
PersonView.as_hash(person, context: {current_user: current_user, scope: :all})
|
96
|
+
=> {
|
97
|
+
id: 1,
|
98
|
+
name: "Billy Bob",
|
99
|
+
first_name: "Billy",
|
100
|
+
last_name: "Bob",
|
101
|
+
is_current_user: true
|
102
|
+
}
|
103
|
+
```
|
data/lib/model_view.rb
CHANGED
@@ -36,8 +36,10 @@ module ModelView
|
|
36
36
|
@scopes
|
37
37
|
end
|
38
38
|
|
39
|
-
def as_hash(object,
|
40
|
-
|
39
|
+
def as_hash(object, opts={} )
|
40
|
+
scope = opts[:scope] || ROOT
|
41
|
+
context = opts[:context] || {}
|
42
|
+
ModelView::Resolver.resolve(object, @scopes, scope , context)
|
41
43
|
end
|
42
44
|
|
43
45
|
private
|
data/lib/model_view/version.rb
CHANGED