restme 1.1.1 → 1.2.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: cccc4f918f72719fedda5f137e14cdccf49de3931209a7adf2e9f7d7a974068c
4
- data.tar.gz: 0e3a7821d6883250e28bb75b5f6ca50841166f6fff2a180740daf8f59dd323df
3
+ metadata.gz: 9a2a5ee870751a00ca5907e773e8d1aaf34fefd68c66bdcbbd42a0809d38ca19
4
+ data.tar.gz: 8ef1ff2ab867c127615d53fa903808cc5579f961204cc9ff9306f217f1c7e4ae
5
5
  SHA512:
6
- metadata.gz: ef683d119c129847df93624eb7f5480db6ec8bb190fe2aff432c59bb228aecec0a699ce1eb9cd91b23e73888fb1d03abe6c01accf1fe278d5ffab57ef51963f3
7
- data.tar.gz: bcfefa780c6c4303e47b85492cd80ca51d404c83c8f68976bb0750a949236c0ccc45cd2278a4741747fa877a45b87e418cee46f6675c212e2a80f6572c6ec0bf
6
+ metadata.gz: 062df3cf4301fad020426e57ee07eb373ab2751c03f6aed5928e32db4988f8283c4e20254c993515d617dbb4b1e581eae2492aad4aecb0fd2610b0cc38c3afbe
7
+ data.tar.gz: ade0cf78bcce6e49a3ecdeb2eb54513c7761c28bce0df20cdd1630bf61393962b68eb9394bdb6eb5e20c9ff04d619ad94932de1a3218ef1578321ca0ecce9fa0
data/README.md CHANGED
@@ -8,12 +8,15 @@ 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`).
9
9
  - Create/Update Actions: Enabling automatic creation and updating of records.
10
10
 
11
+ ## ℹ️ This doc reference latest version of Restme
12
+
13
+
11
14
  ## Installation
12
15
 
13
16
 
14
17
  GEMFILE:
15
18
  ```bash
16
- gem 'restme', '~> 1.1'
19
+ gem 'restme', '~> 1.2'
17
20
  ```
18
21
 
19
22
  INSTALL:
@@ -207,15 +210,10 @@ This rule defines which nested_fields are selectable (nested fields are model re
207
210
  module ProductsController::Field
208
211
  class Rules
209
212
  NESTED_SELECTABLE_FIELDS = {
210
- unit: {
211
- table_name: :units
212
- },
213
- establishment: {
214
- table_name: :establishments
215
- },
216
- category: {
217
- table_name: :categories
218
- }
213
+ unit: {},
214
+ establishment: {},
215
+ category: {},
216
+ producer: {}
219
217
  }.freeze
220
218
  end
221
219
  end
@@ -360,6 +358,15 @@ Example:
360
358
 
361
359
  ```bash
362
360
  http://localhost:3000/api/v1/products?attachment_fields_select=image
361
+
362
+ {
363
+ "products": [
364
+ {
365
+ "id": 1,
366
+ "image_url": "http://localhost/image.png", // The field includes the `_url` suffix
367
+ }
368
+ ]
369
+ }
363
370
  ```
364
371
 
365
372
  <br><br>
@@ -8,19 +8,35 @@ module Restme
8
8
  def insert_attachments(scope)
9
9
  unallowed_attachment_fields_error
10
10
 
11
- return scope.uniq if attachment_fields_select.blank?
11
+ return scope.as_json(json_options) if attachment_fields_select.blank?
12
12
 
13
- scope = scope.includes(attachment_fields_select_includes).uniq
13
+ define_attachment_methods
14
14
 
15
- scope.map do |record|
16
- attachment_fields_select.each do |field|
17
- @record = record.as_json.merge({ "#{field}": record.send(field).url })
18
- end
15
+ scope.includes(attachment_fields_select_includes)
16
+ .as_json(json_options)
17
+ end
18
+
19
+ def json_options
20
+ {
21
+ include: valid_nested_fields_select,
22
+ methods: attachment_methods
23
+ }
24
+ end
19
25
 
20
- @record
26
+ def define_attachment_methods
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
32
+ end
21
33
  end
22
34
  end
23
35
 
36
+ def attachment_methods
37
+ attachment_fields_select&.map { |field| "#{field}_url" }
38
+ end
39
+
24
40
  def attachment_fields_select_includes
25
41
  attachment_fields_select.map { |field| { "#{field}_attachment": :blob } }
26
42
  end
@@ -22,23 +22,13 @@ module Restme
22
22
  end
23
23
 
24
24
  def select_nested_scope(scoped)
25
- scoped.joins(nested_fields_joins)
26
- .left_joins(nested_fields_left_joins)
27
- .preload(valid_nested_fields_select)
28
- .select(nesteds_table)
25
+ scoped.preload(valid_nested_fields_select)
29
26
  end
30
27
 
31
28
  def select_any_field?
32
29
  fields_select || nested_fields_select || attachment_fields_select
33
30
  end
34
31
 
35
- def nesteds_table
36
- valid_nested_fields_select&.map do |field|
37
- table = nested_selectable_fields_keys.dig(field, :table_name)
38
- table.present? ? "#{table}::text AS #{field}" : nil
39
- end
40
- end
41
-
42
32
  def model_fields_select
43
33
  @model_fields_select ||= begin
44
34
  fields = fields_select&.split(",")
@@ -51,22 +41,10 @@ module Restme
51
41
  @model_attributes ||= klass.new.attributes.keys
52
42
  end
53
43
 
54
- def nested_fields_joins
55
- @nested_fields_joins ||= valid_nested_fields_select.select do |field|
56
- nested_selectable_fields_keys[field.to_sym][:join_type].blank?
57
- end
58
- end
59
-
60
- def nested_fields_left_joins
61
- @nested_fields_left_joins ||= valid_nested_fields_select.select do |field|
62
- nested_selectable_fields_keys[field.to_sym][:join_type] == :left_joins
63
- end
64
- end
65
-
66
44
  def valid_nested_fields_select
67
45
  @valid_nested_fields_select ||=
68
46
  nested_fields_select&.split(",")&.select do |field|
69
- nested_selectable_fields_keys[field.to_sym].present?
47
+ nested_selectable_fields_keys.key?(field.to_sym)
70
48
  end&.map(&:to_sym)
71
49
  end
72
50
 
@@ -39,7 +39,7 @@ module Restme
39
39
  @user_scope = user_scope
40
40
 
41
41
  return user_scope unless filterable_scope?
42
- return user_scope if record_not_found_errors
42
+ return none_scope if record_not_found_errors
43
43
 
44
44
  processed_scope
45
45
  end
@@ -22,11 +22,18 @@ module Restme
22
22
  include ::Restme::Shared::CurrentModel
23
23
  include ::Restme::Shared::RestmeCurrentUserRole
24
24
 
25
- attr_reader :filtered_scope, :sorted_scope, :paginated_scope, :fieldated_scope
25
+ attr_reader :sortable_scope_response, :paginable_scope_response
26
26
  attr_writer :restme_scope_errors, :restme_scope_status
27
27
 
28
+ SCOPE_ERROR_METHODS = %i[
29
+ per_page_errors
30
+ unknown_sortable_fields_errors
31
+ unallowed_filter_fields_errors
32
+ unallowed_select_fields_errors
33
+ ].freeze
34
+
28
35
  def pagination_response
29
- @pagination_response ||= restme_response
36
+ @pagination_response ||= restme_pagination_response
30
37
  end
31
38
 
32
39
  def model_scope_object
@@ -39,7 +46,7 @@ module Restme
39
46
 
40
47
  private
41
48
 
42
- def restme_response
49
+ def restme_pagination_response
43
50
  any_scope_errors
44
51
 
45
52
  restme_scope_errors.presence || {
@@ -49,10 +56,7 @@ module Restme
49
56
  end
50
57
 
51
58
  def any_scope_errors
52
- per_page_errors
53
- unknown_sortable_fields_errors
54
- unallowed_filter_fields_errors
55
- unallowed_select_fields_errors
59
+ SCOPE_ERROR_METHODS.each { |m| send(m) }
56
60
 
57
61
  restme_scope_errors
58
62
  end
@@ -64,8 +68,8 @@ module Restme
64
68
  def pagination
65
69
  {
66
70
  page: page_no,
67
- pages: pages(filtered_scope),
68
- total_items: total_items(filtered_scope)
71
+ pages: pages(filterable_scope_response),
72
+ total_items: total_items(filterable_scope_response)
69
73
  }
70
74
  end
71
75
 
@@ -80,10 +84,16 @@ module Restme
80
84
  end
81
85
 
82
86
  def custom_scope
83
- @filtered_scope = filterable_scope(user_scope)
84
- @sorted_scope = sortable_scope(filtered_scope)
85
- @paginated_scope = paginable_scope(sorted_scope)
86
- @fieldated_scope = fieldable_scope(paginated_scope)
87
+ return filterable_scope_response if filterable_scope_response.blank?
88
+
89
+ @sortable_scope_response = sortable_scope(filterable_scope_response)
90
+ @paginable_scope_response = paginable_scope(sortable_scope_response)
91
+
92
+ fieldable_scope(paginable_scope_response)
93
+ end
94
+
95
+ def filterable_scope_response
96
+ @filterable_scope_response ||= filterable_scope(user_scope)
87
97
  end
88
98
 
89
99
  def user_scope
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Restme
4
- VERSION = "1.1.1"
4
+ VERSION = "1.2.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: 1.1.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - everson-ever