Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

if_present created + tests

  • Loading branch information...
commit a3bda1c0f0508e0fdd8176cf75ec07a237afd861 1 parent 5ba3884
@khustochka authored
View
4 app/views/taxa/add_edit.html.haml
@@ -1,8 +1,8 @@
- content_for :page_title do
- = @taxon.name_la.if_true( "#{model_name.capitalize} %s", "#{action_name.humanize} #{model_name}")
+ = @taxon.name_la.if_present {|s| "#{model_name.capitalize} #{s}"} rescue "#{action_name.humanize} #{model_name}"
%h1
- = @taxon.name_la.if_true( "#{@taxon.name_ru} / <em>%s</em>", "#{action_name.humanize} #{model_name}")
+ = @taxon.name_la.if_present {|s| "#{@taxon.name_ru} / #{content_tag :em, s}"} rescue "#{action_name.humanize} #{model_name}"
= render( :partial => "taxa/table" )
View
16 lib/core_ext/object.rb
@@ -1,7 +1,15 @@
class Object
-
- def if_true(positive, negative = '')
- self ? positive.gsub(/\%s/, self) : negative
+ def if_present(value = nil, &block)
+ unless self.blank?
+ if block_given?
+ yield self
+ elsif !value.nil?
+ value
+ else
+ self
+ end
+ else
+ raise
+ end
end
-
end
View
4 lib/core_ext/string.rb
@@ -12,8 +12,4 @@ def slav_humanize
full_name.push(full_name.shift)
full_name.join(" ").mb_chars.capitalize.to_s
end
-
- def if_not_empty(mask, else_option = '')
- self.empty? ? else_option : mask.gsub(/\%s/, self)
- end
end
View
21 test/unit/core_ext/object_test.rb
@@ -0,0 +1,21 @@
+require 'test/test_helper'
+
+class ObjectTest < ActiveSupport::TestCase
+ test "if_present should work" do
+ # success tests
+ assert_equal "Цаплевые", "Цаплевые".if_present
+ assert_equal "<b>Цаплевые</b>", "Цаплевые".if_present { |val| "<b>#{val}</b>" }
+ temp = true.if_present("true") rescue "empty"
+ assert_equal "true", temp
+
+ # failure tests
+ temp = "".if_present { |val| "<b>#{val}</b>" } rescue ""
+ assert_equal "", temp
+ temp = "".if_present { |val| "<b>#{val}</b>" } rescue "empty"
+ assert_equal "empty", temp
+ temp = nil.if_present { |val| "<b>#{val}</b>" } rescue "empty2"
+ assert_equal "empty2", temp
+ temp = false.if_present { |val| "<b>#{val}</b>" } rescue "empty3"
+ assert_equal "empty3", temp
+ end
+end
View
7 test/unit/core_ext/string_test.rb
@@ -0,0 +1,7 @@
+require 'test/test_helper'
+
+class StringTest < ActiveSupport::TestCase
+ test "slav_humanize test" do
+ assert_equal "Большая белая цапля", "Цапля большая белая".slav_humanize
+ end
+end
View
5 test/unit/string_test.rb
@@ -1,5 +0,0 @@
-class StringTest < ActiveSupport::TestCase
- test "slav_humanize test" do
- assert_equal "Цапля большая белая".slav_humanize, "Большая белая цапля"
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.