Skip to content
This repository
Browse code

Add some Rails goodies to core extensions

Add useful? to Object, add a useless? for cleaner ! testing
  • Loading branch information...
commit cce2b26fed44087387bcf0f6042f4b345ec5e0c3 1 parent 72c055c
Michael Moen authored
15  lib/rails_extensions.rb
@@ -40,5 +40,20 @@ def public_file_path(file)
40 40
       file ||= ''
41 41
       File.expand_path(File.join(RAILS_ROOT, 'public', file.gsub(/\?.*$/, '')))
42 42
     end
  43
+
  44
+    def distance_of_time_for(obj, method)
  45
+      "<span title=\"#{obj.send(method).localtime}\">#{distance_of_time_in_words_to_now(obj.send(method).localtime)}</span>"
  46
+    end
  47
+  
  48
+    def back_link(text='Back', *args)
  49
+      link_to text, (request.referrer || 'javascript:history.go(-1)'), *args
  50
+    end
  51
+
  52
+    def current_controller?(*options)
  53
+      options = options[1] if options.is_a?(Array)
  54
+      return false unless options.useful? || options.is_a?(Hash)
  55
+      options[:action] = :index
  56
+      url_for({:action => :index}) == url_for(options)
  57
+    end
43 58
   end
44 59
 end
22  lib/useful.rb
... ...
@@ -1,19 +1,19 @@
  1
+class Object
  2
+  def useful?; true end
  3
+  def useless?; false end
  4
+end
  5
+
1 6
 class NilClass
2  
-  def useful?
3  
-    false
4  
-  end
  7
+  def useful?; false end
  8
+  def useless?; true end
5 9
 end
6 10
 
7 11
 class String
8 12
   def useful?
9 13
     ((self.length == 0) || self !~ /\S/) ? false : true
10 14
   end
11  
-end
12  
-
13  
-class Fixnum
14  
-  def useful?; true end
15  
-end
16  
-
17  
-class Float
18  
-  def useful?; true end
  15
+  
  16
+  def useless?
  17
+    ! self.useful?
  18
+  end
19 19
 end

0 notes on commit cce2b26

Please sign in to comment.
Something went wrong with that request. Please try again.