restme 0.0.38 → 1.0.1

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: 65794fd40e07c3dad593d15b93cbdfd860dfed7dbc4efcf960c410eedc9bd7ec
4
- data.tar.gz: d688961f322dc7caabc5e6c7c28e362a237097e7fababbcf7f3c40bf45cd8582
3
+ metadata.gz: 34e6f83a9517ad4596b91a677ea60bc4520a6d074684ba31069a33967252e520
4
+ data.tar.gz: 94a3e5fee24cd500c7b1904b048f76df2831f22363a463c1273603f56e996fde
5
5
  SHA512:
6
- metadata.gz: 7c67210649fc7511aa4150f14fbf262c76357d667c05c541fe7f0850c8ca7570f6c503d98f6109ffc5ef15be34b875d23a120b528c6c4edce73e1b620c032099
7
- data.tar.gz: 1a6eb52d9fbe9f2d6c4b568f4b6d80ee12f22693698bd993b9175c09f946ef4943b063b218a6a1f5aeb868f86c6e25ca47aa0898a4c4fdca67542bf57c1c6633
6
+ metadata.gz: 42e31bc636a89203a07f9b068ba6916cf6b1df32e978d01bbc01a1f4168c825137c367a5bc6d73c3584b485f8768f32184a2eb026ccd5b78a22f5dcea1a08e74
7
+ data.tar.gz: 4fc46af681505a061ea78d606410b8dcf7bc490ccf34deae1ec07f9ad36382bd232c9e550781a8bc55d67012ab8af104d0efa56f38ee0462e3fe6261613be1d1
data/.env.example ADDED
@@ -0,0 +1 @@
1
+ DATABASE_HOST=restme_postgres
data/.rubocop.yml CHANGED
@@ -1,8 +1,14 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 3.0
3
+ SuggestExtensions: false
4
+ NewCops: enable
3
5
 
4
6
  Style/StringLiterals:
5
7
  EnforcedStyle: double_quotes
6
8
 
7
9
  Style/StringLiteralsInInterpolation:
8
10
  EnforcedStyle: double_quotes
11
+
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - spec/lib/restme_controller_spec.rb
data/README.md CHANGED
@@ -19,7 +19,7 @@ gem install restme
19
19
  OR
20
20
 
21
21
  ```bash
22
- gem 'restme', '~> 0.0.37'
22
+ gem 'restme'
23
23
  ```
24
24
 
25
25
  ## Usage
data/docker-compose.yml CHANGED
@@ -9,5 +9,25 @@ services:
9
9
  volumes:
10
10
  - .:/var/www/restme
11
11
  mem_limit: 1024mb
12
+ depends_on:
13
+ - restme_postgres
14
+ restme_postgres:
15
+ image: postgres:15.8-alpine
16
+ container_name: restme_postgres
17
+ volumes:
18
+ - postgres_db:/var/lib/postgresql/data
19
+ ports:
20
+ - "5442:5432"
21
+ mem_limit: 512mb
22
+ restart: unless-stopped
23
+ environment:
24
+ - POSTGRES_PASSWORD=postgres
25
+ - POSTGRES_USERNAME=postgres
26
+ healthcheck:
27
+ test: ["CMD-SHELL", "pg_isready -U postgres"]
28
+ interval: 5s
29
+ timeout: 5s
30
+ retries: 5
12
31
  volumes:
13
32
  app:
33
+ postgres_db:
@@ -10,11 +10,28 @@ 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 if super_authorize? || allowed_roles_actions[action_name.to_sym]&.include?(user_role.to_sym)
13
+ def user_authorized?
14
+ return true if restme_current_user.blank? || authorize?
16
15
 
17
- render json: { message: "Action not allowed" }, status: :forbidden
16
+ authorize_errors
17
+
18
+ false
19
+ end
20
+
21
+ def authorize?
22
+ super_authorize? ||
23
+ allowed_roles_actions[action_name.to_sym]&.include?(user_role.to_sym)
24
+ end
25
+
26
+ def authorize_errors
27
+ restme_scope_errors(
28
+ {
29
+ message: "Action not allowed",
30
+ body: {}
31
+ }
32
+ )
33
+
34
+ restme_scope_status(:forbidden)
18
35
  end
19
36
 
20
37
  def allowed_roles_actions
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
@@ -10,8 +10,6 @@ module Restme
10
10
  include Restme::Scope::Field::Attachable
11
11
 
12
12
  def fieldable_scope(user_scope)
13
- unallowed_select_fields_error
14
-
15
13
  return user_scope unless select_any_field?
16
14
 
17
15
  scoped = user_scope
@@ -72,12 +70,14 @@ module Restme
72
70
  end&.map(&:to_sym)
73
71
  end
74
72
 
75
- def unallowed_select_fields_error
73
+ def unallowed_select_fields_errors
76
74
  return if unallowed_fields_selected.blank?
77
75
 
78
76
  restme_scope_errors({ body: unallowed_fields_selected, message: "Selected not allowed fields" })
79
77
 
80
78
  restme_scope_status(:bad_request)
79
+
80
+ true
81
81
  end
82
82
 
83
83
  def unallowed_fields_selected
@@ -36,16 +36,24 @@ module Restme
36
36
  private
37
37
 
38
38
  def filterable_scope(user_scope)
39
+ @user_scope = user_scope
40
+
39
41
  return user_scope unless filterable_scope?
40
- return user_scope if unallowed_filter_fields_errors
41
-
42
- next_scope = where_equal(user_scope)
43
- next_scope = where_like(next_scope)
44
- next_scope = where_bigger_than(next_scope)
45
- next_scope = where_less_than(next_scope)
46
- next_scope = where_bigger_than_or_equal_to(next_scope)
47
- next_scope = where_less_than_or_equal_to(next_scope)
48
- where_in(next_scope)
42
+ return user_scope if record_not_found_errors
43
+
44
+ processed_scope
45
+ end
46
+
47
+ def processed_scope
48
+ @processed_scope ||= begin
49
+ next_scope = where_equal(@user_scope)
50
+ next_scope = where_like(next_scope)
51
+ next_scope = where_bigger_than(next_scope)
52
+ next_scope = where_less_than(next_scope)
53
+ next_scope = where_bigger_than_or_equal_to(next_scope)
54
+ next_scope = where_less_than_or_equal_to(next_scope)
55
+ where_in(next_scope)
56
+ end
49
57
  end
50
58
 
51
59
  def allowed_fields
@@ -55,7 +63,6 @@ module Restme
55
63
  end
56
64
 
57
65
  record_field = param_key.to_s.gsub("_#{filter_type}", "")&.to_sym
58
-
59
66
  next unless filter_type
60
67
  next unless filteable_fields.include?(record_field)
61
68
 
@@ -74,8 +81,6 @@ module Restme
74
81
  end
75
82
 
76
83
  def filterable_scope?
77
- try_insert_id_equal
78
-
79
84
  request.get? && controller_params_filters_fields.present?
80
85
  end
81
86
 
@@ -86,6 +91,8 @@ module Restme
86
91
  end
87
92
 
88
93
  def unallowed_filter_fields_errors
94
+ try_insert_id_equal
95
+
89
96
  return unless unallowed_fields_to_filter.present?
90
97
 
91
98
  restme_scope_errors(
@@ -100,6 +107,16 @@ module Restme
100
107
  true
101
108
  end
102
109
 
110
+ def record_not_found_errors
111
+ return if params[:id].blank? || processed_scope.exists?
112
+
113
+ restme_scope_errors({ body: { id: params[:id] }, message: "Record not found" })
114
+
115
+ restme_scope_status(:not_found)
116
+
117
+ true
118
+ end
119
+
103
120
  def unallowed_fields_to_filter
104
121
  @unallowed_fields_to_filter ||= controller_params_filters_fields - allowed_fields
105
122
  end
@@ -10,8 +10,6 @@ module Restme
10
10
  MAX_PER_PAGE = ENV.fetch("PAGINATION_MAX_PER_PAGE", 100)
11
11
 
12
12
  def paginable_scope(user_scope)
13
- return user_scope if per_page_error
14
-
15
13
  user_scope.limit(per_page).offset(paginate_offset)
16
14
  end
17
15
 
@@ -35,7 +33,7 @@ module Restme
35
33
  (page_no - 1) * per_page
36
34
  end
37
35
 
38
- def per_page_error
36
+ def per_page_errors
39
37
  return if per_page <= MAX_PER_PAGE
40
38
 
41
39
  restme_scope_errors(
@@ -23,20 +23,24 @@ module Restme
23
23
  include ::Restme::Shared::UserRole
24
24
 
25
25
  attr_reader :filtered_scope, :sorted_scope, :paginated_scope, :fieldated_scope
26
- attr_accessor :restme_scope_errors, :restme_scope_status
26
+ attr_writer :restme_scope_errors, :restme_scope_status
27
27
 
28
28
  def pagination_response
29
29
  @pagination_response ||= restme_response
30
30
  end
31
31
 
32
32
  def model_scope_object
33
- @model_scope_object ||= model_scope.first
33
+ @model_scope_object ||= begin
34
+ model_scope unless any_scope_errors.present?
35
+
36
+ restme_scope_errors.presence || model_scope.first
37
+ end
34
38
  end
35
39
 
36
40
  private
37
41
 
38
42
  def restme_response
39
- model_scope
43
+ any_scope_errors
40
44
 
41
45
  restme_scope_errors.presence || {
42
46
  objects: model_scope,
@@ -44,8 +48,17 @@ module Restme
44
48
  }
45
49
  end
46
50
 
51
+ def any_scope_errors
52
+ per_page_errors
53
+ unknown_sortable_fields_errors
54
+ unallowed_filter_fields_errors
55
+ unallowed_select_fields_errors
56
+
57
+ restme_scope_errors
58
+ end
59
+
47
60
  def model_scope
48
- @model_scope ||= without_item_in_scope? ? user_scope : custom_scope
61
+ @model_scope ||= custom_scope
49
62
  end
50
63
 
51
64
  def pagination
@@ -57,17 +70,15 @@ module Restme
57
70
  end
58
71
 
59
72
  def restme_scope_errors(error = nil)
60
- @restme_scope_errors ||= error
73
+ @restme_scope_errors ||= []
74
+ @restme_scope_errors << error if error.present?
75
+ @restme_scope_errors
61
76
  end
62
77
 
63
78
  def restme_scope_status(status = :ok)
64
79
  @restme_scope_status ||= status
65
80
  end
66
81
 
67
- def without_item_in_scope?
68
- !user_scope.exists?
69
- end
70
-
71
82
  def custom_scope
72
83
  @filtered_scope = filterable_scope(user_scope)
73
84
  @sorted_scope = sortable_scope(filtered_scope)
@@ -11,7 +11,6 @@ module Restme
11
11
 
12
12
  def sortable_scope(user_scope)
13
13
  return user_scope unless sortable_scope?
14
- return user_scope if unknown_sortable_fields_errors
15
14
 
16
15
  user_scope.order(serialize_sort_params)
17
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Restme
4
- VERSION = "0.0.38"
4
+ VERSION = "1.0.1"
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.38
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - everson-ever
@@ -16,6 +16,7 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - ".env.example"
19
20
  - ".rspec"
20
21
  - ".rubocop.yml"
21
22
  - CHANGELOG.md
@@ -55,6 +56,7 @@ metadata:
55
56
  homepage_uri: https://github.com/everson-ever/restme
56
57
  source_code_uri: https://github.com/everson-ever/restme
57
58
  changelog_uri: https://github.com/everson-ever/restme/blob/main/CHANGELOG.md
59
+ rubygems_mfa_required: 'true'
58
60
  rdoc_options: []
59
61
  require_paths:
60
62
  - lib