Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for models without an error method #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/comfy_bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def draw_errors(bootstrap, method)
if bootstrap.error.present?
errors = [bootstrap.error]
else
return if object.nil?
return if object.nil? || object.errors.nil?

errors = object.errors[method]

Expand Down
37 changes: 37 additions & 0 deletions test/comfy_bootstrap_form/models_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative "../test_helper"

class ModelsTest < ActionView::TestCase

if Rails.version >= "6.0"
include ActionText::TagHelper
end

def test_activerecord_model
user = User.new
builder = ComfyBootstrapForm::FormBuilder.new(:user, user, self, bootstrap: {})
actual = builder.text_field(:email)
expected = <<-HTML
<div class="form-group">
<label for="user_email">Email</label>
<input class="form-control" type="text" name="user[email]" id="user_email" />
</div>
HTML
assert_xml_equal expected, actual
end

def test_openstruct_model
user = OpenStruct.new(email: nil)
builder = ComfyBootstrapForm::FormBuilder.new(:user, user, self, bootstrap: {})
actual = builder.text_field(:email)
expected = <<-HTML
<div class="form-group">
<label for="user_email">Email</label>
<input class="form-control" type="text" name="user[email]" id="user_email" />
</div>
HTML
assert_xml_equal expected, actual
end

end