From 943e21e4944d185d3c576637bec72515071673f9 Mon Sep 17 00:00:00 2001
From: Mathieu Jobin
Date: Wed, 1 Nov 2023 18:04:42 +0900
Subject: [PATCH] raise and expect an exception instead
---
lib/action_view/helpers/dynamic_form.rb | 10 ++++--
test/dynamic_form_test.rb | 42 ++++++++++++++++++-------
2 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/lib/action_view/helpers/dynamic_form.rb b/lib/action_view/helpers/dynamic_form.rb
index fc220e7..ecc171a 100644
--- a/lib/action_view/helpers/dynamic_form.rb
+++ b/lib/action_view/helpers/dynamic_form.rb
@@ -2,6 +2,7 @@
require 'active_support/i18n'
require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/object/blank'
+class BrokenFeatureError < RuntimeError; end
module ActionView
module Helpers
@@ -16,7 +17,7 @@ module DynamicForm
# input("post", "title")
# # =>
def input(record_name, method, options = {})
- raise 'this is dead code, do not use.'
+ raise_broken_code_error
InstanceTag.new(record_name, method, self).to_tag(options)
end
@@ -80,7 +81,7 @@ def form(record_name, options = {})
submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
- raise "unsupported code, do not use!"
+ raise_broken_code_error
contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
contents.safe_concat hidden_field(record_name, :id) if record.persisted?
contents.safe_concat all_input_tags(record, record_name, options)
@@ -247,6 +248,11 @@ def error_messages_for(*params)
private
+ def raise_broken_code_error
+ raise BrokenFeatureError,
+ "This feature is currently broken, please open a pull request at https://github.com/payrollhero/dynamic_form to have it reviewed and released."
+ end
+
def all_input_tags(record, record_name, options)
input_block = options[:input_block] || default_input_block
record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
diff --git a/test/dynamic_form_test.rb b/test/dynamic_form_test.rb
index 47926f3..3ebf832 100644
--- a/test/dynamic_form_test.rb
+++ b/test/dynamic_form_test.rb
@@ -130,11 +130,11 @@ def url_for(options)
end
def test_generic_input_tag
- return true # unsupported
-
+ assert_raise(BrokenFeatureError) do
assert_dom_equal(
%(), input("post", "title")
)
+ end
end
def test_text_area_with_errors
@@ -169,8 +169,12 @@ def test_field_error_proc
end
def test_form_with_string
- return true # unsupported
+ assert_raise(BrokenFeatureError) do
+ inner_test_form_with_string
+ end
+ end
+ def inner_test_form_with_string
assert_dom_equal(
%(
),
form("post")
@@ -191,8 +195,12 @@ def id() 1 end
end
def test_form_with_protect_against_forgery
- return true # unsupported
+ assert_raise(BrokenFeatureError) do
+ inner_test_form_with_protect_against_forgery
+ end
+ end
+ def inner_test_form_with_protect_against_forgery
@protect_against_forgery = true
@request_forgery_protection_token = 'authenticity_token'
@form_authenticity_token = '123'
@@ -203,8 +211,12 @@ def test_form_with_protect_against_forgery
end
def test_form_with_method_option
- return true # unsupported
+ assert_raise(BrokenFeatureError) do
+ test_form_with_method_option
+ end
+ end
+ def inner_test_form_with_method_option
assert_dom_equal(
%(),
form("post", :method=>'get')
@@ -212,17 +224,21 @@ def test_form_with_method_option
end
def test_form_with_action_option
- return true # unsupported
-
+ assert_raise(BrokenFeatureError) do
output_buffer << form("post", :action => "sign")
assert_select "form[action=sign]" do |form|
assert_select "input[type=submit][value=Sign]"
end
+ end
end
def test_form_with_date
- return true # unsupported
+ assert_raise(BrokenFeatureError) do
+ inner_test_form_with_date
+ end
+ end
+ def inner_test_form_with_date
silence_warnings do
def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
end
@@ -234,8 +250,12 @@ def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
end
def test_form_with_datetime
- return true # unsupported
+ assert_raise(BrokenFeatureError) do
+ inner_test_form_with_datetime
+ end
+ end
+ def inner_test_form_with_datetime
silence_warnings do
def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end
end
@@ -347,12 +367,12 @@ def test_error_messages_for_model_objects
end
def test_form_with_string_multipart
- return true # unsupported
-
+ assert_raise(BrokenFeatureError) do
assert_dom_equal(
%(),
form("post", :multipart => true)
)
+ end
end
def test_default_form_builder_with_dynamic_form_helpers