azeroth 0.10.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +3 -3
- data/Dockerfile +6 -9
- data/README.md +31 -1
- data/azeroth.gemspec +12 -11
- data/config/yardstick.yml +1 -0
- data/docker-compose.yml +0 -8
- data/lib/azeroth/decorator/hash_builder.rb +3 -1
- data/lib/azeroth/decorator/key_value_extractor.rb +3 -8
- data/lib/azeroth/options.rb +24 -12
- data/lib/azeroth/params_builder.rb +83 -0
- data/lib/azeroth/request_handler/update.rb +3 -1
- data/lib/azeroth/request_handler.rb +1 -2
- data/lib/azeroth/resource_builder.rb +9 -10
- data/lib/azeroth/resourceable/builder.rb +16 -34
- data/lib/azeroth/resourceable/class_methods.rb +1 -130
- data/lib/azeroth/resourceable.rb +171 -4
- data/lib/azeroth/routes_builder.rb +8 -10
- data/lib/azeroth/version.rb +1 -1
- data/lib/azeroth.rb +1 -0
- data/spec/controllers/documents_with_error_controller_spec.rb +3 -1
- data/spec/dummy/app/models/movie/simple_decorator.rb +7 -0
- data/spec/dummy/app/models/movie.rb +5 -0
- data/spec/dummy/config/environments/development.rb +1 -0
- data/spec/dummy/db/schema.rb +4 -1
- data/spec/integration/yard/controllers/paginated_documents_controller_spec.rb +33 -0
- data/spec/lib/azeroth/decorator/key_value_extractor_spec.rb +3 -1
- data/spec/lib/azeroth/params_builder_spec.rb +58 -0
- data/spec/lib/azeroth/resource_builder_spec.rb +3 -1
- data/spec/lib/azeroth/resourceable_spec.rb +108 -0
- data/spec/lib/azeroth/routes_builder_spec.rb +3 -1
- data/spec/support/app/controllers/controller.rb +1 -1
- data/spec/support/app/controllers/params_builder_controller.rb +14 -0
- data/spec/support/factories/movie.rb +8 -0
- metadata +62 -34
- data/Dockerfile.circleci +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72a4e7c837a4053e850b9479d42cfccd2e279ac028f57da2c5d3baed6c6babfa
|
4
|
+
data.tar.gz: 84591492fc859555d87b86704a61e68c0b8171e42a8fd77d1f4e502216516fc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad6ca16b03b7b99b051bf9bfd25e25d1d522fa89e5b5ff31e495fa1af4fbb193dd269b062d8a87205aa8eeea84bd8a405b84de5d1663d7a1636239d4dd6b973f
|
7
|
+
data.tar.gz: 3eec0d1c24bb260dab5fb3ccccf5deedc8ae2eeff7378084fd501c82e740f776f1e3a14e32cee939155f5f833c4c1affdd3ca19f6636681f664be3deca36ed23
|
data/.circleci/config.yml
CHANGED
@@ -22,7 +22,7 @@ workflows:
|
|
22
22
|
jobs:
|
23
23
|
test:
|
24
24
|
docker:
|
25
|
-
- image: darthjee/circleci_rails_gems:1.
|
25
|
+
- image: darthjee/circleci_rails_gems:1.2.0
|
26
26
|
environment:
|
27
27
|
PROJECT: azeroth
|
28
28
|
steps:
|
@@ -41,7 +41,7 @@ jobs:
|
|
41
41
|
command: cc-test-reporter after-build --exit-code $?
|
42
42
|
checks:
|
43
43
|
docker:
|
44
|
-
- image: darthjee/circleci_rails_gems:1.
|
44
|
+
- image: darthjee/circleci_rails_gems:1.2.0
|
45
45
|
environment:
|
46
46
|
PROJECT: azeroth
|
47
47
|
steps:
|
@@ -66,7 +66,7 @@ jobs:
|
|
66
66
|
command: check_specs
|
67
67
|
build-and-release:
|
68
68
|
docker:
|
69
|
-
- image: darthjee/circleci_rails_gems:1.
|
69
|
+
- image: darthjee/circleci_rails_gems:1.2.0
|
70
70
|
environment:
|
71
71
|
PROJECT: azeroth
|
72
72
|
steps:
|
data/Dockerfile
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
FROM darthjee/
|
2
|
-
|
1
|
+
FROM darthjee/scripts:0.3.1 as scripts
|
2
|
+
|
3
|
+
FROM darthjee/rails_gems:1.2.0 as base
|
4
|
+
|
5
|
+
COPY --chown=app:app ./ /home/app/app/
|
3
6
|
|
4
7
|
######################################
|
5
8
|
|
6
9
|
FROM base as builder
|
7
10
|
|
8
|
-
COPY --chown=app
|
9
|
-
COPY --chown=app:app --from=scripts /home/scripts/builder/bundle_builder.sh /usr/local/sbin/
|
11
|
+
COPY --chown=app:app --from=scripts /home/scripts/builder/bundle_builder.sh /usr/local/sbin/bundle_builder.sh
|
10
12
|
|
11
13
|
ENV HOME_DIR /home/app
|
12
14
|
RUN bundle_builder.sh
|
@@ -14,11 +16,6 @@ RUN bundle_builder.sh
|
|
14
16
|
#######################
|
15
17
|
#FINAL IMAGE
|
16
18
|
FROM base
|
17
|
-
RUN mkdir lib/azeroth -p
|
18
19
|
|
19
20
|
COPY --chown=app:app --from=builder /home/app/bundle/ /usr/local/bundle/
|
20
|
-
|
21
|
-
COPY --chown=app ./*.gemspec ./Gemfile /home/app/app/
|
22
|
-
COPY --chown=app ./lib/azeroth/version.rb /home/app/app/lib/azeroth/
|
23
|
-
|
24
21
|
RUN bundle install
|
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.
|
14
|
+
[https://www.rubydoc.info/gems/azeroth/1.0.0](https://www.rubydoc.info/gems/azeroth/1.0.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
|
@@ -131,6 +131,36 @@ It accepts options
|
|
131
131
|
end
|
132
132
|
```
|
133
133
|
|
134
|
+
```ruby
|
135
|
+
class PaginatedDocumentsController < ApplicationController
|
136
|
+
include Azeroth::Resourceable
|
137
|
+
|
138
|
+
resource_for :document, only: 'index', paginated: true
|
139
|
+
end
|
140
|
+
|
141
|
+
30.times { create(:document) }
|
142
|
+
|
143
|
+
get '/paginated_documents.json'
|
144
|
+
|
145
|
+
# returns Array with 20 first documents
|
146
|
+
# returns in the headers pagination headers
|
147
|
+
# {
|
148
|
+
# 'pages' => 2,
|
149
|
+
# 'per_page' => 20,
|
150
|
+
# 'page' => 1
|
151
|
+
# }
|
152
|
+
|
153
|
+
get '/paginated_documents.json?page=2'
|
154
|
+
|
155
|
+
# returns Array with 10 next documents
|
156
|
+
# returns in the headers pagination headers
|
157
|
+
# {
|
158
|
+
# 'pages' => 2,
|
159
|
+
# 'per_page' => 20,
|
160
|
+
# 'page' => 2
|
161
|
+
# }
|
162
|
+
```
|
163
|
+
|
134
164
|
## Azeroth::Decorator
|
135
165
|
|
136
166
|
[Decorators](https://www.rubydoc.info/gems/azeroth/Azeroth/Decorator) are
|
data/azeroth.gemspec
CHANGED
@@ -18,21 +18,21 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ['lib']
|
20
20
|
|
21
|
-
gem.add_runtime_dependency 'activesupport', '~>
|
21
|
+
gem.add_runtime_dependency 'activesupport', '~> 7.0.x'
|
22
22
|
gem.add_runtime_dependency 'darthjee-active_ext', '>= 1.3.2'
|
23
|
-
gem.add_runtime_dependency 'jace', '>= 0.
|
24
|
-
gem.add_runtime_dependency 'sinclair', '>=
|
23
|
+
gem.add_runtime_dependency 'jace', '>= 0.1.1'
|
24
|
+
gem.add_runtime_dependency 'sinclair', '>= 2.0.0'
|
25
25
|
|
26
|
-
gem.add_development_dependency 'actionpack', '
|
27
|
-
gem.add_development_dependency 'activerecord', '
|
28
|
-
gem.add_development_dependency 'bundler', '~> 2.
|
26
|
+
gem.add_development_dependency 'actionpack', '7.0.4.3'
|
27
|
+
gem.add_development_dependency 'activerecord', '7.0.4.3'
|
28
|
+
gem.add_development_dependency 'bundler', '~> 2.4.8'
|
29
29
|
gem.add_development_dependency 'factory_bot', '6.2.1'
|
30
|
-
gem.add_development_dependency 'minitest', '
|
31
|
-
gem.add_development_dependency 'nokogiri', '1.13.
|
30
|
+
gem.add_development_dependency 'minitest', '5.16.2'
|
31
|
+
gem.add_development_dependency 'nokogiri', '1.13.8'
|
32
32
|
gem.add_development_dependency 'pry', '0.14.1'
|
33
33
|
gem.add_development_dependency 'pry-nav', '1.0.0'
|
34
|
-
gem.add_development_dependency 'rails', '
|
35
|
-
gem.add_development_dependency 'rails-controller-testing', '1.0.
|
34
|
+
gem.add_development_dependency 'rails', '7.0.4.3'
|
35
|
+
gem.add_development_dependency 'rails-controller-testing', '1.0.5'
|
36
36
|
gem.add_development_dependency 'rake', '13.0.6'
|
37
37
|
gem.add_development_dependency 'reek', '6.0.3'
|
38
38
|
gem.add_development_dependency 'rspec', '3.11.0'
|
@@ -44,9 +44,10 @@ Gem::Specification.new do |gem|
|
|
44
44
|
gem.add_development_dependency 'rspec-support', '3.11.0'
|
45
45
|
gem.add_development_dependency 'rubocop', '0.80.1'
|
46
46
|
gem.add_development_dependency 'rubocop-rspec', '1.38.1'
|
47
|
-
gem.add_development_dependency 'rubycritic', '4.
|
47
|
+
gem.add_development_dependency 'rubycritic', '4.7.0'
|
48
48
|
gem.add_development_dependency 'shoulda-matchers', '4.3.0'
|
49
49
|
gem.add_development_dependency 'simplecov', '0.21.2'
|
50
|
+
gem.add_development_dependency 'sprockets-rails', '3.4.2'
|
50
51
|
gem.add_development_dependency 'sqlite3', '1.4.2'
|
51
52
|
gem.add_development_dependency 'tzinfo-data', '~> 1.2022.1'
|
52
53
|
gem.add_development_dependency 'yard', '0.9.27'
|
data/config/yardstick.yml
CHANGED
@@ -31,6 +31,7 @@ rules:
|
|
31
31
|
- Azeroth::Exception::InvalidOptions#initialize
|
32
32
|
- Azeroth::Model#initialize
|
33
33
|
- Azeroth::Options#initialize
|
34
|
+
- Azeroth::ParamsBuilder#initialize
|
34
35
|
- Azeroth::RequestHandler#initialize
|
35
36
|
- Azeroth::ResourceBuilder#initialize
|
36
37
|
- Azeroth::ResourceRouteBuilder#initialize
|
data/docker-compose.yml
CHANGED
@@ -21,11 +21,3 @@ services:
|
|
21
21
|
<<: *base
|
22
22
|
depends_on: [base_build]
|
23
23
|
command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
|
24
|
-
|
25
|
-
circleci:
|
26
|
-
image: azeroth_circleci
|
27
|
-
build:
|
28
|
-
context: ./
|
29
|
-
dockerfile: Dockerfile.circleci
|
30
|
-
volumes:
|
31
|
-
- .:/home/circleci/project
|
@@ -25,7 +25,9 @@ module Azeroth
|
|
25
25
|
# @return [Hash]
|
26
26
|
def as_json
|
27
27
|
attributes_map.inject({}) do |hash, (method, options)|
|
28
|
-
new_hash = KeyValueExtractor.new(
|
28
|
+
new_hash = KeyValueExtractor.new(
|
29
|
+
decorator: decorator, attribute: method, options: options
|
30
|
+
).as_json
|
29
31
|
hash.merge!(new_hash)
|
30
32
|
end
|
31
33
|
end
|
@@ -11,18 +11,14 @@ module Azeroth
|
|
11
11
|
#
|
12
12
|
# A decorator is infered for the value / attribute
|
13
13
|
# or used from the options given
|
14
|
-
class KeyValueExtractor
|
14
|
+
class KeyValueExtractor < Sinclair::Model
|
15
|
+
initialize_with(:decorator, :attribute, :options)
|
15
16
|
# @param decorator [Decorator] decorator object
|
16
17
|
# @param attribute [Symbol] attribute to be used on output hash
|
17
18
|
# @param options [Decorator::Options] decoration options
|
18
19
|
# @option options if [Proc,Symbol] conditional to be
|
19
20
|
# checked when exposing field
|
20
21
|
# (see {Decorator::Options#if})
|
21
|
-
def initialize(decorator, attribute, options)
|
22
|
-
@decorator = decorator
|
23
|
-
@attribute = attribute
|
24
|
-
@options = options
|
25
|
-
end
|
26
22
|
|
27
23
|
# Return hash for attribute
|
28
24
|
#
|
@@ -38,9 +34,8 @@ module Azeroth
|
|
38
34
|
}
|
39
35
|
end
|
40
36
|
|
41
|
-
|
37
|
+
private
|
42
38
|
|
43
|
-
attr_reader :decorator, :attribute, :options
|
44
39
|
# @method decorator
|
45
40
|
# @api private
|
46
41
|
# @private
|
data/lib/azeroth/options.rb
CHANGED
@@ -35,18 +35,13 @@ module Azeroth
|
|
35
35
|
[only].flatten.map(&:to_sym) - [except].flatten.map(&:to_sym)
|
36
36
|
end
|
37
37
|
|
38
|
-
# Returns event
|
39
|
-
#
|
40
|
-
# Event
|
41
|
-
#
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
def event_dispatcher(event)
|
46
|
-
Jace::Dispatcher.new(
|
47
|
-
before: try("before_#{event}"),
|
48
|
-
after: try("after_#{event}")
|
49
|
-
)
|
38
|
+
# Returns the event registry
|
39
|
+
#
|
40
|
+
# Event registry is used to handle events within the request
|
41
|
+
#
|
42
|
+
# @return [Jace::Registry]
|
43
|
+
def event_registry
|
44
|
+
@event_registry ||= build_event_registry
|
50
45
|
end
|
51
46
|
|
52
47
|
alias paginated? paginated
|
@@ -131,5 +126,22 @@ module Azeroth
|
|
131
126
|
# Number of elements when pagination is active
|
132
127
|
#
|
133
128
|
# @return [Integer]
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
# private
|
133
|
+
#
|
134
|
+
# Builds the event registr
|
135
|
+
#
|
136
|
+
# The event registry is build using the before
|
137
|
+
# and after actions defined in optionsy
|
138
|
+
#
|
139
|
+
# @return [Jace::Registry]
|
140
|
+
def build_event_registry
|
141
|
+
Jace::Registry.new.tap do |registry|
|
142
|
+
registry.register(:save, :after, &after_save)
|
143
|
+
registry.register(:save, :before, &before_save)
|
144
|
+
end
|
145
|
+
end
|
134
146
|
end
|
135
147
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Azeroth
|
4
|
+
# @api private
|
5
|
+
# @author Darthjee
|
6
|
+
#
|
7
|
+
# Class responsible for adding params handling methods to a controller
|
8
|
+
class ParamsBuilder < Sinclair::Model
|
9
|
+
# @!method initialize(model:, builder:)
|
10
|
+
# @api private
|
11
|
+
#
|
12
|
+
# @param model [Model] Resource interface
|
13
|
+
# @param builder [Sinclair] Methods builder
|
14
|
+
#
|
15
|
+
# @return [ParamsBuilder]
|
16
|
+
initialize_with(:model, :builder, writter: false)
|
17
|
+
|
18
|
+
# Append the params methods to be built
|
19
|
+
#
|
20
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
21
|
+
def append
|
22
|
+
method_name = name
|
23
|
+
allowed_attributes = permitted_attributes.map(&:to_sym)
|
24
|
+
|
25
|
+
add_method("#{name}_id") { params.require(:id) }
|
26
|
+
add_method("#{name}_params") do
|
27
|
+
params.require(method_name)
|
28
|
+
.permit(*allowed_attributes)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# @method model
|
35
|
+
# @api private
|
36
|
+
# @private
|
37
|
+
#
|
38
|
+
# Resource interface
|
39
|
+
#
|
40
|
+
# @return [Model]
|
41
|
+
|
42
|
+
# @method builder
|
43
|
+
# @api private
|
44
|
+
# @private
|
45
|
+
#
|
46
|
+
# Methods builder
|
47
|
+
#
|
48
|
+
# @return [Sinclair]
|
49
|
+
|
50
|
+
delegate :add_method, to: :builder
|
51
|
+
# @method add_method
|
52
|
+
# @api private
|
53
|
+
# @private
|
54
|
+
#
|
55
|
+
# Appends a method
|
56
|
+
#
|
57
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
58
|
+
|
59
|
+
delegate :name, :klass, to: :model
|
60
|
+
# @method name
|
61
|
+
# @api private
|
62
|
+
# @private
|
63
|
+
#
|
64
|
+
# Resource name
|
65
|
+
#
|
66
|
+
# @return [Symbol,String]
|
67
|
+
|
68
|
+
# @method klass
|
69
|
+
# @api private
|
70
|
+
# @private
|
71
|
+
#
|
72
|
+
# Controller to be changed
|
73
|
+
#
|
74
|
+
# @return [Array<Sinclair::MethodDefinition>]
|
75
|
+
|
76
|
+
# Returns all updatable attributes
|
77
|
+
#
|
78
|
+
# @return [Array<String>]
|
79
|
+
def permitted_attributes
|
80
|
+
@permitted_attributes ||= klass.attribute_names - ['id']
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -36,7 +36,9 @@ module Azeroth
|
|
36
36
|
|
37
37
|
# @private
|
38
38
|
#
|
39
|
-
# Update the resource
|
39
|
+
# Update the resource
|
40
|
+
#
|
41
|
+
# This update happens by either by running update_with
|
40
42
|
# or directly updating the attributes in the object
|
41
43
|
#
|
42
44
|
# @return [Object] updated resource
|
@@ -119,8 +119,7 @@ module Azeroth
|
|
119
119
|
#
|
120
120
|
# @return [Object] Result of given block
|
121
121
|
def trigger_event(event, &block)
|
122
|
-
options.
|
123
|
-
.dispatch(controller, &block)
|
122
|
+
options.event_registry.trigger(event, controller, &block)
|
124
123
|
end
|
125
124
|
|
126
125
|
# @private
|
@@ -9,13 +9,15 @@ module Azeroth
|
|
9
9
|
# The builder adds 2 methods, one for listing all
|
10
10
|
# entries of a resource, and one for fetching an specific
|
11
11
|
# entry
|
12
|
-
class ResourceBuilder
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
class ResourceBuilder < Sinclair::Model
|
13
|
+
# @!method initialize(model:, builder:)
|
14
|
+
# @api private
|
15
|
+
#
|
16
|
+
# @param model [Model] Resource model interface
|
17
|
+
# @param builder [Sinclair] method builder
|
18
|
+
#
|
19
|
+
# @return [ResourceBuilder]
|
20
|
+
initialize_with(:model, :builder, writter: false)
|
19
21
|
|
20
22
|
# Append methods to be built to the builder
|
21
23
|
#
|
@@ -28,9 +30,6 @@ module Azeroth
|
|
28
30
|
add_method(name, "@#{name} ||= #{plural}.find(#{name}_id)")
|
29
31
|
end
|
30
32
|
|
31
|
-
private
|
32
|
-
|
33
|
-
attr_reader :model, :builder
|
34
33
|
# @method model
|
35
34
|
# @api private
|
36
35
|
# @private
|
@@ -13,12 +13,12 @@ module Azeroth
|
|
13
13
|
# @see ResourceRouteBuilder
|
14
14
|
# @see RoutesBuilder
|
15
15
|
class Builder
|
16
|
-
# @param
|
16
|
+
# @param klass [ActionController::Base] Controller to
|
17
17
|
# to be changed
|
18
18
|
# @param model_name [Symbol,String]
|
19
19
|
# @param options [Options]
|
20
|
-
def initialize(
|
21
|
-
@
|
20
|
+
def initialize(klass, model_name, options)
|
21
|
+
@klass = klass
|
22
22
|
@options = options
|
23
23
|
@model = Azeroth::Model.new(model_name, options)
|
24
24
|
|
@@ -30,8 +30,8 @@ module Azeroth
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
attr_reader :
|
34
|
-
# @method
|
33
|
+
attr_reader :klass, :model, :options
|
34
|
+
# @method klass
|
35
35
|
# @api private
|
36
36
|
# @private
|
37
37
|
#
|
@@ -71,7 +71,7 @@ module Azeroth
|
|
71
71
|
#
|
72
72
|
# @return [Array<Sinclair::MethodDefinition>]
|
73
73
|
|
74
|
-
delegate :name,
|
74
|
+
delegate :name, to: :model
|
75
75
|
# @method name
|
76
76
|
# @api private
|
77
77
|
# @private
|
@@ -80,14 +80,6 @@ module Azeroth
|
|
80
80
|
#
|
81
81
|
# @return [Symbol,String]
|
82
82
|
|
83
|
-
# @method klass
|
84
|
-
# @api private
|
85
|
-
# @private
|
86
|
-
#
|
87
|
-
# Controller to be changed
|
88
|
-
#
|
89
|
-
# @return [Array<Sinclair::MethodDefinition>]
|
90
|
-
|
91
83
|
# @api private
|
92
84
|
# @private
|
93
85
|
#
|
@@ -97,50 +89,40 @@ module Azeroth
|
|
97
89
|
#
|
98
90
|
# @see https://www.rubydoc.info/gems/sinclair Sinclair
|
99
91
|
def builder
|
100
|
-
@builder ||= Sinclair.new(
|
92
|
+
@builder ||= Sinclair.new(klass)
|
101
93
|
end
|
102
94
|
|
103
95
|
# Add methods for id and parameters
|
104
96
|
#
|
105
97
|
# @return [Array<Sinclair::MethodDefinition>]
|
106
98
|
def add_params
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
<<-CODE
|
111
|
-
params.require(:#{name})
|
112
|
-
.permit(:#{permitted_attributes.join(', :')})
|
113
|
-
CODE
|
114
|
-
)
|
99
|
+
ParamsBuilder.new(
|
100
|
+
model: model, builder: builder
|
101
|
+
).append
|
115
102
|
end
|
116
103
|
|
117
104
|
# Add methods for resource fetching
|
118
105
|
#
|
119
106
|
# @return [Array<Sinclair::MethodDefinition>]
|
120
107
|
def add_resource
|
121
|
-
ResourceBuilder.new(model, builder).append
|
108
|
+
ResourceBuilder.new(model: model, builder: builder).append
|
122
109
|
end
|
123
110
|
|
124
111
|
# Add metohods for each route
|
125
112
|
#
|
126
113
|
# @return [Array<Sinclair::MethodDefinition>]
|
127
114
|
def add_routes
|
128
|
-
RoutesBuilder.new(
|
115
|
+
RoutesBuilder.new(
|
116
|
+
model: model, builder: builder, options: options
|
117
|
+
).append
|
129
118
|
end
|
130
119
|
|
131
120
|
# Add helpers to render objects on template
|
132
121
|
#
|
133
122
|
# @return [String]
|
134
123
|
def add_helpers
|
135
|
-
|
136
|
-
|
137
|
-
end
|
138
|
-
|
139
|
-
# Returns all updatable attributes
|
140
|
-
#
|
141
|
-
# @return [Array<String>]
|
142
|
-
def permitted_attributes
|
143
|
-
@permitted_attributes ||= klass.attribute_names - ['id']
|
124
|
+
klass.public_send(:helper_method, model.name)
|
125
|
+
klass.public_send(:helper_method, model.plural)
|
144
126
|
end
|
145
127
|
end
|
146
128
|
end
|
@@ -14,136 +14,7 @@ module Azeroth
|
|
14
14
|
# @return (see Resourceable.resource_for)
|
15
15
|
#
|
16
16
|
# @see (see Resourceable.resource_for)
|
17
|
-
#
|
18
|
-
# @example Controller without delete
|
19
|
-
# class DocumentsController < ApplicationController
|
20
|
-
# include Azeroth::Resourceable
|
21
|
-
#
|
22
|
-
# resource_for :document, except: :delete
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# @example Controller with only create, show and list
|
26
|
-
# class DocumentsController < ApplicationController
|
27
|
-
# include Azeroth::Resourceable
|
28
|
-
#
|
29
|
-
# resource_for :document, only: %w[create index show]
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# @example complete example gmaes and publishers
|
33
|
-
# class PublishersController < ApplicationController
|
34
|
-
# include Azeroth::Resourceable
|
35
|
-
# skip_before_action :verify_authenticity_token
|
36
|
-
#
|
37
|
-
# resource_for :publisher, only: %i[create index]
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# class GamesController < ApplicationController
|
41
|
-
# include Azeroth::Resourceable
|
42
|
-
# skip_before_action :verify_authenticity_token
|
43
|
-
#
|
44
|
-
# resource_for :game, except: :delete
|
45
|
-
#
|
46
|
-
# private
|
47
|
-
#
|
48
|
-
# def games
|
49
|
-
# publisher.games
|
50
|
-
# end
|
51
|
-
#
|
52
|
-
# def publisher
|
53
|
-
# @publisher ||= Publisher.find(publisher_id)
|
54
|
-
# end
|
55
|
-
#
|
56
|
-
# def publisher_id
|
57
|
-
# params.require(:publisher_id)
|
58
|
-
# end
|
59
|
-
# end
|
60
|
-
#
|
61
|
-
# ActiveRecord::Schema.define do
|
62
|
-
# self.verbose = false
|
63
|
-
#
|
64
|
-
# create_table :publishers, force: true do |t|
|
65
|
-
# t.string :name
|
66
|
-
# end
|
67
|
-
#
|
68
|
-
# create_table :games, force: true do |t|
|
69
|
-
# t.string :name
|
70
|
-
# t.integer :publisher_id
|
71
|
-
# end
|
72
|
-
# end
|
73
|
-
#
|
74
|
-
# class Publisher < ActiveRecord::Base
|
75
|
-
# has_many :games
|
76
|
-
# end
|
77
|
-
#
|
78
|
-
# class Game < ActiveRecord::Base
|
79
|
-
# belongs_to :publisher
|
80
|
-
# end
|
81
|
-
#
|
82
|
-
# class Game::Decorator < Azeroth::Decorator
|
83
|
-
# expose :id
|
84
|
-
# expose :name
|
85
|
-
# expose :publisher, decorator: NameDecorator
|
86
|
-
# end
|
87
|
-
#
|
88
|
-
# @example requesting games and publishers
|
89
|
-
# post "/publishers.json", params: {
|
90
|
-
# publisher: {
|
91
|
-
# name: 'Nintendo'
|
92
|
-
# }
|
93
|
-
# }
|
94
|
-
#
|
95
|
-
# publisher = JSON.parse(response.body)
|
96
|
-
# # returns
|
97
|
-
# # {
|
98
|
-
# # 'id' => 11,
|
99
|
-
# # 'name' => 'Nintendo'
|
100
|
-
# # }
|
101
|
-
#
|
102
|
-
# publisher = Publisher.last
|
103
|
-
# post "/publishers/#{publisher['id']}/games.json", params: {
|
104
|
-
# game: {
|
105
|
-
# name: 'Pokemon'
|
106
|
-
# }
|
107
|
-
# }
|
108
|
-
#
|
109
|
-
# game = Game.last
|
110
|
-
#
|
111
|
-
# JSON.parse(response.body)
|
112
|
-
# # returns
|
113
|
-
# # {
|
114
|
-
# # id: game.id,
|
115
|
-
# # name: 'Pokemon',
|
116
|
-
# # publisher: {
|
117
|
-
# # name: 'Nintendo'
|
118
|
-
# # }
|
119
|
-
# }
|
120
|
-
#
|
121
|
-
# @example Controller with before_save
|
122
|
-
# class PokemonsController < ApplicationController
|
123
|
-
# include Azeroth::Resourceable
|
124
|
-
#
|
125
|
-
# resource_for :pokemon,
|
126
|
-
# only: %i[create update],
|
127
|
-
# before_save: :set_favorite
|
128
|
-
#
|
129
|
-
# private
|
130
|
-
#
|
131
|
-
# def set_favorite
|
132
|
-
# pokemon.favorite = true
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# def pokemons
|
136
|
-
# master.pokemons
|
137
|
-
# end
|
138
|
-
#
|
139
|
-
# def master
|
140
|
-
# @master ||= PokemonMaster.find(master_id)
|
141
|
-
# end
|
142
|
-
#
|
143
|
-
# def master_id
|
144
|
-
# params.require(:pokemon_master_id)
|
145
|
-
# end
|
146
|
-
# end
|
17
|
+
# @example (see Resourceable.resource_for)
|
147
18
|
def resource_for(name, **options)
|
148
19
|
Builder.new(
|
149
20
|
self, name, Azeroth::Options.new(options)
|