Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

formatting

  • Loading branch information...
commit cb37e216fd4326181ff7f99c06cb1cb2a8a0ac2e 1 parent 1cabbbd
weidenfreak authored
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,7 +33,7 @@ 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
@@ -41,7 +41,7 @@ class FieldErrorProcHelperTest < Test::Unit::TestCase
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,7 +56,7 @@ 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
@@ -64,9 +64,9 @@ class FieldErrorProcHelperTest < Test::Unit::TestCase
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
Please sign in to comment.
Something went wrong with that request. Please try again.