rails_notion_like_multiselect 0.1.1 → 0.2.0

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: eba5478c36b1c52c1ad52b5b3fecb134f13f129a7fb73528f0df01f1a3a088ba
4
- data.tar.gz: 20d97d8df49ddcb71b1b773959cd3f863b784bea06343b20b2ea5b33ee0eae2c
3
+ metadata.gz: e99bd8d6c2dd51ca270fa203e6f7ef9dbf7f5dee8e651de45127e548f9e18ef7
4
+ data.tar.gz: 6cb8d559d809f3ee124eb28dd5511f57ba98d4452fe74b720198029ec5ffc61e
5
5
  SHA512:
6
- metadata.gz: 84346ab6b7bf5a8389e89abb6a6b1660837c3da90a48e0dd34045f110fc841a0f63e80a205d75996f51dc7288668ab02e3a2f57652e8f0e3f13de02696966161
7
- data.tar.gz: 95fed342d1ff2045deec03ec2e2c292284e26ce1108fd37f05dc41fd01f87c3c4147a1e63bc1bb6d880dd4762a7fda721aae7838b23984620effc71e61359b05
6
+ metadata.gz: ad3946fdb3dd98c00be6a078dfede6d8f3d68526929dfd3baab054f99fa98a60617b26486ca9587f6ab16fd06832222e9f69f67a7d8c60139956cc7a1e9e77b7
7
+ data.tar.gz: 704516ab3dcb262260573e4870f85520ff361b4b374a96548c8e46121f77fd9407b4b9af2571f3dc3c36d1873baa22be013d4bc1dafaff96df1dd009f580d6d8
data/CHANGELOG.md CHANGED
@@ -5,6 +5,24 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.2.0] - 2025-09-25
9
+
10
+ ### Added
11
+ - Added `value_method` and `text_method` options for flexible attribute handling
12
+ - Support for objects with custom attribute names (e.g., `slug` instead of `id`, `title` instead of `name`)
13
+ - Enhanced documentation with examples for different data formats
14
+
15
+ ### Enhanced
16
+ - Improved `extract_item_data` method to handle configurable attribute methods
17
+ - Better support for hash formats with custom keys
18
+ - Maintained backward compatibility with existing `id`/`name` defaults
19
+
20
+ ### Use Cases Supported
21
+ - ActiveRecord objects with any attribute names: `value_method: :slug, text_method: :title`
22
+ - Simple string arrays that return selected strings as-is
23
+ - Hash formats with custom keys
24
+ - Mixed data formats in the same collection
25
+
8
26
  ## [0.1.1] - 2025-09-25
9
27
 
10
28
  ### Fixed
data/README.md CHANGED
@@ -201,6 +201,55 @@ For server-side creation of new items:
201
201
  ) %>
202
202
  ```
203
203
 
204
+ ### With Custom Attributes
205
+
206
+ For objects with custom attribute names:
207
+
208
+ ```erb
209
+ <!-- Using objects with slug/title instead of id/name -->
210
+ <%= multiselect_field(
211
+ form,
212
+ :category_slugs,
213
+ collection: @categories, # Objects with .slug and .title methods
214
+ selected: @game.categories, # Selected objects
215
+ value_method: :slug, # Use slug for the value
216
+ text_method: :title, # Use title for display text
217
+ label: "Categories"
218
+ ) %>
219
+
220
+ <!-- Using hash format with custom keys -->
221
+ <%= multiselect_field(
222
+ form,
223
+ :author_ids,
224
+ collection: [
225
+ { author_id: 1, full_name: 'John Doe' },
226
+ { author_id: 2, full_name: 'Jane Smith' }
227
+ ],
228
+ selected: [{ author_id: 1, full_name: 'John Doe' }],
229
+ value_method: :author_id, # Use author_id for the value
230
+ text_method: :full_name, # Use full_name for display text
231
+ label: "Authors"
232
+ ) %>
233
+ ```
234
+
235
+ ### With Simple Strings
236
+
237
+ For simple string arrays:
238
+
239
+ ```erb
240
+ <!-- Strings as both value and display text -->
241
+ <%= multiselect_field(
242
+ form,
243
+ :skills,
244
+ collection: ["Ruby", "JavaScript", "Python", "Go"],
245
+ selected: ["Ruby", "JavaScript"],
246
+ allow_create: true,
247
+ label: "Skills"
248
+ ) %>
249
+
250
+ <!-- Selected values will be the strings themselves: ["Ruby", "JavaScript"] -->
251
+ ```
252
+
204
253
  ## Options
205
254
 
206
255
  | Option | Type | Default | Description |
@@ -215,6 +264,8 @@ For server-side creation of new items:
215
264
  | `api_endpoint` | String | `nil` | API endpoint for server-side creation |
216
265
  | `help_text` | String | `nil` | Help text below the field |
217
266
  | `theme` | String | `"auto"` | Theme mode: "light", "dark", or "auto" |
267
+ | `value_method` | Symbol | `:id` | Method to call for the value/id |
268
+ | `text_method` | Symbol | `:name` | Method to call for display text |
218
269
 
219
270
  ## Keyboard Shortcuts
220
271
 
@@ -18,6 +18,8 @@ module RailsNotionLikeMultiselect
18
18
  # @option options [String] :api_endpoint API endpoint for creating new items
19
19
  # @option options [String] :help_text Help text to display below the field
20
20
  # @option options [String] :theme Theme mode ('light', 'dark', or 'auto')
21
+ # @option options [Symbol] :value_method Method to call for the value/id (default: :id for objects, self for strings)
22
+ # @option options [Symbol] :text_method Method to call for display text (default: :name for objects, self for strings)
21
23
  #
22
24
  def multiselect_field(form, field, options = {})
23
25
  collection = options[:collection] || []
@@ -30,6 +32,8 @@ module RailsNotionLikeMultiselect
30
32
  api_endpoint = options[:api_endpoint]
31
33
  help_text = options[:help_text]
32
34
  theme = options[:theme] || 'auto'
35
+ value_method = options[:value_method] || :id
36
+ text_method = options[:text_method] || :name
33
37
  input_name = "#{form.object_name}[#{field}][]"
34
38
 
35
39
  # Determine theme-specific classes based on theme option
@@ -155,8 +159,7 @@ module RailsNotionLikeMultiselect
155
159
  data: { rails_notion_multiselect_target: 'selectedItems' },
156
160
  class: 'flex flex-wrap gap-1.5 items-center' do
157
161
  selected.map do |item|
158
- item_id = item.respond_to?(:id) ? item.id.to_s : item.to_s
159
- item_name = item.respond_to?(:name) ? item.name : item.to_s
162
+ item_id, item_name = extract_item_data(item, value_method, text_method)
160
163
 
161
164
  content_tag :span,
162
165
  class: badge_classes,
@@ -221,10 +224,9 @@ module RailsNotionLikeMultiselect
221
224
  data: { rails_notion_multiselect_target: 'optionsList' },
222
225
  class: 'max-h-60 overflow-auto py-1' do
223
226
  collection.map do |item|
224
- item_id = item.respond_to?(:id) ? item.id.to_s : item.to_s
225
- item_name = item.respond_to?(:name) ? item.name : item.to_s
227
+ item_id, item_name = extract_item_data(item, value_method, text_method)
226
228
  is_selected = selected.any? { |s|
227
- s_id = s.respond_to?(:id) ? s.id.to_s : s.to_s
229
+ s_id, _ = extract_item_data(s, value_method, text_method)
228
230
  s_id == item_id
229
231
  }
230
232
 
@@ -258,7 +260,7 @@ module RailsNotionLikeMultiselect
258
260
  hidden_inputs_html = content_tag :div, data: { rails_notion_multiselect_target: 'hiddenInputs' } do
259
261
  if selected.any?
260
262
  selected.map do |item|
261
- item_id = item.respond_to?(:id) ? item.id.to_s : item.to_s
263
+ item_id, _ = extract_item_data(item, value_method, text_method)
262
264
  tag.input(type: 'hidden', name: input_name, value: item_id)
263
265
  end.join.html_safe
264
266
  else
@@ -282,6 +284,26 @@ module RailsNotionLikeMultiselect
282
284
 
283
285
  label_html + input_container_html + dropdown_html + hidden_inputs_html + help_text_html
284
286
  end
287
+
288
+ # Extracts id and name from various item formats (objects, hashes, strings)
289
+ # Returns [id, name] as strings
290
+ # @param item [Object] The item to extract data from
291
+ # @param value_method [Symbol] Method to call for the value/id
292
+ # @param text_method [Symbol] Method to call for display text
293
+ def extract_item_data(item, value_method = :id, text_method = :name)
294
+ if item.respond_to?(value_method) && item.respond_to?(text_method)
295
+ # ActiveRecord object or similar with custom methods
296
+ [item.send(value_method).to_s, item.send(text_method).to_s]
297
+ elsif item.is_a?(Hash)
298
+ # Hash with symbol or string keys
299
+ id = item[value_method] || item[value_method.to_s] || item[:id] || item['id']
300
+ name = item[text_method] || item[text_method.to_s] || item[:name] || item['name']
301
+ [id.to_s, name.to_s]
302
+ else
303
+ # Fallback - treat as string/number (both value and display are the same)
304
+ [item.to_s, item.to_s]
305
+ end
306
+ end
285
307
  end
286
308
  end
287
309
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsNotionLikeMultiselect
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_notion_like_multiselect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sulman Baig