croods 0.2.8 → 0.3.2
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 +5 -0
- data/lib/croods/controller/collection.rb +36 -3
- data/lib/croods/policy/scope.rb +2 -0
- data/lib/croods/resource.rb +4 -0
- data/lib/croods/resource/authorization.rb +16 -3
- data/lib/croods/resource/json_schema/links/collection.rb +18 -2
- data/lib/croods/resource/model.rb +11 -0
- data/lib/croods/resource/pagination.rb +31 -0
- data/lib/croods/resource/search.rb +65 -0
- data/lib/croods/resource/sorting.rb +17 -1
- data/lib/croods/routes.rb +1 -0
- data/lib/croods/version.rb +1 -1
- metadata +48 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63c99f117704edaa49eb759248b862eb75611b3cf97d0d539a70b946c94478fe
|
4
|
+
data.tar.gz: 377f6b1e1ba4619fb1004b31c9c23339fbaa47144f76dde57f58a6a09d4cf06e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e16d6d4fdbab8d49a19f24561dc8018c91244373c5bd89de7667147c3186f55c29f183c90beb9a92164ef76392a2be1315db3f4ac0f4d7ef7d513f6c43b65d7
|
7
|
+
data.tar.gz: a01538c937bec85378379476e9d7948dfc87af86f5d952980875dea965e4352e2f14a0a94eae92145ed7730157523b544f12fefc16e44f5607407abfd3a305f7
|
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
@@ -4,11 +4,16 @@ require 'rack/cors'
|
|
4
4
|
require 'committee'
|
5
5
|
require 'devise'
|
6
6
|
require 'devise_token_auth'
|
7
|
+
require 'kaminari'
|
8
|
+
require 'api-pagination'
|
9
|
+
require 'pg_search'
|
7
10
|
require 'pundit'
|
8
11
|
require 'schema_associations'
|
9
12
|
require 'schema_auto_foreign_keys'
|
10
13
|
require 'schema_validations'
|
11
14
|
|
15
|
+
load 'pg_search/tasks.rb'
|
16
|
+
|
12
17
|
module Croods
|
13
18
|
cattr_accessor :namespaces, :json_schema, :multi_tenancy_by
|
14
19
|
|
@@ -5,10 +5,43 @@ 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 search_param
|
13
|
+
params[resource.search_attribute_name]
|
14
|
+
end
|
15
|
+
|
16
|
+
def paginate_collection(list)
|
17
|
+
paginate(list.send(resource.page_method_name, page_param))
|
18
|
+
end
|
19
|
+
|
20
|
+
def search_collection(list)
|
21
|
+
return list unless search_param.present?
|
22
|
+
|
23
|
+
if resource.search_block.present?
|
24
|
+
list.instance_exec(search_param, &resource.search_block)
|
25
|
+
else
|
26
|
+
list.public_send resource.search_method_name, search_param
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def sort(list)
|
31
|
+
if list.respond_to? resource.sort_by
|
32
|
+
list.public_send(resource.sort_by, params[:order_by], params[:order])
|
33
|
+
else
|
34
|
+
list.order(resource.sort_by)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
8
38
|
def collection
|
9
|
-
resource
|
10
|
-
|
11
|
-
|
39
|
+
list = resource.apply_filters(policy_scope(model), params)
|
40
|
+
list = search_collection(list) unless resource.skip_search?
|
41
|
+
list = sort(list)
|
42
|
+
list = paginate_collection(list) if page_param.present?
|
43
|
+
|
44
|
+
list
|
12
45
|
end
|
13
46
|
end
|
14
47
|
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,7 +12,9 @@ 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'
|
17
|
+
require_relative 'resource/search'
|
16
18
|
require_relative 'resource/services'
|
17
19
|
|
18
20
|
module Croods
|
@@ -32,7 +34,9 @@ module Croods
|
|
32
34
|
include Authentication
|
33
35
|
include Authorization
|
34
36
|
include Filters
|
37
|
+
include Pagination
|
35
38
|
include Sorting
|
39
|
+
include Search
|
36
40
|
include Services
|
37
41
|
end
|
38
42
|
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
|
|
@@ -33,6 +33,19 @@ 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
|
+
resource.search_params
|
41
|
+
|
42
|
+
params.each do |attribute|
|
43
|
+
properties[attribute.name] = definition(attribute)
|
44
|
+
end
|
45
|
+
|
46
|
+
properties.merge(filters(resource))
|
47
|
+
end
|
48
|
+
|
36
49
|
def definition(attribute)
|
37
50
|
definition = Definition.schema(attribute)
|
38
51
|
|
@@ -42,13 +55,16 @@ module Croods
|
|
42
55
|
end
|
43
56
|
|
44
57
|
def required(resource)
|
45
|
-
resource.filters
|
58
|
+
collection_properties = resource.filters +
|
59
|
+
resource.pagination_params
|
60
|
+
|
61
|
+
collection_properties.reject(&:null).map(&:name)
|
46
62
|
end
|
47
63
|
|
48
64
|
def schema(resource)
|
49
65
|
{
|
50
66
|
additionalProperties: false,
|
51
|
-
properties:
|
67
|
+
properties: properties(resource),
|
52
68
|
required: required(resource),
|
53
69
|
type: ['object']
|
54
70
|
}
|
@@ -17,9 +17,20 @@ module Croods
|
|
17
17
|
model_name.constantize
|
18
18
|
end
|
19
19
|
|
20
|
+
def configure_search
|
21
|
+
return unless table_exists?
|
22
|
+
|
23
|
+
model.send(:include, PgSearch::Model)
|
24
|
+
model.send(:pg_search_scope,
|
25
|
+
search_method_name,
|
26
|
+
search_options)
|
27
|
+
end
|
28
|
+
|
20
29
|
def create_model!
|
21
30
|
Object.const_set(model_name, Class.new(Croods::Model))
|
22
31
|
|
32
|
+
configure_search unless model.resource.skip_search?
|
33
|
+
|
23
34
|
model_blocks.each do |block|
|
24
35
|
model.instance_eval(&block)
|
25
36
|
end
|
@@ -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
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Croods
|
4
|
+
module Resource
|
5
|
+
module Search
|
6
|
+
def search_attribute
|
7
|
+
return if skip_search?
|
8
|
+
|
9
|
+
@search_attribute ||= Croods::Attribute.new(
|
10
|
+
'query', :string, null: true
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def search_params
|
15
|
+
return [] if skip_search?
|
16
|
+
|
17
|
+
@search_params ||= [search_attribute]
|
18
|
+
end
|
19
|
+
|
20
|
+
def search_attribute_name
|
21
|
+
return if skip_search?
|
22
|
+
|
23
|
+
@search_attribute.name
|
24
|
+
end
|
25
|
+
|
26
|
+
def search_method_name
|
27
|
+
@search_method_name ||= :search
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_search_options
|
31
|
+
searchable = []
|
32
|
+
|
33
|
+
attributes.each do |key, value|
|
34
|
+
searchable << key if value.type.in? %i[string text]
|
35
|
+
end
|
36
|
+
|
37
|
+
{
|
38
|
+
against: searchable
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def search_options
|
43
|
+
@search_options ||= default_search_options
|
44
|
+
end
|
45
|
+
|
46
|
+
def search_block
|
47
|
+
@search_block
|
48
|
+
end
|
49
|
+
|
50
|
+
def skip_search?
|
51
|
+
@skip_search
|
52
|
+
end
|
53
|
+
|
54
|
+
def skip_search
|
55
|
+
@skip_search ||= true
|
56
|
+
end
|
57
|
+
|
58
|
+
def search_by(name, options = {}, &block)
|
59
|
+
@search_method_name = name
|
60
|
+
@search_options = options
|
61
|
+
@search_block = block
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
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.2
|
4
|
+
version: 0.3.2
|
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-23 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: 4.8.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.8.2
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: committee
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +52,34 @@ 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: 1.2.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.2.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg_search
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.4
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.3.4
|
41
83
|
- !ruby/object:Gem::Dependency
|
42
84
|
name: pundit
|
43
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,14 +100,14 @@ dependencies:
|
|
58
100
|
requirements:
|
59
101
|
- - '='
|
60
102
|
- !ruby/object:Gem::Version
|
61
|
-
version: 5.2.4.
|
103
|
+
version: 5.2.4.4
|
62
104
|
type: :runtime
|
63
105
|
prerelease: false
|
64
106
|
version_requirements: !ruby/object:Gem::Requirement
|
65
107
|
requirements:
|
66
108
|
- - '='
|
67
109
|
- !ruby/object:Gem::Version
|
68
|
-
version: 5.2.4.
|
110
|
+
version: 5.2.4.4
|
69
111
|
- !ruby/object:Gem::Dependency
|
70
112
|
name: schema_associations
|
71
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -296,8 +338,10 @@ files:
|
|
296
338
|
- lib/croods/resource/json_schema/required.rb
|
297
339
|
- lib/croods/resource/model.rb
|
298
340
|
- lib/croods/resource/names.rb
|
341
|
+
- lib/croods/resource/pagination.rb
|
299
342
|
- lib/croods/resource/paths.rb
|
300
343
|
- lib/croods/resource/policy.rb
|
344
|
+
- lib/croods/resource/search.rb
|
301
345
|
- lib/croods/resource/services.rb
|
302
346
|
- lib/croods/resource/sorting.rb
|
303
347
|
- lib/croods/routes.rb
|