croods 0.2.6 → 0.3.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/README.md +5 -7
- data/lib/croods.rb +2 -0
- data/lib/croods/controller/collection.rb +13 -1
- data/lib/croods/controller/member.rb +1 -1
- data/lib/croods/policy/scope.rb +2 -0
- data/lib/croods/resource.rb +2 -0
- data/lib/croods/resource/authorization.rb +16 -3
- data/lib/croods/resource/json_schema/definition.rb +24 -10
- data/lib/croods/resource/json_schema/links/collection.rb +15 -2
- data/lib/croods/resource/pagination.rb +31 -0
- data/lib/croods/resource/policy.rb +1 -1
- data/lib/croods/routes.rb +1 -0
- data/lib/croods/version.rb +1 -1
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abd8dafdfb10c2b936a894dcb6da1057c6bfe3646f6e6d5af0f58d59d0be7bab
|
4
|
+
data.tar.gz: 8da5a9b8c82bacefae7d736610d7ba4e0e3305ecf832a602dd61cff0edc1b53c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: deac1ff6c8763d738b8ccd20149632df9be2425690a1550d57172e3a922a1b685f484d848f39fa5f3a8a29b3bd596c4c8c27e6f7356dbd4d56d40ff0abf879a2
|
7
|
+
data.tar.gz: 7a7342dba26327dc7e1422b26adf56b08747a20a3062d1c1090b941c6d3cf4030f2953975fceb770fb1127cc15097e7398ee35ec0ca017c1a4a6b18ba3a78a4a
|
data/README.md
CHANGED
@@ -130,15 +130,13 @@ use_for_authentication!(
|
|
130
130
|
## Contributing
|
131
131
|
|
132
132
|
You can manually check your changes in the dummy Rails app under `/todos`.
|
133
|
+
Use it to run integration tests since it doesn't have an UI.
|
133
134
|
|
134
|
-
|
135
|
+
- Clone the repository
|
136
|
+
- Install bundler `gem install bundler`
|
137
|
+
- Install gems `bin/bundle`
|
135
138
|
|
136
|
-
|
137
|
-
cd todos/
|
138
|
-
bin/setup
|
139
|
-
```
|
140
|
-
|
141
|
-
To run specs use:
|
139
|
+
To run both Croods-rails' and the example's specs use:
|
142
140
|
`bin/rspec`
|
143
141
|
|
144
142
|
## License
|
data/lib/croods.rb
CHANGED
@@ -5,10 +5,22 @@ module Croods
|
|
5
5
|
module Collection
|
6
6
|
protected
|
7
7
|
|
8
|
+
def page_param
|
9
|
+
params[resource.page_attribute_name]
|
10
|
+
end
|
11
|
+
|
12
|
+
def paginate_collection(list)
|
13
|
+
paginate(list.send(resource.page_method_name, page_param))
|
14
|
+
end
|
15
|
+
|
8
16
|
def collection
|
9
|
-
resource
|
17
|
+
list = resource
|
10
18
|
.apply_filters(policy_scope(model), params)
|
11
19
|
.order(resource.sort_by)
|
20
|
+
|
21
|
+
list = paginate_collection(list) if page_param.present?
|
22
|
+
|
23
|
+
list
|
12
24
|
end
|
13
25
|
end
|
14
26
|
end
|
data/lib/croods/policy/scope.rb
CHANGED
@@ -38,6 +38,8 @@ module Croods
|
|
38
38
|
associations = scope.reflect_on_all_associations(:belongs_to)
|
39
39
|
|
40
40
|
associations.each do |association|
|
41
|
+
next if association.options[:optional]
|
42
|
+
|
41
43
|
model = association.class_name.constantize
|
42
44
|
expanded_path = path + [association]
|
43
45
|
association_path = reflection_path(model, target, expanded_path)
|
data/lib/croods/resource.rb
CHANGED
@@ -12,6 +12,7 @@ require_relative 'resource/json_schema'
|
|
12
12
|
require_relative 'resource/authentication'
|
13
13
|
require_relative 'resource/authorization'
|
14
14
|
require_relative 'resource/filters'
|
15
|
+
require_relative 'resource/pagination'
|
15
16
|
require_relative 'resource/sorting'
|
16
17
|
require_relative 'resource/services'
|
17
18
|
|
@@ -32,6 +33,7 @@ module Croods
|
|
32
33
|
include Authentication
|
33
34
|
include Authorization
|
34
35
|
include Filters
|
36
|
+
include Pagination
|
35
37
|
include Sorting
|
36
38
|
include Services
|
37
39
|
end
|
@@ -8,10 +8,23 @@ module Croods
|
|
8
8
|
|
9
9
|
on = [on] if on&.is_a?(Symbol)
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
authorization_roles << { roles: roles, on: on }
|
12
|
+
end
|
13
|
+
|
14
|
+
def authorization_roles
|
15
|
+
@authorization_roles ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def apply_authorization_roles!
|
19
|
+
authorization_roles.each do |authorization|
|
20
|
+
(actions + additional_actions).each do |action|
|
21
|
+
on = authorization[:on]
|
22
|
+
roles = authorization[:roles]
|
23
|
+
|
24
|
+
next if on && !on.include?(action.name)
|
13
25
|
|
14
|
-
|
26
|
+
action.roles = roles
|
27
|
+
end
|
15
28
|
end
|
16
29
|
end
|
17
30
|
|
@@ -5,16 +5,25 @@ module Croods
|
|
5
5
|
module JsonSchema
|
6
6
|
module Definition
|
7
7
|
TYPES = {
|
8
|
-
datetime: 'string',
|
9
|
-
date: 'string',
|
10
|
-
text: 'string',
|
11
|
-
json:
|
12
|
-
|
8
|
+
datetime: ['string'],
|
9
|
+
date: ['string'],
|
10
|
+
text: ['string'],
|
11
|
+
json: %w[object array],
|
12
|
+
jsonb: %w[object array],
|
13
|
+
float: ['number']
|
13
14
|
}.freeze
|
14
15
|
|
15
16
|
class << self
|
16
17
|
def schema(attribute)
|
17
|
-
{ type: types(attribute) }
|
18
|
+
{ type: types(attribute) }
|
19
|
+
.merge(format(attribute))
|
20
|
+
.merge(items(attribute))
|
21
|
+
end
|
22
|
+
|
23
|
+
def items(attribute)
|
24
|
+
return {} unless %i[json jsonb].include?(attribute.type)
|
25
|
+
|
26
|
+
{ items: { type: %w[string number object] } }
|
18
27
|
end
|
19
28
|
|
20
29
|
def format(attribute)
|
@@ -30,13 +39,18 @@ module Croods
|
|
30
39
|
|
31
40
|
def types(attribute)
|
32
41
|
types = []
|
33
|
-
|
34
|
-
|
42
|
+
converted_types(attribute.type).each do |converted_type|
|
43
|
+
types << converted_type
|
44
|
+
end
|
45
|
+
null = (
|
46
|
+
attribute.null || attribute.default || attribute.default_function
|
47
|
+
)
|
48
|
+
types << 'null' if null
|
35
49
|
types
|
36
50
|
end
|
37
51
|
|
38
|
-
def
|
39
|
-
TYPES[type] || type.to_s
|
52
|
+
def converted_types(type)
|
53
|
+
TYPES[type] || [type.to_s]
|
40
54
|
end
|
41
55
|
end
|
42
56
|
end
|
@@ -33,6 +33,16 @@ module Croods
|
|
33
33
|
filters
|
34
34
|
end
|
35
35
|
|
36
|
+
def properties(resource)
|
37
|
+
properties = {}
|
38
|
+
|
39
|
+
resource.pagination_params.each do |attribute|
|
40
|
+
properties[attribute.name] = definition(attribute)
|
41
|
+
end
|
42
|
+
|
43
|
+
properties.merge(filters(resource))
|
44
|
+
end
|
45
|
+
|
36
46
|
def definition(attribute)
|
37
47
|
definition = Definition.schema(attribute)
|
38
48
|
|
@@ -42,13 +52,16 @@ module Croods
|
|
42
52
|
end
|
43
53
|
|
44
54
|
def required(resource)
|
45
|
-
resource.filters
|
55
|
+
collection_properties = resource.filters +
|
56
|
+
resource.pagination_params
|
57
|
+
|
58
|
+
collection_properties.reject(&:null).map(&:name)
|
46
59
|
end
|
47
60
|
|
48
61
|
def schema(resource)
|
49
62
|
{
|
50
63
|
additionalProperties: false,
|
51
|
-
properties:
|
64
|
+
properties: properties(resource),
|
52
65
|
required: required(resource),
|
53
66
|
type: ['object']
|
54
67
|
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Croods
|
4
|
+
module Resource
|
5
|
+
module Pagination
|
6
|
+
def page_attribute
|
7
|
+
@page_attribute ||= Croods::Attribute.new(
|
8
|
+
Kaminari.config.param_name, :string, null: true
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
def per_page_attribute
|
13
|
+
@per_page_attribute ||= Croods::Attribute.new(
|
14
|
+
'per_page', :string, null: true
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def pagination_params
|
19
|
+
@pagination_params ||= [page_attribute, per_page_attribute]
|
20
|
+
end
|
21
|
+
|
22
|
+
def page_attribute_name
|
23
|
+
@page_attribute.name
|
24
|
+
end
|
25
|
+
|
26
|
+
def page_method_name
|
27
|
+
@page_method_name ||= Kaminari.config.page_method_name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/croods/routes.rb
CHANGED
data/lib/croods/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: croods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Weinmann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: api-pagination
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: committee
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +52,20 @@ dependencies:
|
|
38
52
|
- - '='
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: 1.1.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: kaminari
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: pundit
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -296,6 +324,7 @@ files:
|
|
296
324
|
- lib/croods/resource/json_schema/required.rb
|
297
325
|
- lib/croods/resource/model.rb
|
298
326
|
- lib/croods/resource/names.rb
|
327
|
+
- lib/croods/resource/pagination.rb
|
299
328
|
- lib/croods/resource/paths.rb
|
300
329
|
- lib/croods/resource/policy.rb
|
301
330
|
- lib/croods/resource/services.rb
|