model_view 0.1.3 → 0.2.0
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 +4 -0
- data/README.md +33 -4
- data/lib/model_view/version.rb +1 -1
- data/lib/model_view.rb +10 -1
- data/spec/model_view/model_view_spec.rb +25 -0
- 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: 5bbb9e6927b9d2a9a2350d4b2b9fa3bb914a125d
|
4
|
+
data.tar.gz: 58d8e67a612acda3b04469af9573ef2f71bfc2bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68014a9a7f17ad08e169fed2cb92d2582e886821e51bb7690412cec03263a97204bd1c80f127855f907ef52050abbbf42c5edf0b52e57fe64f99d3ab81233250
|
7
|
+
data.tar.gz: 4283cf11f8300868265c6763fef87a6626d0cd61926bfa822fc7b23099848ba4e12d68f60fd8f59d174e05657cec2b2497657090d714aa75373429ccdb13da22
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -49,6 +49,35 @@ PersonView.as_hash(person, context: {current_user: current_user})
|
|
49
49
|
}
|
50
50
|
```
|
51
51
|
|
52
|
+
#### 🐒-patching the model
|
53
|
+
|
54
|
+
ModelView can add a convenience method to the model class
|
55
|
+
|
56
|
+
Example
|
57
|
+
```ruby
|
58
|
+
class PersonModelView
|
59
|
+
model Person
|
60
|
+
field :id
|
61
|
+
end
|
62
|
+
|
63
|
+
p = Person.find 1
|
64
|
+
|
65
|
+
p.as_hash(context: {current_user: current_user})
|
66
|
+
=> {
|
67
|
+
id: 1
|
68
|
+
}
|
69
|
+
```
|
70
|
+
|
71
|
+
When using ModelView in Rails, remember to add an initializer that requires your model views.
|
72
|
+
|
73
|
+
Example initializer:
|
74
|
+
```ruby
|
75
|
+
# require.rb
|
76
|
+
Dir["#{Rails.root}/app/model_views/*.rb"].each do |file|
|
77
|
+
require File.basename(file, File.extname(file))
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
52
81
|
#### Scopes
|
53
82
|
|
54
83
|
Scopes allows you to create serialisation snippets that can be composed
|
@@ -60,7 +89,7 @@ field :id
|
|
60
89
|
scope :demographics do
|
61
90
|
field :name
|
62
91
|
field(:first_name) { |person| person.name.split(' ').first }
|
63
|
-
field(:last_name) { |person| person.name.split(' ').
|
92
|
+
field(:last_name) { |person| person.name.split(' ').last }
|
64
93
|
end
|
65
94
|
|
66
95
|
scope :status do
|
@@ -78,13 +107,13 @@ end
|
|
78
107
|
```ruby
|
79
108
|
person = Person.find(1)
|
80
109
|
|
81
|
-
PersonView.as_hash(person, context: {current_user: current_user})
|
110
|
+
PersonView.as_hash(person, {context: {current_user: current_user}})
|
82
111
|
=> {
|
83
112
|
id: 1
|
84
113
|
}
|
85
114
|
|
86
115
|
|
87
|
-
PersonView.as_hash(person, context: {current_user: current_user, scope: :demographics})
|
116
|
+
PersonView.as_hash(person, {context: {current_user: current_user}, scope: :demographics})
|
88
117
|
=> {
|
89
118
|
id: 1,
|
90
119
|
name: "Billy Bob",
|
@@ -92,7 +121,7 @@ PersonView.as_hash(person, context: {current_user: current_user, scope: :demogra
|
|
92
121
|
last_name: "Bob",
|
93
122
|
}
|
94
123
|
|
95
|
-
PersonView.as_hash(person, context: {current_user: current_user, scope: :all})
|
124
|
+
PersonView.as_hash(person, {context: {current_user: current_user}, scope: :all})
|
96
125
|
=> {
|
97
126
|
id: 1,
|
98
127
|
name: "Billy Bob",
|
data/lib/model_view/version.rb
CHANGED
data/lib/model_view.rb
CHANGED
@@ -36,12 +36,21 @@ module ModelView
|
|
36
36
|
@scopes
|
37
37
|
end
|
38
38
|
|
39
|
-
def as_hash(object, opts={}
|
39
|
+
def as_hash(object, opts={})
|
40
40
|
scope = opts[:scope] || ROOT
|
41
41
|
context = opts[:context] || {}
|
42
42
|
ModelView::Resolver.resolve(object, @scopes, scope , context)
|
43
43
|
end
|
44
44
|
|
45
|
+
def model(model_class)
|
46
|
+
model_view_class = self
|
47
|
+
model_class.class_eval do
|
48
|
+
define_method(:as_hash) do |opts|
|
49
|
+
model_view_class.as_hash(self, opts)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
45
54
|
private
|
46
55
|
|
47
56
|
def add_scope(scope_name)
|
@@ -143,4 +143,29 @@ describe ModelView do
|
|
143
143
|
end
|
144
144
|
end
|
145
145
|
end
|
146
|
+
|
147
|
+
describe :model do
|
148
|
+
before do
|
149
|
+
class MyModel
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
it "adds an as_hash method to the model" do
|
155
|
+
dummy_class.model MyModel
|
156
|
+
|
157
|
+
expect(MyModel.new).to respond_to(:as_hash)
|
158
|
+
end
|
159
|
+
|
160
|
+
it "adds an as_hash method to the model that defers to the model view's as_hash method" do
|
161
|
+
dummy_class.model MyModel
|
162
|
+
instance = MyModel.new
|
163
|
+
|
164
|
+
expect(dummy_class).to receive(:as_hash).with(instance, {})
|
165
|
+
|
166
|
+
instance.as_hash({})
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
146
171
|
end
|