Skip to content

Commit

Permalink
raise and expect an exception instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieujobin committed Nov 1, 2023
1 parent d817496 commit 943e21e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
10 changes: 8 additions & 2 deletions lib/action_view/helpers/dynamic_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +17,7 @@ module DynamicForm
# input("post", "title")
# # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
42 changes: 31 additions & 11 deletions test/dynamic_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 id="post_title" name="post[title]" size="30" type="text" value="Hello World" />), input("post", "title")
)
end
end

def test_text_area_with_errors
Expand Down Expand Up @@ -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 accept-charset="UTF-8" action="create" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="field_with_errors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
form("post")
Expand All @@ -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'
Expand All @@ -203,26 +211,34 @@ 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 accept-charset="UTF-8" action="create" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="field_with_errors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
form("post", :method=>'get')
)
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
Expand All @@ -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
Expand Down Expand Up @@ -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 accept-charset="UTF-8" action="create" enctype="multipart/form-data" method="post"><input name="utf8" type="hidden" value="&#x2713;" autocomplete="off" /><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="field_with_errors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
form("post", :multipart => true)
)
end
end

def test_default_form_builder_with_dynamic_form_helpers
Expand Down

0 comments on commit 943e21e

Please sign in to comment.