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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d384b5ea86ec6b48095b9772afb47414627799144627547722b6322aae58d73
4
- data.tar.gz: e58a394c1e3c9bd4b6787c092a09d1bb6582e313151baaedcb9ec0aee8c72735
3
+ metadata.gz: b9fa939ee66088adb051cc63ee6f9cfc1206da7674dd6f2c48c524ea0ca8b931
4
+ data.tar.gz: 9e9722e961a8b24760106965a385a5aad1a89d9b85369243d3497c2c3fc33784
5
5
  SHA512:
6
- metadata.gz: 369cc10370be1f7e010d07a858143d929292633752286529b72c7f5558195146a350e5071585adb6a35d6c2edb1bf2a4915ab87f293001fc5669f90a0f46e77c
7
- data.tar.gz: ffaec510b409f9cd1d93337aa9939af86d685f4d144cc9e6a06b8fe259a0fabbe1d60fb9628d90ea53ebcf5d313d02d8a3d09b49d548d8aac86abcb56710673b
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
- To set it up:
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
@@ -4,6 +4,8 @@ require 'rack/cors'
4
4
  require 'committee'
5
5
  require 'devise'
6
6
  require 'devise_token_auth'
7
+ require 'kaminari'
8
+ require 'api-pagination'
7
9
  require 'pundit'
8
10
  require 'schema_associations'
9
11
  require 'schema_auto_foreign_keys'
@@ -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
- .apply_filters(policy_scope(model), params)
11
- .order(resource.sort_by)
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
@@ -29,7 +29,7 @@ module Croods
29
29
  .merge(
30
30
  params
31
31
  .require(resource.resource_name)
32
- .permit(resource.request_attributes.keys)
32
+ .permit!
33
33
  )
34
34
  end
35
35
 
@@ -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)
@@ -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
- actions.each do |action|
12
- next if on && !on.include?(action.name)
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
- action.roles = roles
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) }.merge(format(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.reject(&:null).map(&:name)
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: filters(resource),
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 = attribute
25
+ @sort_by ||= attribute
10
26
  end
11
27
  end
12
28
  end
@@ -10,6 +10,7 @@ module ActionDispatch
10
10
  resource.create_model!
11
11
  resource.create_policy!
12
12
  resource.create_controller!
13
+ resource.apply_authorization_roles!
13
14
  create_resource_routes!(resource)
14
15
  end
15
16
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Croods
4
- VERSION = '0.2.7'
4
+ VERSION = '0.3.1'
5
5
  end
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.7
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-06-25 00:00:00.000000000 Z
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