croods 0.2.6 → 0.3.0

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: da70e6c23805cc55c8a4d7db9e97adc567a4bcd436fb0296600238e9826955c7
4
- data.tar.gz: 43415c1a16fdefeaa51fc4dd5cf4b7c93121c8f16fbefefee5966af4ac714766
3
+ metadata.gz: abd8dafdfb10c2b936a894dcb6da1057c6bfe3646f6e6d5af0f58d59d0be7bab
4
+ data.tar.gz: 8da5a9b8c82bacefae7d736610d7ba4e0e3305ecf832a602dd61cff0edc1b53c
5
5
  SHA512:
6
- metadata.gz: 3ea56e2741d2559944657daeeb73ef18cee25a381da09e50fe5f430cc4ae008015ec241e60cf96b7c931cc998fce5333fdd7f13e9e978d6f4ca890ece42554b7
7
- data.tar.gz: da63eb7581f72c974162b6c905d438ba7afa2b8f18b1c5d319d675e8821c7a3aae4fbb3a420f46f4c09b8548911a53fd2d1b3915b7609859fdac6be82c6ac2b2
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
- 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,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
@@ -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
 
@@ -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: 'object',
12
- float: 'number'
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) }.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] } }
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
- types << type(attribute.type)
34
- types << 'null' if attribute.null
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 type(type)
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.reject(&:null).map(&:name)
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: filters(resource),
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
@@ -26,7 +26,7 @@ module Croods
26
26
  end
27
27
 
28
28
  def policy_scope_name(action)
29
- "#{model_name}#{action.to_s.camelize}Scope"
29
+ "#{model_name}#{action.to_s.titleize.gsub(/\ /, '')}Scope"
30
30
  end
31
31
 
32
32
  def create_policy!
@@ -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.6'
4
+ VERSION = '0.3.0'
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.6
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-06-13 00:00:00.000000000 Z
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