restme 1.3.0 → 1.3.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: a2993fdfed0cd616219142038db44085a76bd4f24e10f4ea758947527613a8d5
4
- data.tar.gz: 8fa26f48ba3c9fab9bb72d95217e809245b8e0c22aa8aabee009a5aa9d340c60
3
+ metadata.gz: e2516a111b171a33790f33c9d1eb3e8b86235ee1098355fd6255874f7c3da4ee
4
+ data.tar.gz: 80480f4119cd76fb00617260768ca3bbb3bc4981cd2529a8c170e6b6d82b9460
5
5
  SHA512:
6
- metadata.gz: f5b0089277d026cd4580caabe24402c690db46ba2631cdb12c55f4d51a8166a2afc728f82200a6c8c6fabb49b9b3077d163d6aac9697a2aa1d791dbddacb9115
7
- data.tar.gz: 0d3525d0dbb9a0795396a96e681890698b3ba4bb6e646732880218b5aa378e00714f3125d54e6d656cc9c3f6242fa8aaa14f3c61451b936fac308d7ac0f25ebe
6
+ metadata.gz: 0321eb54e6fc00497b0d8b9edd93dc330b8baba2c902f1dae62b1fd9290634935344529fa3472c888145f173f52d74638a36cf7cd8f782b2b93caf0e2acc0d3f
7
+ data.tar.gz: fb28214a0996952bf2dab3bad2f405a18c482f4d3da32867dc3f1e981ee8b051ca624862b91795a3026ae980ef2327526b1e4b61fa2bc19787217bc051309546
data/README.md CHANGED
@@ -28,7 +28,7 @@ gem 'restme'
28
28
 
29
29
  #### ℹ️ Current Version of gem require the following pré configs
30
30
  - 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").
31
- - You must create a folder inside app named restfy to define controller rules for authorization, scoping, creation, updating, and field selection (see example below).
31
+ - You must create a folder inside app named restme to define controller rules for authorization, scoping, creation, updating, and field selection (see example below).
32
32
 
33
33
 
34
34
  <br>
@@ -239,6 +239,8 @@ module ProductsController::Field
239
239
  # These fields are always included in the response.
240
240
  MODEL_FIELDS_SELECT = %i[id].freeze
241
241
 
242
+ UNALLOWED_MODEL_FIELDS_SELECT = %i[internal_code].freeze
243
+
242
244
  NESTED_SELECTABLE_FIELDS = {
243
245
  unit: {},
244
246
  establishment: {},
@@ -329,8 +331,8 @@ http://localhost:3000/api/v1/products?per_page=12&page=1
329
331
 
330
332
  #### Field Selection (`fields_select`)
331
333
 
332
- You can select specific fields from your model, such as `id`, `name`, or `created_at`.
333
- The resulting query will retrieve **only** the selected fields directly from the database, improving performance.
334
+ You can select specific fields from your model, such as `id`, `name`, or `created_at`.
335
+ The generated query will retrieve only the selected fields directly from the database, reducing unnecessary data loading and improving performance.
334
336
 
335
337
  Example:
336
338
 
@@ -338,6 +340,13 @@ Example:
338
340
  http://localhost:3000/api/v1/products?fields_select=id,name
339
341
  ```
340
342
 
343
+ If you need to prevent a specific field from being returned in the response — even when it is selected — simply override the as_json method in the corresponding model:
344
+ ```ruby
345
+ def as_json(options = {})
346
+ super({ except: [:name] }.merge(options))
347
+ end
348
+ ```
349
+
341
350
  <br><br>
342
351
 
343
352
 
@@ -25,10 +25,13 @@ module Restme
25
25
 
26
26
  def define_attachment_methods
27
27
  attachment_fields_select.each do |attachment_field_name|
28
- klass.class_eval do
29
- define_method(:"#{attachment_field_name}_url") do
30
- send(attachment_field_name).url
31
- end
28
+ method_name = "#{attachment_field_name}_url"
29
+
30
+ next if klass.method_defined?(method_name)
31
+
32
+ klass.define_method(method_name) do
33
+ attachment = public_send(attachment_field_name)
34
+ attachment&.url
32
35
  end
33
36
  end
34
37
  end
@@ -50,10 +53,9 @@ module Restme
50
53
  def unallowed_attachment_fields_error
51
54
  return if unallowed_attachment_fields.blank?
52
55
 
53
- render json: {
54
- body: unallowed_attachment_fields,
55
- message: "Selected not allowed attachment fields"
56
- }, status: :bad_request
56
+ restme_scope_errors({ body: unallowed_attachment_fields, message: "Selected not allowed attachment fields" })
57
+
58
+ restme_scope_status(:bad_request)
57
59
  end
58
60
 
59
61
  def unallowed_attachment_fields
@@ -38,21 +38,23 @@ module Restme
38
38
  end
39
39
 
40
40
  def select_selected_fields
41
- @select_selected_fields ||= begin
42
- fields = defined_fields_select | fields_select.split(",")
43
-
44
- fields.map { |field| "#{klass.table_name}.#{field}" }.join(",")
45
- end
41
+ @select_selected_fields ||= defined_fields_select | fields_select.split(",").map(&:to_s)
46
42
  end
47
43
 
48
44
  def model_attributes
49
- @model_attributes ||= klass.attribute_names
45
+ @model_attributes ||= klass.attribute_names - unallowed_model_fields_select
50
46
  end
51
47
 
52
48
  def defined_fields_select
53
49
  return [] unless field_class_rules&.const_defined?(:MODEL_FIELDS_SELECT)
54
50
 
55
- field_class_rules::MODEL_FIELDS_SELECT || []
51
+ (field_class_rules::MODEL_FIELDS_SELECT || []).map(&:to_s)
52
+ end
53
+
54
+ def unallowed_model_fields_select
55
+ return [] unless field_class_rules&.const_defined?(:UNALLOWED_MODEL_FIELDS_SELECT)
56
+
57
+ (field_class_rules::UNALLOWED_MODEL_FIELDS_SELECT || []).map(&:to_s)
56
58
  end
57
59
 
58
60
  def valid_nested_fields_select
@@ -108,7 +110,7 @@ module Restme
108
110
  end
109
111
 
110
112
  def field_class_rules
111
- "#{controller_class.to_s.split("::").last}::Field::Rules".constantize
113
+ "#{controller_class.to_s.split("::").last}::Field::Rules".safe_constantize
112
114
  end
113
115
  end
114
116
  end
@@ -30,6 +30,7 @@ module Restme
30
30
  unknown_sortable_fields_errors
31
31
  unallowed_filter_fields_errors
32
32
  unallowed_select_fields_errors
33
+ unallowed_attachment_fields_error
33
34
  ].freeze
34
35
 
35
36
  def pagination_response
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Restme
4
- VERSION = "1.3.0"
4
+ VERSION = "1.3.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: 1.3.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - everson-ever