easy-admin-rails 0.1.9 → 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: d516d9564b3e2e33581e101ed5f5fed1fb294407f6de918e2532a3fcef1acea5
4
- data.tar.gz: f3f23ffc0489acddce45c29f4324d38397e088fc4b7fc872fb06ed6cf4d07c64
3
+ metadata.gz: 8077539734038d6d2f5e76fd811bc6350f48fd9f15854c94bacccc0828d597a9
4
+ data.tar.gz: 8e21d37b20333eb6cc11ed4c8acb0031bae2c7ad86642e210a0bb5ced98ebd3c
5
5
  SHA512:
6
- metadata.gz: 3632ce6f54bb99a75db6a45471f53f01eb34c132480367f84017c90ab45f4605527f2a87f89a4446e41ff3182decf590c9df0e3d6178034cc713421b41d30ff6
7
- data.tar.gz: 20b25f16081adfd5eea8cbbf62d96c35aee0b38da356befed86132d576d20819c9985987ae1d04e557f49b61964492f2c714b1fe9dca981c4867b9dce8051d60
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"
@@ -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
@@ -1,3 +1,3 @@
1
1
  module EasyAdmin
2
- VERSION = "0.1.9"
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.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Slaurmagan