azeroth 2.0.0 → 2.1.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/.gitignore +2 -0
- data/.rubocop.yml +1 -1
- data/README.md +5 -3
- data/lib/azeroth/controller_interface.rb +0 -2
- data/lib/azeroth/decorator.rb +0 -4
- data/lib/azeroth/options.rb +16 -0
- data/lib/azeroth/params_builder.rb +13 -3
- data/lib/azeroth/resource_builder.rb +15 -3
- data/lib/azeroth/resourceable/class_methods.rb +15 -2
- data/lib/azeroth/resourceable/endpoints_builder.rb +104 -0
- data/lib/azeroth/resourceable/{builder.rb → resources_builder.rb} +5 -17
- data/lib/azeroth/resourceable.rb +17 -2
- data/lib/azeroth/version.rb +1 -1
- data/spec/lib/azeroth/params_builder_spec.rb +49 -33
- data/spec/lib/azeroth/resource_builder_spec.rb +41 -17
- data/spec/lib/azeroth/resourceable/endpoints_builder_spec.rb +90 -0
- data/spec/lib/azeroth/resourceable/resources_builder_spec.rb +32 -0
- data/spec/lib/azeroth/resourceable_spec.rb +37 -37
- data/spec/support/app/controllers/params_builder_controller.rb +2 -2
- metadata +6 -4
- data/spec/lib/azeroth/resourceable/builder_spec.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2bb67d32bc51b4aa26e666ec6d4f942dba16b454d07cf65cb5ebff1f052766ae
|
4
|
+
data.tar.gz: b5d68c84445bde028dcc0038cfe075947de6d45ffb31afb5f7bec088de680a09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2604e2557de1bf029e1fbb0f32d16957e150a302c80b4db0c95e70dc2c7809f4883365b0e7f5756dfb8348aafd304f7262087ca80f95fa3f24e626a4dafe2b0
|
7
|
+
data.tar.gz: a54c254012d8de5f00c799a5cc9fa3e65467d8c624e3ad1d8eff8adf892f3bd7ec6b80134abd61766e0cadf37719a56d607b9d241af7c038fb56591b93255e94
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -11,7 +11,7 @@ Azeroth
|
|
11
11
|
|
12
12
|
Yard Documentation
|
13
13
|
-------------------
|
14
|
-
[https://www.rubydoc.info/gems/azeroth/2.
|
14
|
+
[https://www.rubydoc.info/gems/azeroth/2.1.0](https://www.rubydoc.info/gems/azeroth/2.1.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
|
@@ -25,9 +25,9 @@ does not perform database operations
|
|
25
25
|
Future versions will enable `html` rendering to also perform
|
26
26
|
database operations.
|
27
27
|
|
28
|
-
Current Release: [2.
|
28
|
+
Current Release: [2.1.0](https://github.com/darthjee/azeroth/tree/2.1.0)
|
29
29
|
|
30
|
-
[Next release](https://github.com/darthjee/azeroth/compare/2.
|
30
|
+
[Next release](https://github.com/darthjee/azeroth/compare/2.1.0...master)
|
31
31
|
|
32
32
|
Installation
|
33
33
|
---------------
|
@@ -68,6 +68,8 @@ It accepts options
|
|
68
68
|
- update_with: Method/Block to be ran when updating the reource on update
|
69
69
|
- paginated: Flag when pagination should be applied
|
70
70
|
- per_page: Number of items returned when pagination is active
|
71
|
+
- id_key: key used to find a model. id by default
|
72
|
+
- param_key: parameter key used to find the model
|
71
73
|
|
72
74
|
```ruby
|
73
75
|
# publishers_controller.rb
|
data/lib/azeroth/decorator.rb
CHANGED
@@ -94,8 +94,6 @@ module Azeroth
|
|
94
94
|
# When object is an iterator, decoration is applied to each
|
95
95
|
# and an array is returned
|
96
96
|
#
|
97
|
-
# @param args [Hash] options (to be implemented)
|
98
|
-
#
|
99
97
|
# @return [Hash]
|
100
98
|
def as_json(*)
|
101
99
|
return nil if object.nil?
|
@@ -146,8 +144,6 @@ module Azeroth
|
|
146
144
|
#
|
147
145
|
# @param method_name [Symbol] name of the method
|
148
146
|
# called
|
149
|
-
# @param args [Array<Object>] arguments of the
|
150
|
-
# method called
|
151
147
|
#
|
152
148
|
# @return [Object]
|
153
149
|
def method_missing(method_name, *)
|
data/lib/azeroth/options.rb
CHANGED
@@ -17,6 +17,8 @@ module Azeroth
|
|
17
17
|
DEFAULT_OPTIONS = {
|
18
18
|
only: %i[create destroy edit index new show update],
|
19
19
|
except: [],
|
20
|
+
id_key: :id,
|
21
|
+
param_key: :id,
|
20
22
|
decorator: true,
|
21
23
|
before_save: nil,
|
22
24
|
after_save: nil,
|
@@ -65,6 +67,20 @@ module Azeroth
|
|
65
67
|
#
|
66
68
|
# @return [Array<String,Symbol>]
|
67
69
|
|
70
|
+
# @method id_key
|
71
|
+
# @api private
|
72
|
+
#
|
73
|
+
# key used to find a model. id by default
|
74
|
+
#
|
75
|
+
# @return [Symbol]
|
76
|
+
|
77
|
+
# @method param_key
|
78
|
+
# @api private
|
79
|
+
#
|
80
|
+
# parameter key used to find the model
|
81
|
+
#
|
82
|
+
# @return [Symbol]
|
83
|
+
|
68
84
|
# @method decorator
|
69
85
|
# @api private
|
70
86
|
#
|
@@ -6,14 +6,15 @@ module Azeroth
|
|
6
6
|
#
|
7
7
|
# Class responsible for adding params handling methods to a controller
|
8
8
|
class ParamsBuilder < Sinclair::Model
|
9
|
-
# @!method initialize(model:, builder:)
|
9
|
+
# @!method initialize(model:, builder:, options:)
|
10
10
|
# @api private
|
11
11
|
#
|
12
12
|
# @param model [Model] Resource interface
|
13
13
|
# @param builder [Sinclair] Methods builder
|
14
|
+
# @param options [Azeroth::Options] options
|
14
15
|
#
|
15
16
|
# @return [ParamsBuilder]
|
16
|
-
initialize_with(:model, :builder, writter: false)
|
17
|
+
initialize_with(:model, :builder, :options, writter: false)
|
17
18
|
|
18
19
|
# Append the params methods to be built
|
19
20
|
#
|
@@ -22,7 +23,8 @@ module Azeroth
|
|
22
23
|
method_name = name
|
23
24
|
allowed_attributes = permitted_attributes.map(&:to_sym)
|
24
25
|
|
25
|
-
|
26
|
+
param_key = options.param_key
|
27
|
+
add_method("#{name}_id") { params.require(param_key) }
|
26
28
|
add_method("#{name}_params") do
|
27
29
|
params.require(method_name)
|
28
30
|
.permit(*allowed_attributes)
|
@@ -47,6 +49,14 @@ module Azeroth
|
|
47
49
|
#
|
48
50
|
# @return [Sinclair]
|
49
51
|
|
52
|
+
# @method options
|
53
|
+
# @api private
|
54
|
+
# @private
|
55
|
+
#
|
56
|
+
# Methods builder
|
57
|
+
#
|
58
|
+
# @return [Azeroth::Options]
|
59
|
+
|
50
60
|
delegate :add_method, to: :builder
|
51
61
|
# @method add_method
|
52
62
|
# @api private
|
@@ -10,14 +10,15 @@ module Azeroth
|
|
10
10
|
# entries of a resource, and one for fetching an specific
|
11
11
|
# entry
|
12
12
|
class ResourceBuilder < Sinclair::Model
|
13
|
-
# @!method initialize(model:, builder:)
|
13
|
+
# @!method initialize(model:, builder:, options:)
|
14
14
|
# @api private
|
15
15
|
#
|
16
16
|
# @param model [Model] Resource model interface
|
17
17
|
# @param builder [Sinclair] method builder
|
18
|
+
# @param options [Azeroth::Options] options
|
18
19
|
#
|
19
20
|
# @return [ResourceBuilder]
|
20
|
-
initialize_with(:model, :builder, writter: false)
|
21
|
+
initialize_with(:model, :builder, :options, writter: false)
|
21
22
|
|
22
23
|
# Append methods to be built to the builder
|
23
24
|
#
|
@@ -27,7 +28,10 @@ module Azeroth
|
|
27
28
|
# @return [Array<Sinclair::MethodDefinition>]
|
28
29
|
def append
|
29
30
|
add_method(plural, "@#{plural} ||= #{model.klass}.all")
|
30
|
-
add_method(
|
31
|
+
add_method(
|
32
|
+
name,
|
33
|
+
"@#{name} ||= #{plural}.find_by!(#{id_key}: #{name}_id)"
|
34
|
+
)
|
31
35
|
end
|
32
36
|
|
33
37
|
# @method model
|
@@ -70,5 +74,13 @@ module Azeroth
|
|
70
74
|
# Return the pluralized version of resource name
|
71
75
|
#
|
72
76
|
# @return [String]
|
77
|
+
|
78
|
+
delegate :id_key, to: :options
|
79
|
+
# @method plural
|
80
|
+
# @api private
|
81
|
+
#
|
82
|
+
# key used to find a model. id by default
|
83
|
+
#
|
84
|
+
# @return [Symbol]
|
73
85
|
end
|
74
86
|
end
|
@@ -7,7 +7,7 @@ module Azeroth
|
|
7
7
|
#
|
8
8
|
# Class methods added by {Resourceable}
|
9
9
|
module ClassMethods
|
10
|
-
# Adds resource methods for resource
|
10
|
+
# Adds resource and routes methods for resource
|
11
11
|
#
|
12
12
|
# @param (see Resourceable.resource_for)
|
13
13
|
# @option (see Resourceable.resource_for)
|
@@ -16,10 +16,23 @@ module Azeroth
|
|
16
16
|
# @see (see Resourceable.resource_for)
|
17
17
|
# @example (see Resourceable.resource_for)
|
18
18
|
def resource_for(name, **options)
|
19
|
-
|
19
|
+
EndpointsBuilder.new(
|
20
20
|
self, name, Azeroth::Options.new(options)
|
21
21
|
).build
|
22
22
|
end
|
23
|
+
|
24
|
+
# Adds resource methods for resource
|
25
|
+
#
|
26
|
+
# @param (see Resourceable.model_for)
|
27
|
+
# @return (see Resourceable.model_for)
|
28
|
+
#
|
29
|
+
# @see (see Resourceable.model_for)
|
30
|
+
# @example (see Resourceable.model_for)
|
31
|
+
def model_for(name)
|
32
|
+
ResourcesBuilder.new(
|
33
|
+
self, name, Azeroth::Options.new({})
|
34
|
+
).build
|
35
|
+
end
|
23
36
|
end
|
24
37
|
end
|
25
38
|
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sinclair'
|
4
|
+
|
5
|
+
module Azeroth
|
6
|
+
module Resourceable
|
7
|
+
# @api private
|
8
|
+
# Builder responsible to add all methods to the controller
|
9
|
+
#
|
10
|
+
# Builder uses other builders to put together it's methods
|
11
|
+
#
|
12
|
+
# @see ResourceBuilder
|
13
|
+
# @see ResourceRouteBuilder
|
14
|
+
# @see RoutesBuilder
|
15
|
+
class EndpointsBuilder
|
16
|
+
# @param klass [ActionController::Base] Controller to
|
17
|
+
# to be changed
|
18
|
+
# @param model_name [Symbol,String]
|
19
|
+
# @param options [Options]
|
20
|
+
def initialize(klass, model_name, options)
|
21
|
+
@klass = klass
|
22
|
+
@options = options
|
23
|
+
@model = Azeroth::Model.new(model_name, options)
|
24
|
+
|
25
|
+
add_routes
|
26
|
+
ResourcesBuilder.new(klass, model_name, options).build
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :klass, :model, :options
|
32
|
+
|
33
|
+
# @method klass
|
34
|
+
# @api private
|
35
|
+
# @private
|
36
|
+
#
|
37
|
+
# Controller to changed
|
38
|
+
#
|
39
|
+
# @return [ActionController::Base]
|
40
|
+
|
41
|
+
# @method model
|
42
|
+
# @api private
|
43
|
+
# @private
|
44
|
+
#
|
45
|
+
# Model interface to resource model
|
46
|
+
#
|
47
|
+
# @return [Model]
|
48
|
+
|
49
|
+
# @method options
|
50
|
+
# @api private
|
51
|
+
# @private
|
52
|
+
#
|
53
|
+
# Options
|
54
|
+
#
|
55
|
+
# @return [Options]
|
56
|
+
|
57
|
+
delegate :build, :add_method, to: :builder
|
58
|
+
# @method build
|
59
|
+
# @api private
|
60
|
+
# @private
|
61
|
+
#
|
62
|
+
# build all the methods
|
63
|
+
#
|
64
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
65
|
+
|
66
|
+
# @method add_method
|
67
|
+
# @api private
|
68
|
+
#
|
69
|
+
# Add method to be built
|
70
|
+
#
|
71
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
72
|
+
|
73
|
+
delegate :name, to: :model
|
74
|
+
# @method name
|
75
|
+
# @api private
|
76
|
+
# @private
|
77
|
+
#
|
78
|
+
# Resource name
|
79
|
+
#
|
80
|
+
# @return [Symbol,String]
|
81
|
+
|
82
|
+
# @api private
|
83
|
+
# @private
|
84
|
+
#
|
85
|
+
# Returns a method builder
|
86
|
+
#
|
87
|
+
# @return [Sinclair]
|
88
|
+
#
|
89
|
+
# @see https://www.rubydoc.info/gems/sinclair Sinclair
|
90
|
+
def builder
|
91
|
+
@builder ||= Sinclair.new(klass)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Add metohods for each route
|
95
|
+
#
|
96
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
97
|
+
def add_routes
|
98
|
+
RoutesBuilder.new(
|
99
|
+
model: model, builder: builder, options: options
|
100
|
+
).append
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -5,14 +5,12 @@ require 'sinclair'
|
|
5
5
|
module Azeroth
|
6
6
|
module Resourceable
|
7
7
|
# @api private
|
8
|
-
# Builder responsible to add
|
8
|
+
# Builder responsible to add resource / model methods to the controller
|
9
9
|
#
|
10
|
-
# Builder uses
|
10
|
+
# Builder uses {ResourceBuilder} to put together it's methods
|
11
11
|
#
|
12
12
|
# @see ResourceBuilder
|
13
|
-
|
14
|
-
# @see RoutesBuilder
|
15
|
-
class Builder
|
13
|
+
class ResourcesBuilder
|
16
14
|
# @param klass [ActionController::Base] Controller to
|
17
15
|
# to be changed
|
18
16
|
# @param model_name [Symbol,String]
|
@@ -24,7 +22,6 @@ module Azeroth
|
|
24
22
|
|
25
23
|
add_params
|
26
24
|
add_resource
|
27
|
-
add_routes
|
28
25
|
add_helpers
|
29
26
|
end
|
30
27
|
|
@@ -98,7 +95,7 @@ module Azeroth
|
|
98
95
|
# @return [Array<Sinclair::MethodDefinition>]
|
99
96
|
def add_params
|
100
97
|
ParamsBuilder.new(
|
101
|
-
model: model, builder: builder
|
98
|
+
model: model, builder: builder, options: options
|
102
99
|
).append
|
103
100
|
end
|
104
101
|
|
@@ -106,16 +103,7 @@ module Azeroth
|
|
106
103
|
#
|
107
104
|
# @return [Array<Sinclair::MethodDefinition>]
|
108
105
|
def add_resource
|
109
|
-
ResourceBuilder.new(model: model, builder: builder).append
|
110
|
-
end
|
111
|
-
|
112
|
-
# Add metohods for each route
|
113
|
-
#
|
114
|
-
# @return [Array<Sinclair::MethodDefinition>]
|
115
|
-
def add_routes
|
116
|
-
RoutesBuilder.new(
|
117
|
-
model: model, builder: builder, options: options
|
118
|
-
).append
|
106
|
+
ResourceBuilder.new(model: model, builder: builder, options: options).append
|
119
107
|
end
|
120
108
|
|
121
109
|
# Add helpers to render objects on template
|
data/lib/azeroth/resourceable.rb
CHANGED
@@ -16,13 +16,16 @@ module Azeroth
|
|
16
16
|
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
17
17
|
end
|
18
18
|
|
19
|
-
autoload :
|
20
|
-
autoload :
|
19
|
+
autoload :ClassMethods, 'azeroth/resourceable/class_methods'
|
20
|
+
autoload :EndpointsBuilder, 'azeroth/resourceable/endpoints_builder'
|
21
|
+
autoload :ResourcesBuilder, 'azeroth/resourceable/resources_builder'
|
21
22
|
|
22
23
|
class << self
|
23
24
|
# @method self.resource_for(name, **options)
|
24
25
|
# @api public
|
25
26
|
#
|
27
|
+
# Adds resource and routes methods for resource
|
28
|
+
#
|
26
29
|
# @param name [String, Symbol] Name of the resource
|
27
30
|
# @param options [Hash] resource building options
|
28
31
|
# @option options only [Array<Symbol,String>,Symbol,String] List of
|
@@ -45,6 +48,8 @@ module Azeroth
|
|
45
48
|
# endpoint should be paginated
|
46
49
|
# @option options per_page [Integer] number of entries returned per
|
47
50
|
# page on index
|
51
|
+
# @option options id_key [Symbol] key used to find a model. id by default
|
52
|
+
# @option options param_key [Symbol] parameter key used to find the model
|
48
53
|
#
|
49
54
|
# @return [Array<MethodDefinition>] list of methods created
|
50
55
|
#
|
@@ -209,6 +214,16 @@ module Azeroth
|
|
209
214
|
# 'per_page' => 20,
|
210
215
|
# 'page' => 2
|
211
216
|
# }
|
217
|
+
|
218
|
+
# @method self.model_for(name, **options)
|
219
|
+
# @api public
|
220
|
+
#
|
221
|
+
# Adds resource and routes methods for resource
|
222
|
+
#
|
223
|
+
# @param name [String, Symbol] Name of the resource
|
224
|
+
# @param options [Hash] resource building options
|
225
|
+
# @option options id_key [Symbol] key used to find a model. id by default
|
226
|
+
# @option options param_key [Symbol] parameter key used to find the model
|
212
227
|
end
|
213
228
|
|
214
229
|
private
|
data/lib/azeroth/version.rb
CHANGED
@@ -4,55 +4,71 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Azeroth::ParamsBuilder do
|
6
6
|
subject(:params_builder) do
|
7
|
-
described_class.new(model: model, builder: builder)
|
7
|
+
described_class.new(model: model, builder: builder, options: options)
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:model)
|
11
|
-
let(:options)
|
12
|
-
let(:
|
13
|
-
let(:
|
10
|
+
let(:model) { Azeroth::Model.new(:document, options) }
|
11
|
+
let(:options) { Azeroth::Options.new(options_hash) }
|
12
|
+
let(:options_hash) { {} }
|
13
|
+
let(:builder) { Sinclair.new(klass) }
|
14
|
+
let(:klass) { Class.new(ParamsBuilderController) }
|
15
|
+
let(:controller) { klass.new(id, attributes) }
|
16
|
+
let(:attributes) { document.attributes }
|
17
|
+
let(:document) { create(:document) }
|
18
|
+
let(:id) { Random.rand(10..100) }
|
14
19
|
|
15
20
|
before do
|
16
21
|
params_builder.append
|
17
22
|
end
|
18
23
|
|
19
24
|
describe '#append' do
|
20
|
-
|
21
|
-
|
22
|
-
.
|
23
|
-
|
24
|
-
|
25
|
-
it 'adds params method' do
|
26
|
-
expect { builder.build }
|
27
|
-
.to add_method(:document_params).to(klass)
|
28
|
-
end
|
25
|
+
context 'when no options are given' do
|
26
|
+
it 'adds id method' do
|
27
|
+
expect { builder.build }
|
28
|
+
.to add_method(:document_id).to(klass)
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
let(:attributes) { document.attributes }
|
34
|
-
let(:id) { Random.rand(10..100) }
|
35
|
-
let(:expected_attributes) do
|
36
|
-
{
|
37
|
-
'name' => document.name,
|
38
|
-
'reference' => document.reference
|
39
|
-
}
|
31
|
+
it 'adds params method' do
|
32
|
+
expect { builder.build }
|
33
|
+
.to add_method(:document_params).to(klass)
|
40
34
|
end
|
41
35
|
|
42
|
-
|
36
|
+
describe 'after the build' do
|
37
|
+
let(:expected_attributes) do
|
38
|
+
{
|
39
|
+
'name' => document.name,
|
40
|
+
'reference' => document.reference
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
before { builder.build }
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
context 'when requesting id' do
|
47
|
+
it 'returns id from request path' do
|
48
|
+
expect(controller.document_id).to eq(id)
|
49
|
+
end
|
47
50
|
end
|
48
|
-
end
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
context 'when requesting params' do
|
53
|
+
it 'returns payload' do
|
54
|
+
expect(controller.document_params.to_h)
|
55
|
+
.to eq(expected_attributes)
|
56
|
+
end
|
54
57
|
end
|
55
58
|
end
|
56
59
|
end
|
57
60
|
end
|
61
|
+
|
62
|
+
context 'when no options are given' do
|
63
|
+
let(:options_hash) { { param_key: :document_id } }
|
64
|
+
let(:controller) { klass.new(id, attributes, param_id: :document_id) }
|
65
|
+
|
66
|
+
before { builder.build }
|
67
|
+
|
68
|
+
context 'when requesting id' do
|
69
|
+
it 'returns id from request path' do
|
70
|
+
expect(controller.document_id).to eq(id)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
58
74
|
end
|
@@ -4,13 +4,14 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Azeroth::ResourceBuilder do
|
6
6
|
subject(:resource_builder) do
|
7
|
-
described_class.new(model: model, builder: builder)
|
7
|
+
described_class.new(model: model, builder: builder, options: options)
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:model)
|
11
|
-
let(:options)
|
12
|
-
let(:
|
13
|
-
let(:
|
10
|
+
let(:model) { Azeroth::Model.new(:document, options) }
|
11
|
+
let(:options) { Azeroth::Options.new(options_hash) }
|
12
|
+
let(:options_hash) { {} }
|
13
|
+
let(:builder) { Sinclair.new(klass) }
|
14
|
+
let(:klass) { Class.new(ResourceBuilderController) }
|
14
15
|
|
15
16
|
before do
|
16
17
|
resource_builder.append
|
@@ -18,21 +19,44 @@ describe Azeroth::ResourceBuilder do
|
|
18
19
|
end
|
19
20
|
|
20
21
|
describe '#append' do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
context 'when no option is given' do
|
23
|
+
it 'adds the listing method' do
|
24
|
+
expect { builder.build }
|
25
|
+
.to change { klass.new.respond_to?(:documents) }
|
26
|
+
.to(true)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'adds the fetching method' do
|
30
|
+
expect { builder.build }
|
31
|
+
.to change { klass.new.respond_to?(:document) }
|
32
|
+
.to(true)
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'after the build' do
|
36
|
+
let(:controller) { klass.new(document_id: document.id) }
|
37
|
+
let(:document) { create(:document) }
|
38
|
+
|
39
|
+
before { builder.build }
|
26
40
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
41
|
+
context 'when requesting the list of documents' do
|
42
|
+
it 'returns the list of documents' do
|
43
|
+
expect(controller.documents).to eq(Document.all)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when requesting one document' do
|
48
|
+
it 'returns the requested document' do
|
49
|
+
expect(controller.document).to eq(document)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
31
53
|
end
|
32
54
|
|
33
|
-
|
34
|
-
let(:
|
35
|
-
|
55
|
+
context 'when id_key option is given' do
|
56
|
+
let(:options_hash) { { id_key: :reference } }
|
57
|
+
|
58
|
+
let(:controller) { klass.new(document_id: document.reference) }
|
59
|
+
let(:document) { create(:document, reference: SecureRandom.hex(20)) }
|
36
60
|
|
37
61
|
before { builder.build }
|
38
62
|
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Azeroth::Resourceable::EndpointsBuilder do
|
6
|
+
subject(:builder) { described_class.new(klass, :document, options) }
|
7
|
+
|
8
|
+
let(:options) { Azeroth::Options.new(options_hash) }
|
9
|
+
let(:options_hash) { {} }
|
10
|
+
|
11
|
+
let(:klass) do
|
12
|
+
Class.new(Controller) do
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#build' do
|
17
|
+
let(:available_routes) { %i[index show new destroy create update] }
|
18
|
+
let(:expected_params_methods) { %i[document_id document_params] }
|
19
|
+
let(:expected_routes_methods) { available_routes }
|
20
|
+
let(:expected_resource_methods) { %i[document documents] }
|
21
|
+
|
22
|
+
context 'when no option is given' do
|
23
|
+
it 'adds params methods' do
|
24
|
+
expect { builder.build }
|
25
|
+
.to change { (klass.instance_methods & expected_params_methods).sort }
|
26
|
+
.from([]).to(expected_params_methods.sort)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'adds routes methods' do
|
30
|
+
expect { builder.build }
|
31
|
+
.to change { (klass.instance_methods & available_routes).sort }
|
32
|
+
.from([]).to(expected_routes_methods.sort)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'adds resource methods' do
|
36
|
+
expect { builder.build }
|
37
|
+
.to change { (klass.instance_methods & expected_resource_methods).sort }
|
38
|
+
.from([]).to(expected_resource_methods.sort)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when only option is given for a route' do
|
43
|
+
let(:options_hash) { { only: route } }
|
44
|
+
let(:route) { available_routes.sample }
|
45
|
+
let(:expected_routes_methods) { [route] }
|
46
|
+
|
47
|
+
it 'adds routes methods' do
|
48
|
+
expect { builder.build }
|
49
|
+
.to change { (klass.instance_methods & available_routes).sort }
|
50
|
+
.from([]).to(expected_routes_methods.sort)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when only option as array is given for a route' do
|
55
|
+
let(:options_hash) { { only: routes } }
|
56
|
+
let(:routes) { [available_routes.sample, available_routes.sample].uniq }
|
57
|
+
let(:expected_routes_methods) { routes }
|
58
|
+
|
59
|
+
it 'adds routes methods' do
|
60
|
+
expect { builder.build }
|
61
|
+
.to change { (klass.instance_methods & available_routes).sort }
|
62
|
+
.from([]).to(expected_routes_methods.sort)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when except option is given for a route' do
|
67
|
+
let(:options_hash) { { except: route } }
|
68
|
+
let(:route) { available_routes.sample }
|
69
|
+
let(:expected_routes_methods) { available_routes - [route] }
|
70
|
+
|
71
|
+
it 'adds routes methods' do
|
72
|
+
expect { builder.build }
|
73
|
+
.to change { (klass.instance_methods & available_routes).sort }
|
74
|
+
.from([]).to(expected_routes_methods.sort)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when except option as array is given for a route' do
|
79
|
+
let(:options_hash) { { except: routes } }
|
80
|
+
let(:routes) { [available_routes.sample, available_routes.sample].uniq }
|
81
|
+
let(:expected_routes_methods) { available_routes - routes }
|
82
|
+
|
83
|
+
it 'adds routes methods' do
|
84
|
+
expect { builder.build }
|
85
|
+
.to change { (klass.instance_methods & available_routes).sort }
|
86
|
+
.from([]).to(expected_routes_methods.sort)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Azeroth::Resourceable::ResourcesBuilder do
|
6
|
+
subject(:builder) { described_class.new(klass, :document, options) }
|
7
|
+
|
8
|
+
let(:options) { Azeroth::Options.new(options_hash) }
|
9
|
+
let(:options_hash) { {} }
|
10
|
+
|
11
|
+
let(:klass) do
|
12
|
+
Class.new(Controller) do
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#build' do
|
17
|
+
let(:expected_resource_methods) { %i[document documents] }
|
18
|
+
let(:expected_params_methods) { %i[document_id document_params] }
|
19
|
+
|
20
|
+
it 'adds params methods' do
|
21
|
+
expect { builder.build }
|
22
|
+
.to change { (klass.instance_methods & expected_params_methods).sort }
|
23
|
+
.from([]).to(expected_params_methods.sort)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'adds resource methods' do
|
27
|
+
expect { builder.build }
|
28
|
+
.to change { (klass.instance_methods & expected_resource_methods).sort }
|
29
|
+
.from([]).to(expected_resource_methods.sort)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -10,54 +10,41 @@ describe Azeroth::Resourceable do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
describe '.resource_for' do
|
13
|
-
let(:
|
14
|
-
let(:
|
15
|
-
let(:
|
16
|
-
let(:
|
17
|
-
let(:
|
13
|
+
let(:available_routes) { %i[index show new destroy create update] }
|
14
|
+
let(:params) { { id: model.id, format: :json } }
|
15
|
+
let(:model_name) { :document }
|
16
|
+
let(:model) { create(model_name) }
|
17
|
+
let(:controller) { controller_class.new(params) }
|
18
|
+
let(:decorator) { Document::Decorator }
|
18
19
|
|
19
20
|
context 'when no special option is given' do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
21
|
+
it 'add routes methods' do
|
22
|
+
expect { controller_class.resource_for(model_name) }
|
23
|
+
.to change { (controller.methods & available_routes).sort }
|
24
|
+
.from([]).to(available_routes.sort)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context 'when passing the only option' do
|
29
|
-
let(:options) { { only:
|
30
|
-
|
31
|
-
%i[index show].each do |method_name|
|
32
|
-
it do
|
33
|
-
expect { controller_class.resource_for(model_name, **options) }
|
34
|
-
.to add_method(method_name).to(controller_class)
|
35
|
-
end
|
36
|
-
end
|
29
|
+
let(:options) { { only: expected_routes_methods } }
|
30
|
+
let(:expected_routes_methods) { %i[index show] }
|
37
31
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
32
|
+
it 'add routes methods' do
|
33
|
+
expect { controller_class.resource_for(model_name, **options) }
|
34
|
+
.to change { (controller.methods & available_routes).sort }
|
35
|
+
.from([]).to(expected_routes_methods.sort)
|
43
36
|
end
|
44
37
|
end
|
45
38
|
|
46
39
|
context 'when passing the except option' do
|
47
|
-
let(:options) { { except:
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
%i[new edit update destroy].each do |method_name|
|
57
|
-
it do
|
58
|
-
expect { controller_class.resource_for(model_name, **options) }
|
59
|
-
.to add_method(method_name).to(controller_class)
|
60
|
-
end
|
40
|
+
let(:options) { { except: skipped_routes } }
|
41
|
+
let(:skipped_routes) { %i[index show] }
|
42
|
+
let(:expected_routes_methods) { available_routes - skipped_routes }
|
43
|
+
|
44
|
+
it 'add routes methods' do
|
45
|
+
expect { controller_class.resource_for(model_name, **options) }
|
46
|
+
.to change { (controller.methods & available_routes).sort }
|
47
|
+
.from([]).to(expected_routes_methods.sort)
|
61
48
|
end
|
62
49
|
end
|
63
50
|
|
@@ -105,4 +92,17 @@ describe Azeroth::Resourceable do
|
|
105
92
|
end
|
106
93
|
end
|
107
94
|
end
|
95
|
+
|
96
|
+
describe '.model_for' do
|
97
|
+
let(:model_name) { :document }
|
98
|
+
let(:model) { create(model_name) }
|
99
|
+
let(:controller) { controller_class.new({}) }
|
100
|
+
let(:expected_resource_methods) { %i[document documents] }
|
101
|
+
|
102
|
+
it 'adds resource methods' do
|
103
|
+
expect { controller_class.model_for(model_name) }
|
104
|
+
.to change { (controller.methods & expected_resource_methods).sort }
|
105
|
+
.from([]).to(expected_resource_methods.sort)
|
106
|
+
end
|
107
|
+
end
|
108
108
|
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: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-05-
|
11
|
+
date: 2025-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -115,8 +115,9 @@ files:
|
|
115
115
|
- lib/azeroth/request_handler/update.rb
|
116
116
|
- lib/azeroth/resource_builder.rb
|
117
117
|
- lib/azeroth/resourceable.rb
|
118
|
-
- lib/azeroth/resourceable/builder.rb
|
119
118
|
- lib/azeroth/resourceable/class_methods.rb
|
119
|
+
- lib/azeroth/resourceable/endpoints_builder.rb
|
120
|
+
- lib/azeroth/resourceable/resources_builder.rb
|
120
121
|
- lib/azeroth/routes_builder.rb
|
121
122
|
- lib/azeroth/version.rb
|
122
123
|
- spec/controllers/documents_controller_spec.rb
|
@@ -253,7 +254,8 @@ files:
|
|
253
254
|
- spec/lib/azeroth/request_handler/update_spec.rb
|
254
255
|
- spec/lib/azeroth/request_handler_spec.rb
|
255
256
|
- spec/lib/azeroth/resource_builder_spec.rb
|
256
|
-
- spec/lib/azeroth/resourceable/
|
257
|
+
- spec/lib/azeroth/resourceable/endpoints_builder_spec.rb
|
258
|
+
- spec/lib/azeroth/resourceable/resources_builder_spec.rb
|
257
259
|
- spec/lib/azeroth/resourceable_spec.rb
|
258
260
|
- spec/lib/azeroth/routes_builder_spec.rb
|
259
261
|
- spec/spec_helper.rb
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Azeroth::Resourceable::Builder do
|
6
|
-
subject { described_class.new(klass, :document, options) }
|
7
|
-
|
8
|
-
let(:options) { Azeroth::Options.new(options_hash) }
|
9
|
-
let(:options_hash) { {} }
|
10
|
-
|
11
|
-
let(:klass) do
|
12
|
-
Class.new(Controller) do
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe '#build' do
|
17
|
-
it 'adds spec'
|
18
|
-
end
|
19
|
-
end
|