Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplifying the form builder implementation to make it easier on the …

…eye. Also making tag attribute altering adapters (such as validatious) work again, woot!
  • Loading branch information...
commit 9c7473bca42704131fac50c4ef917a7c12d1be01 1 parent 9dba554
@augustl authored
View
56 lib/live_validations/form_builder.rb
@@ -11,15 +11,17 @@ def self.included(base)
helpers_with_one_option_hash.each do |helper|
define_method("#{helper}_with_live_validations") do |attribute, *args|
- @template.adapter_instance.renders_attribute(attribute) if @template.adapter_instance
-
- if @template.adapter_instance && @template.adapter_instance.alters_tag_attributes?
- options = args.extract_options!
- options.merge!(@template.adapter_instance[:tag_attributes])
- __send__("#{helper}_without_live_validations", attribute, *(args << options))
- else
- __send__("#{helper}_without_live_validations", attribute, *args)
+ if @template.adapter_instance
+ @template.adapter_instance.renders_attribute(attribute)
+
+ if @template.adapter_instance.alters_tag_attributes?
+ options = args.extract_options!
+ options.merge!(@template.adapter_instance[:tag_attributes][attribute])
+ args << options
+ end
end
+
+ __send__("#{helper}_without_live_validations", attribute, *args)
end
alias_method_chain helper, "live_validations"
@@ -27,28 +29,28 @@ def self.included(base)
helpers_with_two_option_hashes.each do |helper|
define_method("#{helper}_with_live_validations") do |attribute, *args|
- @template.adapter_instance.renders_attribute(attribute) if @template.adapter_instance
+ if @template.adapter_instance
+ @template.adapter_instance.renders_attribute(attribute)
+
+ if @template.adapter_instance.alters_tag_attributes?
+ # We have both options and html_options
+ if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
+ html_options = args.pop
+ options = args.pop
- if @template.adapter_instance && @template.adapter_instance.alters_tag_attributes?
- # We have both options and html_options
- if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
- html_options = args.pop
- options = args.pop
-
- html_options.merge!(@template.adapter_instance[:tag_attributes])
- args << options
- args << html_options
- __send__("#{helper}_without_live_validations", *args)
- # No html_options was specified
- else
- html_options = @template.adapter_instance[:tag_attributes]
- args << {} unless args[-1].is_a?(Hash)
- args << html_options
- __send__("#{helper}_without_live_validations", attribute, *args)
+ html_options.merge!(@template.adapter_instance[:tag_attributes][attribute])
+ args << options
+ args << html_options
+ # No html_options was specified
+ else
+ html_options = @template.adapter_instance[:tag_attributes][attribute]
+ args << {} unless args[-1].is_a?(Hash)
+ args << html_options
+ end
end
- else
- __send__("#{helper}_without_live_validations", attribute, *args)
end
+
+ __send__("#{helper}_without_live_validations", attribute, *args)
end
alias_method_chain helper, "live_validations"
View
9 lib/live_validations/view_helpers.rb
@@ -26,9 +26,14 @@ def form_for_with_live_validations(record_name_or_array, *args, &block)
self.adapter_instance = LiveValidations.current_adapter.new(record)
adapter_instance.handle_form_for_options(options)
- form_for_without_live_validations(record_name_or_array, *(args << options), &block)
- adapter_instance.run_validations
+ if adapter_instance.alters_tag_attributes?
+ adapter_instance.run_validations
+ form_for_without_live_validations(record_name_or_array, *(args << options), &block)
+ else
+ form_for_without_live_validations(record_name_or_array, *(args << options), &block)
+ adapter_instance.run_validations
+ end
silence_warnings do
concat(%{<script type="text/javascript">#{adapter_instance.render_inline_javascript}</script>}, block.binding) if adapter_instance.utilizes_inline_javascript?
View
71 test/validatious_controller_output_test.rb
@@ -11,16 +11,69 @@ def teardown
restore_callbacks Post
end
- def test_html_attribute_output
- Post.validates_presence_of :title, :category
+ def test_one_option_hash_helpers
+ Post.validates_presence_of :title
+
+ render %{
+ <% form_for(Post.new, :live_validations => true) do |f| %>
+ <%= f.text_field :title %>
+ <% end %>
+ }
+
+ assert_html 'input[type=text]#post_title.required'
+ end
+
+ def test_one_option_hash_helper_with_options
+ Post.validates_presence_of :title
+
+ render %{
+ <% form_for(Post.new, :live_validations => true) do |f| %>
+ <%= f.text_field :title, :id => "a_custom_id" %>
+ <% end %>
+ }
+
+ assert_html 'input[type=text]#a_custom_id.required'
+ end
+
+ def test_confirmation
Post.validates_confirmation_of :password
+ Post.send(:attr_accessor, :password)
+
+ render %{
+ <% form_for(Post.new, :live_validations => true) do |f| %>
+ <%= f.password_field :password %>
+ <%= f.password_field :password_confirmation %>
+ <% end %>
+ }
+
+ assert_html 'input[type=password]#post_password_confirmation.confirmation-of_post_password'
+ end
+
+ def test_select_tag_with_one_option_hash
+ Post.validates_presence_of :title
+
+ render %{
+ <% form_for(Post.new, :live_validations => true) do |f| %>
+ <%= f.select :title, ["foo", "bar"], :include_blank => true %>
+ <% end %>
+ }
+
+ assert_html 'select#post_title'
+ assert_html 'select option[value=""]'
+ assert_html 'select.required'
+ end
+
+ def test_select_tag_with_two_option_hashes
+ Post.validates_presence_of :title
+
+ render %{
+ <% form_for(Post.new, :live_validations => true) do |f| %>
+ <%= f.select :title, ["foo", "bar"], {:include_blank => true}, :id => "a_custom_id" %>
+ <% end %>
+ }
- # The tag_attributes stuff doesn't work. No adapters are using it yet, so I'm
- # disabling these tests because they don't test functionality that's being used.
- #assert_select('form.validate')
- #assert_select('input#post_title.required')
- #assert_select('input#post_password_confirmation.confirmation-of_post_password')
- #assert_select('select#post_category.required')
- #assert css_select('script[type=text/javascript]').empty?
+ assert_html 'select#a_custom_id'
+ assert_html 'select option[value=""]'
+ assert_html 'select.required'
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.