azeroth 2.0.0 → 2.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65ba326980d7b37bb878cf1f230dea518561a68fdc337e53b9ca6391766957f3
4
- data.tar.gz: 6f39fcee3056ad4eccbc72d1fc268460144bf3ec442cc4292f31a3bbf98f77b4
3
+ metadata.gz: bffbd5680be44e59335195e606064c1c4861c30eebdf5a94063b97b6831b564d
4
+ data.tar.gz: 112ffcf30bfcd30f58ff24c8bd8b28a29d82e3708335f462192b3d8b1bc20854
5
5
  SHA512:
6
- metadata.gz: 10907b167920b813c0de6aa9a77dad7366cf8a16179a4179c6343ab4544d9b86560ed812a2d897aaf702d4d556b3d77d45de2a03b00a47e34368488230ef2cf7
7
- data.tar.gz: 805c0ea2d52a06e1c2472ba390d973fcf0b06e3ae16fcf03cde5cae97a70b0b25072aff1f7295a42cbcfd4c228a62e32481e126bbc9191394f2031d1df5b7482
6
+ metadata.gz: 44dacff749a10df31d63b6a51f2f9769030ff3b9addceb537839db3c98dac51885cbcea229992ebcb68e024dd1012ebe9a3d94f158ba587e562802e26c98a570
7
+ data.tar.gz: ae2263e16bc69ed18bf8690c4e938b4622dab9e645d20458cee4d6afb2529531726c54e582249d265d79fc551d51af096e9b08b41fb304d3578227c66eba4492
data/.gitignore CHANGED
@@ -12,3 +12,5 @@ spec/dummy/log/*
12
12
  spec/dummy/tmp/*
13
13
  spec/dummy/db/*.sqlite3
14
14
  spec/dummy/db/*.sqlite3-journal
15
+ spec/dummy/db/*.sqlite3-shm
16
+ spec/dummy/db/*.sqlite3-wal
data/.rubocop.yml CHANGED
@@ -13,7 +13,7 @@ Metrics/BlockLength:
13
13
  - 'azeroth.gemspec'
14
14
 
15
15
  RSpec/NestedGroups:
16
- Max: 4
16
+ Max: 5
17
17
 
18
18
  RSpec/AlignLeftLetBrace:
19
19
  Enabled: true
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.0.0](https://www.rubydoc.info/gems/azeroth/2.0.0)
14
+ [https://www.rubydoc.info/gems/azeroth/2.1.1](https://www.rubydoc.info/gems/azeroth/2.1.1)
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.0.0](https://github.com/darthjee/azeroth/tree/2.0.0)
28
+ Current Release: [2.1.1](https://github.com/darthjee/azeroth/tree/2.1.1)
29
29
 
30
- [Next release](https://github.com/darthjee/azeroth/compare/2.0.0...master)
30
+ [Next release](https://github.com/darthjee/azeroth/compare/2.1.1...master)
31
31
 
32
32
  Installation
33
33
  ---------------
@@ -54,10 +54,14 @@ Usage
54
54
  ## Azeroth::Resourceable
55
55
 
56
56
  [Resourceable](https://www.rubydoc.info/gems/azeroth/Azeroth/Resourceable)
57
- module adds class method [resource_for](https://www.rubydoc.info/gems/azeroth/Azeroth/Resourceable/ClassMethods#resource_for-instance_method)
57
+ module adds class methods
58
+ [resource_for](https://www.rubydoc.info/gems/azeroth/Azeroth/Resourceable/ClassMethods#resource_for-instance_method)
58
59
  which adds a resource and action methods for `create`, `show`, `index`,
59
60
  `update`, `delete`, `edit`
61
+ and
62
+ [model_for](https://www.rubydoc.info/gems/azeroth/Azeroth/Resourceable/ClassMethods#model_for-instance_method)
60
63
 
64
+ ### #resource_for
61
65
  It accepts options
62
66
  - only: List of actions to be built
63
67
  - except: List of actions to not to be built
@@ -68,6 +72,8 @@ It accepts options
68
72
  - update_with: Method/Block to be ran when updating the reource on update
69
73
  - paginated: Flag when pagination should be applied
70
74
  - per_page: Number of items returned when pagination is active
75
+ - id_key: key used to find a model. id by default
76
+ - param_key: parameter key used to find the model
71
77
 
72
78
  ```ruby
73
79
  # publishers_controller.rb
@@ -165,6 +171,11 @@ It accepts options
165
171
  # }
166
172
  ```
167
173
 
174
+ ### #model_for
175
+ It accepts options
176
+ - id_key: key used to find a model. id by default
177
+ - param_key: parameter key used to find the model
178
+
168
179
  ## Azeroth::Decorator
169
180
 
170
181
  [Decorators](https://www.rubydoc.info/gems/azeroth/Azeroth/Decorator) are
@@ -75,8 +75,6 @@ module Azeroth
75
75
  #
76
76
  # @param method_name [Symbol] name of the method
77
77
  # called
78
- # @param args [Array<Object>] arguments of the
79
- # method called
80
78
  #
81
79
  # @return [Object]
82
80
  def method_missing(method_name, *)
@@ -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, *)
@@ -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
- add_method("#{name}_id") { params.require(:id) }
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(name, "@#{name} ||= #{plural}.find(#{name}_id)")
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,7 +16,20 @@ module Azeroth
16
16
  # @see (see Resourceable.resource_for)
17
17
  # @example (see Resourceable.resource_for)
18
18
  def resource_for(name, **options)
19
- Builder.new(
19
+ EndpointsBuilder.new(
20
+ self, name, Azeroth::Options.new(options)
21
+ ).build
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, **options)
32
+ ResourcesBuilder.new(
20
33
  self, name, Azeroth::Options.new(options)
21
34
  ).build
22
35
  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 all methods to the controller
8
+ # Builder responsible to add resource / model methods to the controller
9
9
  #
10
- # Builder uses other builders to put together it's methods
10
+ # Builder uses {ResourceBuilder} to put together it's methods
11
11
  #
12
12
  # @see ResourceBuilder
13
- # @see ResourceRouteBuilder
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
@@ -16,13 +16,16 @@ module Azeroth
16
16
  rescue_from ActiveRecord::RecordNotFound, with: :not_found
17
17
  end
18
18
 
19
- autoload :Builder, 'azeroth/resourceable/builder'
20
- autoload :ClassMethods, 'azeroth/resourceable/class_methods'
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Azeroth
4
- VERSION = '2.0.0'
4
+ VERSION = '2.1.1'
5
5
  end
@@ -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) { Azeroth::Model.new(:document, options) }
11
- let(:options) { Azeroth::Options.new }
12
- let(:builder) { Sinclair.new(klass) }
13
- let(:klass) { Class.new(ParamsBuilderController) }
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
- it 'adds id method' do
21
- expect { builder.build }
22
- .to add_method(:document_id).to(klass)
23
- end
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
- describe 'after the build' do
31
- let(:controller) { klass.new(id, attributes) }
32
- let(:document) { create(:document) }
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
- before { builder.build }
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
- context 'when requesting id' do
45
- it 'returns id from request path' do
46
- expect(controller.document_id).to eq(id)
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
- context 'when requesting params' do
51
- it 'returns payload' do
52
- expect(controller.document_params.to_h)
53
- .to eq(expected_attributes)
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) { Azeroth::Model.new(:document, options) }
11
- let(:options) { Azeroth::Options.new }
12
- let(:builder) { Sinclair.new(klass) }
13
- let(:klass) { Class.new(ResourceBuilderController) }
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
- it 'adds the listing method' do
22
- expect { builder.build }
23
- .to change { klass.new.respond_to?(:documents) }
24
- .to(true)
25
- end
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
- it 'adds the fetching method' do
28
- expect { builder.build }
29
- .to change { klass.new.respond_to?(:document) }
30
- .to(true)
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
- describe 'after the build' do
34
- let(:controller) { klass.new(document_id: document.id) }
35
- let(:document) { create(:document) }
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(:params) { { id: model.id, format: :json } }
14
- let(:model_name) { :document }
15
- let(:model) { create(model_name) }
16
- let(:controller) { controller_class.new(params) }
17
- let(:decorator) { Document::Decorator }
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
- %i[index show new edit update destroy].each do |method_name|
21
- it do
22
- expect { controller_class.resource_for(model_name) }
23
- .to add_method(method_name).to(controller_class)
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: %i[index show] } }
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
- %i[new edit update destroy].each do |method_name|
39
- it do
40
- expect { controller_class.resource_for(model_name, **options) }
41
- .not_to add_method(method_name).to(controller_class)
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: %i[index show] } }
48
-
49
- %i[index show].each do |method_name|
50
- it do
51
- expect { controller_class.resource_for(model_name, **options) }
52
- .not_to add_method(method_name).to(controller_class)
53
- end
54
- end
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
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ParamsBuilderController
4
- def initialize(id, attributes)
4
+ def initialize(id, attributes, param_id: :id)
5
5
  @params = ActionController::Parameters.new(
6
- id: id,
6
+ param_id => id,
7
7
  document: attributes
8
8
  )
9
9
  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.0.0
4
+ version: 2.1.1
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-09 00:00:00.000000000 Z
11
+ date: 2025-05-16 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/builder_spec.rb
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