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 +4 -4
- data/README.md +12 -3
- data/lib/restme/scope/field/attachable.rb +10 -8
- data/lib/restme/scope/field/rules.rb +10 -8
- data/lib/restme/scope/rules.rb +1 -0
- data/lib/restme/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e2516a111b171a33790f33c9d1eb3e8b86235ee1098355fd6255874f7c3da4ee
|
|
4
|
+
data.tar.gz: 80480f4119cd76fb00617260768ca3bbb3bc4981cd2529a8c170e6b6d82b9460
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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 ||=
|
|
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".
|
|
113
|
+
"#{controller_class.to_s.split("::").last}::Field::Rules".safe_constantize
|
|
112
114
|
end
|
|
113
115
|
end
|
|
114
116
|
end
|
data/lib/restme/scope/rules.rb
CHANGED
data/lib/restme/version.rb
CHANGED