diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index 77121d0..2eb738c 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -248,7 +248,7 @@ def associated_object end def association_exists? - form_object&.class&.reflect_on_association(association_name).present? + association_name && form_object&.respond_to?(association_name) end def form_object @@ -300,7 +300,7 @@ def hidden_field_data def hidden_field_value return value if value - if form_object&.defined_enums&.try :[], name + if form_object&.try(:defined_enums)&.try(:[], name) form_object.public_send "#{name}_before_type_cast" else form_object&.try(name).then do |value| diff --git a/test/dummy/app/controllers/comboboxes_controller.rb b/test/dummy/app/controllers/comboboxes_controller.rb index 3d7457e..059dd6e 100644 --- a/test/dummy/app/controllers/comboboxes_controller.rb +++ b/test/dummy/app/controllers/comboboxes_controller.rb @@ -98,6 +98,10 @@ def morph @user = User.where.not(home_state: nil).first || raise("No user found with home state, load fixtures first.") end + def form_object + @object = Form.new + end + private delegate :combobox_options, :html_combobox_options, to: "ApplicationController.helpers", private: true diff --git a/test/dummy/app/models/form.rb b/test/dummy/app/models/form.rb new file mode 100644 index 0000000..9db43e0 --- /dev/null +++ b/test/dummy/app/models/form.rb @@ -0,0 +1,14 @@ +class Form + include ActiveModel::Model + include ActiveModel::Attributes + + attr_accessor :state_id + + def state + State.find state_id + end + + def state_id + @state_id || State.first.id + end +end diff --git a/test/dummy/app/views/comboboxes/form_object.html.erb b/test/dummy/app/views/comboboxes/form_object.html.erb new file mode 100644 index 0000000..cb9022d --- /dev/null +++ b/test/dummy/app/views/comboboxes/form_object.html.erb @@ -0,0 +1,4 @@ +<%= form_with model: @object, url: "#" do |form| %> + <%= form.combobox :state_id, states_path %> + <%= form.submit %> +<% end %> diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 1403a6b..a8e3b88 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -29,6 +29,7 @@ get "multiselect_new_values", to: "comboboxes#multiselect_new_values" get "grouped_options", to: "comboboxes#grouped_options" get "morph", to: "comboboxes#morph" + get "form_object", to: "comboboxes#form_object" resources :movies, only: %i[ index update ] get "movies_html", to: "movies#index_html" diff --git a/test/system/hotwire_combobox_test.rb b/test/system/hotwire_combobox_test.rb index 3768fe1..7d629c1 100644 --- a/test/system/hotwire_combobox_test.rb +++ b/test/system/hotwire_combobox_test.rb @@ -1162,6 +1162,12 @@ class HotwireComboboxTest < ApplicationSystemTestCase user.visited_states.map(&:id) end + test "POROs as form objects" do + visit form_object_path + + assert_combobox_display_and_value "#form_state_id", "Alabama", states(:alabama).id + end + private def open_combobox(selector) find(selector).click