tam_select 0.1.0 → 0.1.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: 6cf23ba08878f41cb17355d5ce629542fc3bab8e61756960403223d2abde898c
4
- data.tar.gz: 9257189f40b467cfdf80106c5c2c30ce3430f10395c8bb122b7c6ae09ee6a3c6
3
+ metadata.gz: b3a066f1e145b6a880f28494f78c00d51c1ca280fc984975c2eebf849704041c
4
+ data.tar.gz: 1537eeda1e3dda9a3a38077bbfe6076cc9d851e6b404345188b61b845fd3107a
5
5
  SHA512:
6
- metadata.gz: 8c2ac3a785840329c378213fa5fdf9499756c44adc64788c6170ac5fc8f8a2de2c18eb7bd8949dbf6cd9792894ef7b5ca75e5b1135066d31052de62598e67510
7
- data.tar.gz: 326c67bbe68fc22799b6a24993bd378ebb06650772d829ffdba775fe2f119997df09a66bb63c99b1661cc85e21b3e29a3f3662b2b71fa57789fb997fc126e368
6
+ metadata.gz: 454ff7b7e777da2409741dff572ead04fc84ae5e65be2984c44d9c0196924ecf6d695e7445d23cbb7ba683abea8bf7e821f79e06cac71da9e4c8c6a718f8c823
7
+ data.tar.gz: f5950761f9e589454f4dad82e2ed52dbe5ac88d5856a8ee133da81916bf5e5ec20c8e434e23aca8a868b8d9cce4612d5926929e1ef4643e9c4d597541c3b943e
data/README.md CHANGED
@@ -35,6 +35,7 @@ app/javascript/tam_select/tam_select.js
35
35
  app/javascript/controllers/tam_select_controller.js
36
36
  app/inputs/tam_select_input.rb
37
37
  app/controllers/concerns/tam_select_paginatable.rb
38
+ app/controllers/tam_select_remote_controller.rb
38
39
  app/helpers/tam_select_helper.rb
39
40
  ```
40
41
 
@@ -153,6 +154,47 @@ The generator installs `TamSelectPaginatable` as a Rails response helper. A comp
153
154
 
154
155
  Secure remote endpoints exactly like other Rails JSON endpoints. Scope records by the current user's permissions and never trust a submitted value merely because it appeared in the dropdown.
155
156
 
157
+ ### Generic remote controller
158
+
159
+ The installer generates `TamSelectRemoteController`. Create a small subclass for each allowed remote source:
160
+
161
+ ```ruby
162
+ # app/controllers/region_options_controller.rb
163
+ class RegionOptionsController < TamSelectRemoteController
164
+ tam_select model: Region, label: :name, search_by: %i[name code]
165
+
166
+ private
167
+
168
+ # Override this method when records require authorization or tenant scoping.
169
+ def tam_select_scope(config)
170
+ current_account.regions.order(:name)
171
+ end
172
+ end
173
+ ```
174
+
175
+ Expose the JSON endpoint and pass it to the input:
176
+
177
+ ```ruby
178
+ # config/routes.rb
179
+ get "region_options", to: "region_options#index", defaults: { format: :json }
180
+ ```
181
+
182
+ ```erb
183
+ <%= form.input :region_id,
184
+ as: :tam_select,
185
+ collection: [form.object.region].compact,
186
+ label_method: :name,
187
+ value_method: :id,
188
+ input_html: {
189
+ tam_options: {
190
+ remoteUrl: region_options_path,
191
+ minQueryLength: 1
192
+ }
193
+ } %>
194
+ ```
195
+
196
+ Do not accept a model name from request parameters. Declaring each source in a controller subclass prevents clients from querying arbitrary application models.
197
+
156
198
  ## Core JavaScript
157
199
 
158
200
  ```js
@@ -20,6 +20,10 @@ module TamSelect
20
20
  copy_file "lib/generators/tam_select/templates/tam_select_paginatable.rb", "app/controllers/concerns/tam_select_paginatable.rb"
21
21
  end
22
22
 
23
+ def copy_remote_controller
24
+ copy_file "lib/generators/tam_select/templates/tam_select_remote_controller.rb", "app/controllers/tam_select_remote_controller.rb"
25
+ end
26
+
23
27
  def copy_helper
24
28
  copy_file "lib/generators/tam_select/templates/tam_select_helper.rb", "app/helpers/tam_select_helper.rb"
25
29
  end
@@ -0,0 +1,44 @@
1
+ class TamSelectRemoteController < ApplicationController
2
+ include TamSelectPaginatable
3
+
4
+ class_attribute :tam_select_config, instance_writer: false
5
+
6
+ def self.tam_select(model:, label:, value: :id, search_by: nil, per_page: 20)
7
+ self.tam_select_config = {
8
+ model: model,
9
+ label: label,
10
+ value: value,
11
+ search_by: Array(search_by || label),
12
+ per_page: per_page
13
+ }.freeze
14
+ end
15
+
16
+ def index
17
+ config = self.class.tam_select_config
18
+ raise "Configure #{self.class.name} with tam_select(...)" unless config
19
+
20
+ records = search(tam_select_scope(config), config)
21
+ render json: tam_select_payload(
22
+ records,
23
+ label: config[:label],
24
+ value: config[:value],
25
+ per_page: config[:per_page]
26
+ )
27
+ end
28
+
29
+ private
30
+
31
+ def tam_select_scope(config)
32
+ config[:model].all
33
+ end
34
+
35
+ def search(records, config)
36
+ query = params[:q].to_s.strip
37
+ return records if query.blank?
38
+
39
+ model = config[:model]
40
+ pattern = "%#{model.sanitize_sql_like(query)}%"
41
+ predicates = config[:search_by].map { |field| model.arel_table[field].matches(pattern) }
42
+ records.where(predicates.reduce(&:or))
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module TamSelect
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tam_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tamiru Hailu
@@ -62,6 +62,7 @@ files:
62
62
  - lib/generators/tam_select/templates/tam_select_helper.rb
63
63
  - lib/generators/tam_select/templates/tam_select_input.rb
64
64
  - lib/generators/tam_select/templates/tam_select_paginatable.rb
65
+ - lib/generators/tam_select/templates/tam_select_remote_controller.rb
65
66
  - lib/tam_select.rb
66
67
  - lib/tam_select/engine.rb
67
68
  - lib/tam_select/version.rb