Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@UnderpantsGnome authored
Showing with 26 additions and 11 deletions.
  1. +15 −0 lib/rails_extensions.rb
  2. +11 −11 lib/useful.rb
View
15 lib/rails_extensions.rb
@@ -40,5 +40,20 @@ def public_file_path(file)
file ||= ''
File.expand_path(File.join(RAILS_ROOT, 'public', file.gsub(/\?.*$/, '')))
end
+
+ def distance_of_time_for(obj, method)
+ "<span title=\"#{obj.send(method).localtime}\">#{distance_of_time_in_words_to_now(obj.send(method).localtime)}</span>"
+ end
+
+ def back_link(text='Back', *args)
+ link_to text, (request.referrer || 'javascript:history.go(-1)'), *args
+ end
+
+ def current_controller?(*options)
+ options = options[1] if options.is_a?(Array)
+ return false unless options.useful? || options.is_a?(Hash)
+ options[:action] = :index
+ url_for({:action => :index}) == url_for(options)
+ end
end
end
View
22 lib/useful.rb
@@ -1,19 +1,19 @@
+class Object
+ def useful?; true end
+ def useless?; false end
+end
+
class NilClass
- def useful?
- false
- end
+ def useful?; false end
+ def useless?; true end
end
class String
def useful?
((self.length == 0) || self !~ /\S/) ? false : true
end
-end
-
-class Fixnum
- def useful?; true end
-end
-
-class Float
- def useful?; true end
+
+ def useless?
+ ! self.useful?
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.