easy-admin-rails 0.1.8 → 0.1.10

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: 714d6b87819aca514f1b6547871b544c36f7ba2d9941b3c235df73c6276fad3e
4
- data.tar.gz: 42b81c8f6157e0589d4c39ea6684c836154b1e4d5a0c6ab22c723ad34604ab1d
3
+ metadata.gz: 8077539734038d6d2f5e76fd811bc6350f48fd9f15854c94bacccc0828d597a9
4
+ data.tar.gz: 8e21d37b20333eb6cc11ed4c8acb0031bae2c7ad86642e210a0bb5ced98ebd3c
5
5
  SHA512:
6
- metadata.gz: b347e40183ea5cdfed508bf9921826defddb39f9dcd5701746b3622b44d4917a900df3cf029eb5c2decc5a792a313617127909588377f23cbdd6e977edee8d9e
7
- data.tar.gz: feb58175155da2dcb224766ba9be58d3bc2837a7fbee6b64353ef823b3879bd164f95772d05f7695e80ee996b19bb84781c5faf0df770d1626c2d7c4da9adfc4
6
+ metadata.gz: 7f1035f56aae702e202f742b1ef7a2747f6a53d8c0acb1cdecca91b5d137a6044eff7a07a6879437836ce9a6c9f985ddde53f122377592b01881fe7093b33069
7
+ data.tar.gz: f94fc0dd71cb0d4b04b3b46c4b19e3e91b413a4be0617273e1900cc2a7a3c7fd9024a3340ee0be064650141060627253b4fc751c100e4f0da0b9a6999562aed1
@@ -17,7 +17,18 @@ module EasyAdmin
17
17
 
18
18
  # Override to handle belongs_to association field name
19
19
  def association_key
20
- # Convert belongs_to association name to foreign key
20
+ # If field specifies a custom foreign_key, use that
21
+ if field[:foreign_key]
22
+ return field[:foreign_key]
23
+ end
24
+
25
+ # If field specifies an association, get the foreign key from Rails reflection
26
+ if field[:association]
27
+ reflection = form.object.class.reflect_on_association(field[:association])
28
+ return reflection.foreign_key if reflection
29
+ end
30
+
31
+ # Default behavior: Convert field name to foreign key
21
32
  # e.g., user -> user_id, category -> category_id
22
33
  if field_name.to_s.end_with?('_id')
23
34
  field_name
@@ -74,6 +85,28 @@ module EasyAdmin
74
85
  current_value.present? ? [current_value] : []
75
86
  end
76
87
 
88
+ # Override to get current display value for suggest mode
89
+ def current_display_value
90
+ # Get the associated record
91
+ association_name = field[:association] || field_name
92
+ associated_record = form.object.public_send(association_name) if form.object.respond_to?(association_name)
93
+
94
+ return "" unless associated_record
95
+
96
+ # Determine how to display the associated record
97
+ if field[:display_method]
98
+ associated_record.public_send(field[:display_method])
99
+ elsif associated_record.respond_to?(:name)
100
+ associated_record.name
101
+ elsif associated_record.respond_to?(:title)
102
+ associated_record.title
103
+ elsif associated_record.respond_to?(:to_label)
104
+ associated_record.to_label
105
+ else
106
+ "##{associated_record.id}"
107
+ end
108
+ end
109
+
77
110
  # Override to ensure hidden input uses the foreign key value
78
111
  def render_hidden_inputs
79
112
  # Single select - one hidden input with foreign key value
@@ -133,7 +133,7 @@ module EasyAdmin
133
133
  class: single_select_input_classes,
134
134
  placeholder: suggest_mode? ? placeholder : (field[:placeholder] || "Select #{field_label.downcase}..."),
135
135
  readonly: !suggest_mode?,
136
- value: suggest_mode? ? "" : current_display_value,
136
+ value: current_display_value,
137
137
  data: suggest_mode? ? {
138
138
  select_field_target: "search",
139
139
  action: "input->select-field#filter keydown->select-field#handleKeydown focus->select-field#openDropdown"
@@ -28,21 +28,50 @@ module EasyAdmin
28
28
  end
29
29
 
30
30
  def load_association_record
31
- return nil if value.blank?
31
+ foreign_key_value = get_foreign_key_value
32
+ return nil if foreign_key_value.blank?
32
33
 
33
34
  association_class = determine_association
34
35
 
35
- if value.respond_to?(:id)
36
+ if foreign_key_value.respond_to?(:id)
36
37
  # Already loaded record
37
- value
38
+ foreign_key_value
38
39
  else
39
40
  # ID value, need to load the record
40
- association_class.find_by(id: value)
41
+ association_class.find_by(id: foreign_key_value)
42
+ end
43
+ end
44
+
45
+ def get_foreign_key_value
46
+ # If foreign_key is explicitly specified, use that field
47
+ if field[:foreign_key]
48
+ @record.public_send(field[:foreign_key])
49
+ elsif field[:association]
50
+ # Use Rails association reflection to get the foreign key
51
+ reflection = @record.class.reflect_on_association(field[:association])
52
+ if reflection
53
+ @record.public_send(reflection.foreign_key)
54
+ else
55
+ # Fallback to the original value (field name)
56
+ value
57
+ end
58
+ else
59
+ # Default behavior - use the field value
60
+ value
41
61
  end
42
62
  end
43
63
 
44
64
  def determine_association
45
- return field[:association] if field[:association]
65
+ if field[:association]
66
+ # If association is specified, get the class from the model's reflection
67
+ association_name = field[:association].to_s
68
+ reflection = @record.class.reflect_on_association(association_name)
69
+ if reflection
70
+ return reflection.klass
71
+ else
72
+ raise ArgumentError, "Association '#{association_name}' not found on #{@record.class}"
73
+ end
74
+ end
46
75
 
47
76
  # Try to determine from attribute name
48
77
  if field_name.to_s.end_with?('_id')
@@ -28,7 +28,7 @@ module EasyAdmin
28
28
 
29
29
  def render_field_value
30
30
  # Use the existing field rendering logic from the Rails views
31
- field_value = @record.public_send(@field_config[:name])
31
+ field_value = get_field_value
32
32
 
33
33
  # Use the same render_field helper that the ERB templates use
34
34
  unsafe_raw render_field(
@@ -38,6 +38,15 @@ module EasyAdmin
38
38
  record: @record
39
39
  )
40
40
  end
41
+
42
+ def get_field_value
43
+ # For belongs_to fields, use the association name if specified
44
+ if @field_config[:type] == :belongs_to && @field_config[:association]
45
+ @record.public_send(@field_config[:association])
46
+ else
47
+ @record.public_send(@field_config[:name])
48
+ end
49
+ end
41
50
 
42
51
  def render_inline_edit_trigger
43
52
  div do
@@ -231,18 +231,21 @@ module EasyAdmin
231
231
  return
232
232
  end
233
233
 
234
+ # Determine the correct parameter key (handle complex model names)
235
+ param_key = determine_param_key_for_update_field
236
+
234
237
  # For belongs_to fields, we need to handle foreign key updates
235
238
  if @field_config[:type] == :belongs_to
236
239
  association_name = @field_config[:name]
237
240
  foreign_key = get_foreign_key_name(association_name)
238
- field_value = params.dig(@resource_class.param_key, foreign_key)
241
+ field_value = params.dig(param_key, foreign_key)
239
242
  update_attrs = { foreign_key.to_sym => field_value }
240
243
  else
241
244
  # Create a field object to handle normalization
242
245
  field_obj = EasyAdmin::Field.new(@field_name, @field_config[:type], @field_config)
243
246
 
244
247
  # Get the field value from params
245
- field_value = params.dig(@resource_class.param_key, @field_name)
248
+ field_value = params.dig(param_key, @field_name)
246
249
 
247
250
  # Normalize the input
248
251
  update_attrs = { @field_name => field_value }
@@ -913,5 +916,18 @@ module EasyAdmin
913
916
  params[:period].present? ||
914
917
  (params[:scope].present? && params[:scope] != determine_current_scope[:scope])
915
918
  end
919
+
920
+ def determine_param_key_for_update_field
921
+ # Try resource param_key first, then fall back to model's natural param key
922
+ if params.key?(@resource_class.param_key)
923
+ @resource_class.param_key
924
+ elsif params.key?(@resource_class.model_class.model_name.param_key)
925
+ @resource_class.model_class.model_name.param_key
926
+ else
927
+ # Handle namespaced models that use slash format (e.g., "catalog/payment_method")
928
+ namespaced_param_key = @resource_class.model_class.name.underscore
929
+ params.key?(namespaced_param_key) ? namespaced_param_key : @resource_class.param_key
930
+ end
931
+ end
916
932
  end
917
933
  end
@@ -56,7 +56,9 @@ module EasyAdmin
56
56
  suggest: options[:suggest], # for dynamic option loading
57
57
  display_method: options[:display_method], # for association fields
58
58
  help_text: options[:help_text],
59
- editable: options[:editable]
59
+ editable: options[:editable],
60
+ association: options[:association],
61
+ foreign_key: options[:foreign_key] # for belongs_to fields
60
62
  }]
61
63
  end
62
64
 
@@ -321,7 +323,9 @@ module EasyAdmin
321
323
  multiple: options[:multiple],
322
324
  placeholder: options[:placeholder],
323
325
  help_text: options[:help_text],
324
- editable: options[:editable]
326
+ editable: options[:editable],
327
+ association: options[:association],
328
+ foreign_key: options[:foreign_key]
325
329
  }
326
330
 
327
331
  if @current_tab
@@ -1,3 +1,3 @@
1
1
  module EasyAdmin
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-admin-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Slaurmagan