croods 0.2.5 → 0.2.10

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: b0900ac5dbe8afc94b575ee6e36071c7ca3d153c99e3fbd741e09824fe2d4334
4
- data.tar.gz: 6d2e2dd3fa58485c3bdf64c4f78615505cb9197a41efa3e362b6f15b5a807369
3
+ metadata.gz: 47184f50917d2e0f8d4c16826c1d056b783813bfc6c54a0831bc8fb333155ba8
4
+ data.tar.gz: 38f8082f8019604ac318d7308b87c5bf167eb85b2b3c85b1e58795a46e2a4895
5
5
  SHA512:
6
- metadata.gz: 50b61038f5c2fa00e737f620ae92d3a38aa2ab1128005fe53202bce881a47c673f0fc20a4d1968d4dafab4431a24ca41d13c34680f758aaf6e1c0d3a6566baa1
7
- data.tar.gz: 5fe444a426181006daff456b818201a647f1dec094f55c7627bcee5e47f5cd84a0a7d7f63dc7cbccf161188c2fb52fa8549d2fa2467eca29ac459c617281ec80
6
+ metadata.gz: 7bac77110662f6d9cbf664c3d59bb02a82f7a486cafc36a1ffa0e392772aff15ef78085b5e5f1f1ff60dd8a3235dba5e6a4c10cabb76ee70ce3364f5a1eaecbe
7
+ data.tar.gz: b7f08e87e4e51aa32067b805853f5fef004d4f8bd3b0163f63fec3abb7f797806cadf58da24b28a77709b8fb9b2c988aa7e606764ee702afd03a5aecf968e94a
data/README.md CHANGED
@@ -4,9 +4,11 @@
4
4
  ![](https://ruby-gem-downloads-badge.herokuapp.com/croods?type=total)
5
5
 
6
6
  # Croods
7
- Short description and motivation.
7
+
8
+ A framework for creating CRUDs in Rails APIs. https://croods-rails.netlify.app
8
9
 
9
10
  ## Installation
11
+
10
12
  Add this line to your application's Gemfile:
11
13
 
12
14
  ```ruby
@@ -14,19 +16,23 @@ gem 'croods'
14
16
  ```
15
17
 
16
18
  And then execute:
19
+
17
20
  ```bash
18
21
  $ bundle
19
22
  ```
20
23
 
21
24
  ## Usage
25
+
22
26
  ### Resource
27
+
23
28
  Resource is a generic abstraction for any object your app needs to represent. Instead of `app/models/` and `app/controllers/`, with croods we have `app/resources/`.
24
29
 
25
30
  ### Creating a resource
31
+
26
32
  To add a `Project` resource to your app, start by generating a migration:
27
- ```rails g migration CreateProjects```
33
+ `rails g migration CreateProjects`
28
34
 
29
- ``` ruby
35
+ ```ruby
30
36
  # It's crucial to write really solid migrations
31
37
  # croods will use your database schema to build your resources.
32
38
  class CreateProjects < ActiveRecord::Migration[5.2]
@@ -38,8 +44,10 @@ class CreateProjects < ActiveRecord::Migration[5.2]
38
44
  end
39
45
  end
40
46
  ```
47
+
41
48
  Then create the module and the main file `app/resources/projects/resource.rb`:
42
- ```ruby
49
+
50
+ ```ruby
43
51
  module Projects
44
52
  class Resource < ApplicationResource
45
53
  end
@@ -47,12 +55,15 @@ end
47
55
  ```
48
56
 
49
57
  Last step is to initialize your resource in `config/initializers/croods.rb`:
58
+
50
59
  ```ruby
51
60
  Croods.initialize_for(:users, :projects)
52
61
  ```
53
62
 
54
63
  ### Skip actions
55
- Croods creates five basic endpoints for your resource. If you don't want one, you need to skip it's action:
64
+
65
+ Croods creates five basic endpoints for your resource. If you don't want one, you need to skip its action:
66
+
56
67
  ```ruby
57
68
  module Projects
58
69
  class Resource < ApplicationResource
@@ -60,8 +71,11 @@ module Projects
60
71
  end
61
72
  end
62
73
  ```
74
+
63
75
  ### Skip attributes
76
+
64
77
  By default, every single attribute in your table is exposed in your endpoints. If you don't want this, let croods know:
78
+
65
79
  ```ruby
66
80
  module Projects
67
81
  class Resource < ApplicationResource
@@ -71,7 +85,9 @@ end
71
85
  ```
72
86
 
73
87
  ### Extend model
88
+
74
89
  Croods creates a model for your resource. It looks at your database and automatically infers your model's `belongs_to` associations. But if you need to add code to your model just use `extend_model`.
90
+
75
91
  ```ruby
76
92
  module Projects
77
93
  class Resource < ApplicationResource
@@ -83,6 +99,7 @@ end
83
99
  ```
84
100
 
85
101
  Protip: you can create a Model module and `extend_model { include Projects::Model }`.
102
+
86
103
  ```ruby
87
104
  module Projects
88
105
  module Model
@@ -94,9 +111,12 @@ module Projects
94
111
  end
95
112
  end
96
113
  ```
114
+
97
115
  ### Authentication
116
+
98
117
  Croods uses [devise_token_auth](https://github.com/lynndylanhurley/devise_token_auth) under the hood.
99
118
  To customize which devise modules are loaded, you can pass them as arguments to `use_for_authentication!`
119
+
100
120
  ```ruby
101
121
  use_for_authentication!(
102
122
  :database_authenticatable,
@@ -110,16 +130,15 @@ use_for_authentication!(
110
130
  ## Contributing
111
131
 
112
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.
113
134
 
114
- To set it up:
115
- ```
116
- cd todos/
117
- bin/setup
118
- ```
135
+ - Clone the repository
136
+ - Install bundler `gem install bundler`
137
+ - Install gems `bin/bundle`
119
138
 
120
- To run specs use:
121
- ```bin/rspec```
139
+ To run both Croods-rails' and the example's specs use:
140
+ `bin/rspec`
122
141
 
123
142
  ## License
124
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
125
143
 
144
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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)
@@ -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
@@ -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.5'
4
+ VERSION = '0.2.10'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: croods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.10
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-05-25 00:00:00.000000000 Z
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: committee
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 5.2.4.2
61
+ version: 5.2.4.3
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 5.2.4.2
68
+ version: 5.2.4.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: schema_associations
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: 4.0.0.rc1
159
+ version: 4.0.1
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: 4.0.0.rc1
166
+ version: 4.0.1
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: rspec_junit_formatter
169
169
  requirement: !ruby/object:Gem::Requirement