croods 0.2.7 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -7
- data/lib/croods.rb +2 -0
- data/lib/croods/controller/collection.rb +21 -3
- 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 +9 -1
- data/lib/croods/resource/json_schema/links/collection.rb +17 -2
- data/lib/croods/resource/pagination.rb +31 -0
- data/lib/croods/resource/sorting.rb +17 -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: b9fa939ee66088adb051cc63ee6f9cfc1206da7674dd6f2c48c524ea0ca8b931
|
4
|
+
data.tar.gz: 9e9722e961a8b24760106965a385a5aad1a89d9b85369243d3497c2c3fc33784
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65565603371e3e5d46d3c8f412af157bd4e50ca2c1f6afc57a3577f4dd77f294bd63624c7f02585973214ff978d7312367d520916f7497c4f29f49f582b28f1b
|
7
|
+
data.tar.gz: 3d0f2872653a07a3edb49cdb3cc368e34eb153826a5066099ed250400000613ea128bbbf5dac4cc3563fffe87f67b94ac82d2854a8a30004010d4f6d200b9be5
|
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,28 @@ 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
|
+
|
16
|
+
def sort(list)
|
17
|
+
if list.respond_to? resource.sort_by
|
18
|
+
list.public_send(resource.sort_by, params[:order_by], params[:order])
|
19
|
+
else
|
20
|
+
list.order(resource.sort_by)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
8
24
|
def collection
|
9
|
-
resource
|
10
|
-
|
11
|
-
|
25
|
+
list = resource.apply_filters(policy_scope(model), params)
|
26
|
+
list = sort(list)
|
27
|
+
list = paginate_collection(list) if page_param.present?
|
28
|
+
|
29
|
+
list
|
12
30
|
end
|
13
31
|
end
|
14
32
|
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
|
|
@@ -15,7 +15,15 @@ module Croods
|
|
15
15
|
|
16
16
|
class << self
|
17
17
|
def schema(attribute)
|
18
|
-
{ 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] } }
|
19
27
|
end
|
20
28
|
|
21
29
|
def format(attribute)
|
@@ -33,6 +33,18 @@ module Croods
|
|
33
33
|
filters
|
34
34
|
end
|
35
35
|
|
36
|
+
def properties(resource)
|
37
|
+
properties = {}
|
38
|
+
|
39
|
+
params = resource.pagination_params + resource.order_params
|
40
|
+
|
41
|
+
params.each do |attribute|
|
42
|
+
properties[attribute.name] = definition(attribute)
|
43
|
+
end
|
44
|
+
|
45
|
+
properties.merge(filters(resource))
|
46
|
+
end
|
47
|
+
|
36
48
|
def definition(attribute)
|
37
49
|
definition = Definition.schema(attribute)
|
38
50
|
|
@@ -42,13 +54,16 @@ module Croods
|
|
42
54
|
end
|
43
55
|
|
44
56
|
def required(resource)
|
45
|
-
resource.filters
|
57
|
+
collection_properties = resource.filters +
|
58
|
+
resource.pagination_params
|
59
|
+
|
60
|
+
collection_properties.reject(&:null).map(&:name)
|
46
61
|
end
|
47
62
|
|
48
63
|
def schema(resource)
|
49
64
|
{
|
50
65
|
additionalProperties: false,
|
51
|
-
properties:
|
66
|
+
properties: properties(resource),
|
52
67
|
required: required(resource),
|
53
68
|
type: ['object']
|
54
69
|
}
|
@@ -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
|
@@ -3,10 +3,26 @@
|
|
3
3
|
module Croods
|
4
4
|
module Resource
|
5
5
|
module Sorting
|
6
|
+
def order_by_attribute
|
7
|
+
@order_by_attribute ||= Croods::Attribute.new(
|
8
|
+
'order_by', :string, null: true
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
def order_attribute
|
13
|
+
@order_attribute ||= Croods::Attribute.new(
|
14
|
+
'order', :string, null: true
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def order_params
|
19
|
+
@order_params ||= [order_by_attribute, order_attribute]
|
20
|
+
end
|
21
|
+
|
6
22
|
def sort_by(attribute = nil)
|
7
23
|
return @sort_by ||= :created_at unless attribute
|
8
24
|
|
9
|
-
@sort_by
|
25
|
+
@sort_by ||= attribute
|
10
26
|
end
|
11
27
|
end
|
12
28
|
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.1
|
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-14 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
|