azeroth 0.8.2 → 0.9.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
  SHA256:
3
- metadata.gz: b37d53edb3f9b83aca0a7b121040c8db535da8b40d1bbc863c4d0b368dabe65d
4
- data.tar.gz: 567d011f2f370e24d4b182182cfed7d08e46e344e533f6caccc1529c1c318643
3
+ metadata.gz: fef3f6d11872a3a25c77900028239d743c809aa1aae8d59526df351a1c47e97c
4
+ data.tar.gz: ae58f2737c72e939067a86061dbc3ab20f1da0ef0d27615c753aacbebb0591d1
5
5
  SHA512:
6
- metadata.gz: 8c89d250bf8825965909dcc57aac1adb333cc06995cc27e381a78a442fd3c0ce187d33f6b39b2f3a002eeb97e9ca5462a44d2f81ca957d3aa8810bd1441b8081
7
- data.tar.gz: 0eb7564aa351c821bf88cf25d7ca3991eb239089b492dc31587526b437067f9436fdb175c86a7ca937fc457d76fb546e4c8cc5159236cc88145ed06ac215e3f6
6
+ metadata.gz: ed3b96c2b672b737239713899bfb8ef5546dd1e66d28327fb3a7a66540eede50e93f5e73a3890dcea0474eda4f4bceaaa6804da47a1ea174606430eac14afe93
7
+ data.tar.gz: ce7d30afc6594731f31aa0591c6747dd110db2f485557d51fffd14cb4f7199e1ab4ebbfb748109dc42da72bda1764de48bd25821461f1ddf15fc49c607a971be
data/README.md CHANGED
@@ -11,7 +11,7 @@ Azeroth
11
11
 
12
12
  Yard Documentation
13
13
  -------------------
14
- [https://www.rubydoc.info/gems/azeroth/0.8.2](https://www.rubydoc.info/gems/azeroth/0.8.2)
14
+ [https://www.rubydoc.info/gems/azeroth/0.9.0](https://www.rubydoc.info/gems/azeroth/0.9.0)
15
15
 
16
16
  Azeroth has been designed making the coding of controllers easier
17
17
  as routes in controllers are usually copy, paste and replace of same
@@ -61,6 +61,7 @@ It accepts options
61
61
  - before_save: Method/Proc to be ran before saving the resource on create or update
62
62
  - after_save: Method/Proc to be ran after saving the resource on create or update
63
63
  - build_with: Method/Block to be ran when building the reource on create
64
+ - update_with: Method/Block to be ran when updating the reource on update
64
65
  - paginated: Flag when pagination should be applied
65
66
  - per_page: Number of items returned when pagination is active
66
67
 
@@ -42,6 +42,21 @@ module Azeroth
42
42
  controller.instance_variable_set("@#{variable}", value)
43
43
  end
44
44
 
45
+ # Forces a controller to run a block
46
+ #
47
+ # When the block is a +Proc+ that is evaluated,
48
+ # when it is a +Symbol+ or +String+, a method is called
49
+ #
50
+ # @return [Object] whatever the block returns
51
+ def run(block)
52
+ case block
53
+ when Proc
54
+ controller.instance_eval(&block)
55
+ else
56
+ controller.send(block)
57
+ end
58
+ end
59
+
45
60
  private
46
61
 
47
62
  attr_reader :controller
@@ -21,6 +21,7 @@ module Azeroth
21
21
  before_save: nil,
22
22
  after_save: nil,
23
23
  build_with: nil,
24
+ update_with: nil,
24
25
  paginated: false,
25
26
  per_page: 20
26
27
  }.freeze
@@ -110,6 +111,13 @@ module Azeroth
110
111
  #
111
112
  # @return [Symbol,Proc]
112
113
 
114
+ # @method update_with
115
+ # @api private
116
+ #
117
+ # Block or method name to be ran when updating the resource
118
+ #
119
+ # @return [Symbol,Proc]
120
+
113
121
  # @method paginated
114
122
  # @api private
115
123
  #
@@ -47,12 +47,7 @@ module Azeroth
47
47
  def build_resource
48
48
  return collection.build(attributes) unless build_with
49
49
 
50
- case build_with
51
- when Proc
52
- controller.instance_eval(&build_with)
53
- else
54
- controller.send(build_with)
55
- end
50
+ controller.run(build_with)
56
51
  end
57
52
 
58
53
  # @private
@@ -8,6 +8,8 @@ module Azeroth
8
8
  class Update < RequestHandler
9
9
  private
10
10
 
11
+ delegate :update_with, to: :options
12
+
11
13
  # @private
12
14
  #
13
15
  # Updates and return an instance of the model
@@ -17,20 +19,33 @@ module Azeroth
17
19
  #
18
20
  # @return [Object]
19
21
  def resource
20
- @resource ||= update_resource
22
+ @resource ||= perform_update
21
23
  end
22
24
 
23
- # build resource for update
25
+ # Update a resource saving it to the database
24
26
  #
25
- # @return [Object]
26
- def update_resource
27
- controller.send(model.name).tap do |entry|
27
+ # @return [Object] updated resource
28
+ def perform_update
29
+ @resource = controller.send(model.name)
30
+ resource.tap do
28
31
  trigger_event(:save) do
29
- entry.update(attributes)
32
+ update_and_save_resource
30
33
  end
31
34
  end
32
35
  end
33
36
 
37
+ # @private
38
+ #
39
+ # Update the resource, either by running update_with
40
+ # or directly updating the attributes in the object
41
+ #
42
+ # @return [Object] updated resource
43
+ def update_and_save_resource
44
+ return resource.update(attributes) unless update_with
45
+
46
+ controller.run(update_with)
47
+ end
48
+
34
49
  # @private
35
50
  #
36
51
  # Response status
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Azeroth
4
- VERSION = '0.8.2'
4
+ VERSION = '0.9.0'
5
5
  end
@@ -30,6 +30,118 @@ describe Azeroth::RequestHandler::Update do
30
30
  end
31
31
  end
32
32
 
33
+ context 'when update_with option is given ' do
34
+ context 'with block' do
35
+ it_behaves_like 'a request handler' do
36
+ let(:block) do
37
+ proc do
38
+ document.assign_attributes(document_params)
39
+ document.name = "#{document.name}!"
40
+ document.save
41
+ end
42
+ end
43
+
44
+ let(:options_hash) do
45
+ {
46
+ update_with: block
47
+ }
48
+ end
49
+
50
+ let(:expected_resource) { document }
51
+
52
+ let(:extra_params) do
53
+ {
54
+ id: document.id,
55
+ document: {
56
+ name: 'New Name'
57
+ }
58
+ }
59
+ end
60
+
61
+ let(:expected_json) do
62
+ {
63
+ 'name' => 'New Name!'
64
+ }
65
+ end
66
+
67
+ it 'updates the values given by request' do
68
+ expect { handler.process }
69
+ .to change { document.reload.name }
70
+ .from(document.name)
71
+ .to('New Name!')
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'with symbol' do
77
+ it_behaves_like 'a request handler' do
78
+ let(:options_hash) do
79
+ {
80
+ update_with: :add_bang_name
81
+ }
82
+ end
83
+
84
+ let(:expected_resource) { document }
85
+
86
+ let(:extra_params) do
87
+ {
88
+ id: document.id,
89
+ document: {
90
+ name: 'New Name'
91
+ }
92
+ }
93
+ end
94
+
95
+ let(:expected_json) do
96
+ {
97
+ 'name' => 'New Name!'
98
+ }
99
+ end
100
+
101
+ it 'updates the values given by request' do
102
+ expect { handler.process }
103
+ .to change { document.reload.name }
104
+ .from(document.name)
105
+ .to('New Name!')
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'with string' do
111
+ it_behaves_like 'a request handler' do
112
+ let(:options_hash) do
113
+ {
114
+ update_with: 'add_bang_name'
115
+ }
116
+ end
117
+
118
+ let(:expected_resource) { document }
119
+
120
+ let(:extra_params) do
121
+ {
122
+ id: document.id,
123
+ document: {
124
+ name: 'New Name'
125
+ }
126
+ }
127
+ end
128
+
129
+ let(:expected_json) do
130
+ {
131
+ 'name' => 'New Name!'
132
+ }
133
+ end
134
+
135
+ it 'updates the values given by request' do
136
+ expect { handler.process }
137
+ .to change { document.reload.name }
138
+ .from(document.name)
139
+ .to('New Name!')
140
+ end
141
+ end
142
+ end
143
+ end
144
+
33
145
  context 'with before_save block option' do
34
146
  it_behaves_like 'a request handler' do
35
147
  let(:block) do
@@ -28,4 +28,10 @@ class RequestHandlerController < ActionController::Base
28
28
  documents.where(reference: 'X-MAGIC-15')
29
29
  .build(document_params)
30
30
  end
31
+
32
+ def add_bang_name
33
+ document.assign_attributes(document_params)
34
+ document.name = "#{document.name}!"
35
+ document.save
36
+ end
31
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azeroth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-16 00:00:00.000000000 Z
11
+ date: 2022-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport