Skip to content

Commit

Permalink
Support non-AR form objects (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefarias authored Apr 21, 2024
1 parent 9482a75 commit 641d8a4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/comboboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions test/dummy/app/models/form.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/dummy/app/views/comboboxes/form_object.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= form_with model: @object, url: "#" do |form| %>
<%= form.combobox :state_id, states_path %>
<%= form.submit %>
<% end %>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 641d8a4

Please sign in to comment.