Browse files

formatting

  • Loading branch information...
1 parent 1cabbbd commit cb37e216fd4326181ff7f99c06cb1cb2a8a0ac2e weidenfreak committed Aug 1, 2011
Showing with 20 additions and 20 deletions.
  1. +12 −12 lib/field_error_proc_helper.rb
  2. +8 −8 test/field_error_proc_helper_test.rb
View
24 lib/field_error_proc_helper.rb
@@ -10,40 +10,40 @@ module FieldErrorProcHelper
def self.add_css_class_to_element(css_class, html_element)
FieldErrorProcHelper::HtmlElement.add_css_class(css_class, html_element)
end
-
+
class HtmlElement
-
+
def initialize(css_class, html_element)
@css_class = css_class
@html_element = html_element
end
-
+
def self.add_css_class(css_class, html_element)
new(css_class, html_element).add_css_class
end
-
+
def add_css_class
should_add_css_class? ? insert_or_update_css_class : @html_element
end
-
+
private
-
+
def should_add_css_class?
match_form_element && element_needs_css_class
end
-
+
def element_needs_css_class
!(@html_element =~ /<[^>]+class=['"].*#{@css_class}.*['"]/)
end
-
+
def insert_or_update_css_class
match_class_attribute ? update_css_class : insert_css_class
end
-
+
def insert_css_class
- @html_element.insert(match_form_element.end(1), " class=\"#{@css_class}\"")
+ @html_element.insert(match_form_element.end(1), " class=\"#{@css_class}\"")
end
-
+
def update_css_class
@html_element.insert(match_class_attribute.end(1), "#{@css_class} ")
end
@@ -61,5 +61,5 @@ def target_elements
end
end
-
+
end
View
16 test/field_error_proc_helper_test.rb
@@ -12,17 +12,17 @@ def test(name, &block)
class FieldErrorProcHelperTest < Test::Unit::TestCase
test "should return tag with input_error class for input" do
- assert_equal "<input class=\"input_error test\">Test</input>",
+ assert_equal "<input class=\"input_error test\">Test</input>",
FieldErrorProcHelper.add_css_class_to_element("input_error", "<input class=\"test\">Test</input>")
end
test "should return tag with input_error class for label" do
- assert_equal "<label class=\"input_error test\">Test</label>",
+ assert_equal "<label class=\"input_error test\">Test</label>",
FieldErrorProcHelper.add_css_class_to_element("input_error", "<label class=\"test\">Test</label>")
end
test "should return tag with input_error class for textarea" do
- assert_equal "<textarea class=\"input_error test\">Test</textarea>",
+ assert_equal "<textarea class=\"input_error test\">Test</textarea>",
FieldErrorProcHelper.add_css_class_to_element("input_error", "<textarea class=\"test\">Test</textarea>")
end
@@ -33,15 +33,15 @@ class FieldErrorProcHelperTest < Test::Unit::TestCase
end
test "should return tag with input_error class for select" do
- assert_equal "<select class=\"input_error test\">Test</select>",
+ assert_equal "<select class=\"input_error test\">Test</select>",
FieldErrorProcHelper.add_css_class_to_element("input_error", "<select class=\"test\">Test</select>")
end
test "should return tag with input_error called through Proc::new" do
my_proc = Proc.new do |html_tag, instance|
FieldErrorProcHelper.add_css_class_to_element("input_error", html_tag)
end
-
+
assert_equal "<select class=\"input_error test\">Test</select>", my_proc.call("<select class=\"test\">Test</select>")
end
@@ -56,17 +56,17 @@ class FieldErrorProcHelperTest < Test::Unit::TestCase
test "should work without any spaces or other attributes" do
assert_equal "<select class=\"input_error\">Test</select>", FieldErrorProcHelper.add_css_class_to_element("input_error", "<select>Test</select>")
end
-
+
test "should return tag without input_error class for other tags" do
assert_equal "<p class=\"test\">Test</p>", FieldErrorProcHelper.add_css_class_to_element("input_error", "<p class=\"test\">Test</p>")
end
test "should return tag without input_error class for other tags without class" do
assert_equal "<p>Test</p>", FieldErrorProcHelper.add_css_class_to_element("input_error", "<p>Test</p>")
end
-
+
test "should not add class if it already exists for the element" do
- assert_equal "<input class=\"test input_error\">Test</input>",
+ assert_equal "<input class=\"test input_error\">Test</input>",
FieldErrorProcHelper.add_css_class_to_element("input_error", "<input class=\"test input_error\">Test</input>")
end

0 comments on commit cb37e21

Please sign in to comment.