model_view 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +13 -2
- data/lib/model_view/resolver.rb +5 -1
- data/lib/model_view/version.rb +1 -1
- data/spec/model_view/resolver_spec.rb +17 -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: d6f1228c9decb1c8c548d0641e35dc935ee4473b
|
4
|
+
data.tar.gz: db0e1c74ad97f5ffb60de645f7407cf80f79cadc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3be2f2b46b4a7a9302716a8f917688c7b5f2d1a01d2a09c4096f62074a9f4336b34850d037623465b2fc96a6045044a9782923b4a3e901d2c544596586105cda
|
7
|
+
data.tar.gz: 0ba01965454e3aa695bb7068902bdb51ef4b4b4c576c6e6756f492f1cd4d7f109424fe3511e7ee6ae46a23715214f34f8b6eaf519fd115068d691c48e2bae703
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -36,7 +36,13 @@ field(:first_name) { |person| person.name.split(' ').first }
|
|
36
36
|
field :is_current_user, {context: current_user} do |person, current_user|
|
37
37
|
person == current_user
|
38
38
|
end
|
39
|
+
|
40
|
+
field :type, constant: 'User'
|
41
|
+
|
42
|
+
field :person_name, alias_for: :name
|
39
43
|
```
|
44
|
+
|
45
|
+
|
40
46
|
4) Let ModelView serialise an instance
|
41
47
|
|
42
48
|
```ruby
|
@@ -156,9 +162,9 @@ setter(:name) do |obj, name|
|
|
156
162
|
end
|
157
163
|
```
|
158
164
|
|
159
|
-
To avoid having to explicitly save the model in every block, one can define an `
|
165
|
+
To avoid having to explicitly save the model in every block, one can define an `after_update` block:
|
160
166
|
```ruby
|
161
|
-
|
167
|
+
after_update { |obj| obj.save! }
|
162
168
|
|
163
169
|
field :telephone_number, setter: true
|
164
170
|
setter(:name) do |obj, name|
|
@@ -167,3 +173,8 @@ setter(:name) do |obj, name|
|
|
167
173
|
obj.last_name = last_name
|
168
174
|
end
|
169
175
|
```
|
176
|
+
|
177
|
+
Models can then be updated using ModelView.update:
|
178
|
+
```ruby
|
179
|
+
PersonView.update(person, {phone: "+123 456 7890"}, scope: :contact_details)
|
180
|
+
```
|
data/lib/model_view/resolver.rb
CHANGED
@@ -36,7 +36,11 @@ module ModelView
|
|
36
36
|
private
|
37
37
|
|
38
38
|
def evaluate_field(object, field_name, options, block, context)
|
39
|
-
if
|
39
|
+
if options && options[:constant]
|
40
|
+
options[:constant]
|
41
|
+
elsif options && options[:alias_for]
|
42
|
+
object.send(options[:alias_for])
|
43
|
+
elsif block.nil?
|
40
44
|
object.send(field_name)
|
41
45
|
else
|
42
46
|
if block.arity == 1
|
data/lib/model_view/version.rb
CHANGED
@@ -15,7 +15,9 @@ describe ModelView::Resolver do
|
|
15
15
|
fields: {
|
16
16
|
field4: {block: Proc.new { |obj| obj.field4 + obj.field1 }},
|
17
17
|
field5: {},
|
18
|
-
field6: {}
|
18
|
+
field6: {},
|
19
|
+
field7a: {args: {constant: 'foo'}},
|
20
|
+
field7b: {args: {alias_for: :field5}},
|
19
21
|
},
|
20
22
|
},
|
21
23
|
scope2: {
|
@@ -101,6 +103,20 @@ describe ModelView::Resolver do
|
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
106
|
+
context "fields with a constant" do
|
107
|
+
it "resolves to the constant" do
|
108
|
+
res = ModelView::Resolver.resolve(instance, scopes, :scope1)
|
109
|
+
expect(res[:field7a]).to eq('foo')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "fields with an alias" do
|
114
|
+
it "resolves to the aliased property" do
|
115
|
+
res = ModelView::Resolver.resolve(instance, scopes, :scope1)
|
116
|
+
expect(res[:field7b]).to eq(5)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
104
120
|
context "fields with a block" do
|
105
121
|
context "with an arity of one" do
|
106
122
|
it "evaluates the block" do
|