restme 0.0.39 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66521fd280c2f53800dfece67dc0167d10f25b76a2d435064fdaea3d37f9b260
4
- data.tar.gz: 3e8f3889d228f4e051e3516d23fedb16dacea71393667c9acbf6b897f42c5e6f
3
+ metadata.gz: 20a6deb0be1cd7ca4a0835d7fc1211057a944237a4370ce576f3a6e8bc067738
4
+ data.tar.gz: 60fb44729f7721fc885224b2df4f7dcd3ec7ac07efaeacbee0ccd42dcece913c
5
5
  SHA512:
6
- metadata.gz: c4f29154906b6b23c1d2b2f2b18aa3be16da6c1b6e9d0f0952360eea13c141e05c39a6eaee952d402685e94556a07f2063137cbdcbbabbf8b0046434795e5124
7
- data.tar.gz: 58e3f792a2f85054f3a9ff88ab19bd70fd9ceb6c1a3f9308bbdf403b53a6dab56430bc6ddc9af47d83941ef48cc629e2b4d360b523136282b8f543ca9f996c08
6
+ metadata.gz: 633737ce0a993e839281b86d9a2d686f098a585d43b608aabb1c770b6b4a8f5c75c6faa0480e75c3c5a7d8089c01666aafd7c1d299415c4f0f391f8958ef209a
7
+ data.tar.gz: 9d859f78801920fca33bd80ab075e6f00e0a7d54e098dd5d2f66defa19481d66c23650f5a21b703b6640ed1e09fe2450c549168b63d0f41e9504ca38669038ea
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/restme.svg)](https://badge.fury.io/rb/restme)
4
4
 
5
- Adds support for new **Rails** controller actions such as pagination, filtering, sorting, and selecting specific model fields. Easily implement full CRUD functionality by importing Restme into your controller.
5
+ Adds support for new **Rails/Postgres** controller actions such as pagination, filtering, sorting, and selecting specific model fields. Easily implement full CRUD functionality by importing Restme into your controller.
6
6
 
7
7
  This gem manages your controller's responsibilities for:
8
8
  - Read Actions: Provide complete pagination, filtering, sorting, and field selection for records, all handled through query parameters (e.g., `http://127.0.0.1/products?name_equal=foo`).
@@ -11,13 +11,12 @@ This gem manages your controller's responsibilities for:
11
11
  ## Installation
12
12
 
13
13
 
14
-
14
+ GEMFILE:
15
15
  ```bash
16
- gem install restme
16
+ gem 'restme', '~> 1.0', '>= 1.0.1'
17
17
  ```
18
18
 
19
- OR
20
-
19
+ INSTALL:
21
20
  ```bash
22
21
  gem 'restme'
23
22
  ```
@@ -25,10 +24,7 @@ gem 'restme'
25
24
  ## Usage
26
25
 
27
26
  #### ℹ️ Current Version of gem require the following pré configs
28
-
29
- - If your controller defines an instance variable named `current_user`, Restme will automatically assign it to `model.current_user` during create and update actions—provided your model responds to the `current_user` method.
30
- - Your user model must have a role attribute (user.role).
31
- - Your controllers must be named using the plural form of the model (e.g., Product → ProductsController). Alternatively, you can manually set the model name by defining the MODEL_NAME constant (e.g., MODEL_NAME = "Shopping").
27
+ - Your controllers must be named using the plural form of the model (e.g., Product → ProductsController). Alternatively, you can manually set the model name by defining the MODEL_NAME constant (e.g., MODEL_NAME = "Product").
32
28
  - You must create a folder inside app named restfy to define controller rules for authorization, scoping, creation, updating, and field selection (see example below).
33
29
 
34
30
 
data/docker-compose.yml CHANGED
@@ -17,7 +17,7 @@ services:
17
17
  volumes:
18
18
  - postgres_db:/var/lib/postgresql/data
19
19
  ports:
20
- - "5432:5432"
20
+ - "5442:5432"
21
21
  mem_limit: 512mb
22
22
  restart: unless-stopped
23
23
  environment:
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../shared/user_role"
3
+ require_relative "../shared/restme_current_user_role"
4
4
  require_relative "../shared/current_model"
5
5
 
6
6
  module Restme
@@ -10,16 +10,17 @@ module Restme
10
10
  include ::Restme::Shared::CurrentModel
11
11
  include ::Restme::Shared::UserRole
12
12
 
13
- def user_authorize
14
- return true unless restme_current_user
15
- return authorize_errors unless authorize?
13
+ def user_authorized?
14
+ return true if restme_current_user.blank? || authorize?
16
15
 
17
- true
16
+ authorize_errors
17
+
18
+ false
18
19
  end
19
20
 
20
21
  def authorize?
21
- super_authorize? ||
22
- allowed_roles_actions[action_name.to_sym]&.include?(user_role.to_sym)
22
+ allowed_roles_actions[action_name.to_sym]
23
+ &.include?(restme_current_user_role&.to_sym)
23
24
  end
24
25
 
25
26
  def authorize_errors
@@ -34,17 +35,13 @@ module Restme
34
35
  end
35
36
 
36
37
  def allowed_roles_actions
37
- return {} unless authorize_rules_class.const_defined?(:ALLOWED_ROLES_ACTIONS)
38
+ return {} unless authorize_rules_class&.const_defined?(:ALLOWED_ROLES_ACTIONS)
38
39
 
39
40
  authorize_rules_class::ALLOWED_ROLES_ACTIONS
40
41
  end
41
42
 
42
- def super_authorize?
43
- restme_current_user&.super_admin?
44
- end
45
-
46
43
  def authorize_rules_class
47
- "#{controller_class.to_s.split("::").last}::Authorize::Rules".constantize
44
+ "#{controller_class.to_s.split("::").last}::Authorize::Rules".safe_constantize
48
45
  end
49
46
  end
50
47
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../shared/user_role"
3
+ require_relative "../shared/restme_current_user_role"
4
4
  require_relative "../shared/current_model"
5
5
  require_relative "../shared/controller_params"
6
6
 
@@ -64,13 +64,9 @@ module Restme
64
64
  def createable_scope?
65
65
  return true unless restme_current_user
66
66
 
67
- method_scope = "#{creatable_current_action}_#{user_role}_scope?"
67
+ method_scope = "#{creatable_current_action}_#{restme_current_user_role}_scope?"
68
68
 
69
- createable_super_admin_scope? || create_rules_class.try(method_scope) || false
70
- end
71
-
72
- def createable_super_admin_scope?
73
- restme_current_user.super_admin?
69
+ create_rules_class.try(method_scope) || false
74
70
  end
75
71
 
76
72
  def createable_object_errors_messages
data/lib/restme/restme.rb CHANGED
@@ -18,11 +18,15 @@ module Restme
18
18
  def initialize_restme
19
19
  use_current_user
20
20
 
21
- user_authorize
21
+ restme_authorize_response unless user_authorized?
22
22
  end
23
23
 
24
24
  private
25
25
 
26
+ def restme_authorize_response
27
+ render json: restme_scope_errors, status: restme_scope_status
28
+ end
29
+
26
30
  def use_current_user
27
31
  @restme_current_user = try(:current_user)
28
32
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../shared/user_role"
3
+ require_relative "../shared/restme_current_user_role"
4
4
  require_relative "../shared/current_model"
5
5
  require_relative "../shared/controller_params"
6
6
  require_relative "filter/rules"
@@ -58,7 +58,7 @@ module Restme
58
58
  end
59
59
 
60
60
  def model_scope
61
- @model_scope ||= without_item_in_scope? ? user_scope : custom_scope
61
+ @model_scope ||= custom_scope
62
62
  end
63
63
 
64
64
  def pagination
@@ -79,10 +79,6 @@ module Restme
79
79
  @restme_scope_status ||= status
80
80
  end
81
81
 
82
- def without_item_in_scope?
83
- !user_scope.exists?
84
- end
85
-
86
82
  def custom_scope
87
83
  @filtered_scope = filterable_scope(user_scope)
88
84
  @sorted_scope = sortable_scope(filtered_scope)
@@ -91,11 +87,11 @@ module Restme
91
87
  end
92
88
 
93
89
  def user_scope
94
- @user_scope ||= super_admin_scope || scope_rules_class.try(method_scope) || none_scope
90
+ @user_scope ||= none_user_scope || scope_rules_class.try(method_scope) || none_scope
95
91
  end
96
92
 
97
- def super_admin_scope
98
- klass.all if restme_current_user&.super_admin? || restme_current_user.blank?
93
+ def none_user_scope
94
+ klass.all if restme_current_user.blank?
99
95
  end
100
96
 
101
97
  def none_scope
@@ -103,7 +99,7 @@ module Restme
103
99
  end
104
100
 
105
101
  def method_scope
106
- "#{user_role}_scope"
102
+ "#{restme_current_user_role}_scope"
107
103
  end
108
104
 
109
105
  def scope_rules_class
@@ -4,8 +4,8 @@ module Restme
4
4
  module Shared
5
5
  # Returns the roles associated with the user, if any exist.
6
6
  module UserRole
7
- def user_role
8
- restme_current_user&.role
7
+ def restme_current_user_role
8
+ restme_current_user&.try(:role)
9
9
  end
10
10
  end
11
11
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../shared/user_role"
3
+ require_relative "../shared/restme_current_user_role"
4
4
  require_relative "../shared/current_model"
5
5
  require_relative "../shared/controller_params"
6
6
 
@@ -74,13 +74,9 @@ module Restme
74
74
  def updateable_scope?
75
75
  return true unless restme_current_user
76
76
 
77
- method_scope = "#{updateable_current_action}_#{user_role}_scope?"
77
+ method_scope = "#{updateable_current_action}_#{restme_current_user_role}_scope?"
78
78
 
79
- updateable_super_admin_scope? || update_rules_class.try(method_scope) || false
80
- end
81
-
82
- def updateable_super_admin_scope?
83
- restme_current_user&.super_admin?
79
+ update_rules_class.try(method_scope) || false
84
80
  end
85
81
 
86
82
  def updateable_record_errors_messages
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Restme
4
- VERSION = "0.0.39"
4
+ VERSION = "1.0.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.39
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - everson-ever
@@ -45,7 +45,7 @@ files:
45
45
  - lib/restme/scope/sort/rules.rb
46
46
  - lib/restme/shared/controller_params.rb
47
47
  - lib/restme/shared/current_model.rb
48
- - lib/restme/shared/user_role.rb
48
+ - lib/restme/shared/restme_current_user_role.rb
49
49
  - lib/restme/update/rules.rb
50
50
  - lib/restme/version.rb
51
51
  - sig/restme.rbs