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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c5dbabbb8d69d5dadabc4592e8ac4dc6c705c26
4
- data.tar.gz: 226889168c0093984f854e7717e5decf9658c19f
3
+ metadata.gz: d6f1228c9decb1c8c548d0641e35dc935ee4473b
4
+ data.tar.gz: db0e1c74ad97f5ffb60de645f7407cf80f79cadc
5
5
  SHA512:
6
- metadata.gz: 526855a236a1e2f026da812c2deca02ef02b174f7e0d37f44da02a4d5783ebc98c013c04efe2909b5cf3b451d6dafd01fa50b0f6c6a617852296bd2d0eaea0db
7
- data.tar.gz: 1cdb32908ed57177c8f278d7af4730549a5b57fe5ad30555e32bcbf42c3e5faeb8eb3711a9f20663b2134d9062cac0a31a4e912d60270da698801fb8d2af8bbe
6
+ metadata.gz: 3be2f2b46b4a7a9302716a8f917688c7b5f2d1a01d2a09c4096f62074a9f4336b34850d037623465b2fc96a6045044a9782923b4a3e901d2c544596586105cda
7
+ data.tar.gz: 0ba01965454e3aa695bb7068902bdb51ef4b4b4c576c6e6756f492f1cd4d7f109424fe3511e7ee6ae46a23715214f34f8b6eaf519fd115068d691c48e2bae703
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ # 0.4.0
4
+ * Added constant and alias_for helpers
5
+
6
+
3
7
  # 0.3.0
4
8
  * ModelView can be used to update models
5
9
 
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 `after_save` block:
165
+ To avoid having to explicitly save the model in every block, one can define an `after_update` block:
160
166
  ```ruby
161
- after_save { |obj| obj.save! }
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
+ ```
@@ -36,7 +36,11 @@ module ModelView
36
36
  private
37
37
 
38
38
  def evaluate_field(object, field_name, options, block, context)
39
- if block.nil?
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
@@ -1,3 +1,3 @@
1
1
  module ModelView
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pretorius