Permalink
Browse files

refactoring

git-svn-id: https://svn.thoughtbot.com/plugins/what@361 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 2bde681 commit 7609bd7d4ba34abeaec77d25e61c02e48c2b4287 jcarroll committed Feb 20, 2008
Showing with 60 additions and 18 deletions.
  1. +1 −1 MIT-LICENSE
  2. +53 −11 README
  3. +3 −3 lib/what.rb
  4. +3 −3 test/what_test.rb
View
@@ -1,4 +1,4 @@
-Copyright (c) 2008 [name of plugin creator]
+Copyright (c) 2008 Dan Croak, Jared Carroll, and thoughtbot, inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
64 README
@@ -1,7 +1,7 @@
What
====
-What provides boolean instance methods on controllers
+What provides boolean query methods on Mime::Type instances
for the native Rails mime-types:
atom?
@@ -18,22 +18,64 @@ url_encoded_form?
xml?
yaml?
-... and a few other beauts:
-
-gif?
-iphone?
-mobile?
-pdf?
-png?
-
+... and any custom mime types that you add in any environment specific config files.
Examples
========
+__config/initializers/mime_types.rb__
+Mime::Type.register 'text/html', :blah
+
+__app/controllers/what_controller.rb__
+class WhatController < ApplicationController
+
+ before_filter :csv?,
+ :blah?
+
+ def index
+ render :text => 'index'
+ end
+
+ def csv?
+ if request.format.csv?
+ render :text => 'csv' and return false
+ end
+ end
+
+ def blah?
+ if request.format.blah?
+ render :text => 'blah' and return false
+ end
+ end
+
+end
+
What is especially powerful in combination with When.
-before_filter :render_css_colors, :if => :css?
-before_filter :verify_admin, :if => :csv?
+__app/controllers/what_controller.rb__
+class WhatController < ApplicationController
+
+ before_filter :csv,
+ :if => :csv?
+ before_filter :blah,
+ :if => lambda {|controller| controller.request.format.blah?}
+
+ def index
+ render :text => 'index'
+ end
+
+ def csv
+ render :text => 'csv' and return false
+ end
+
+ def csv?
+ request.format.csv?
+ end
+
+ def blah
+ render :text => 'blah' and return false
+ end
+end
Copyright (c) 2008 Dan Croak, Jared Carroll, and thoughtbot, inc. released under the MIT license
View
@@ -1,8 +1,8 @@
Mime::Type.class_eval do
- Mime::SET.each do |mime_type|
- define_method "#{mime_type.to_sym}?" do
- to_sym == mime_type.to_sym
+ Mime::SET.each do |each|
+ define_method "#{each.to_sym}?" do
+ to_sym == each.to_sym
end
end
View
@@ -10,9 +10,9 @@ def setup
Mime::Type.register 'image/png', :png
end
- Mime::SET.each do |type|
- define_method "test_new_#{type.to_sym}?_convenience_method" do
- assert type.send("#{type.to_sym}?")
+ Mime::SET.each do |each|
+ define_method "test_should_say_its_a_#{each.to_sym}_mime_type_when_sent_#{each.to_sym}?" do
+ assert each.send("#{each.to_sym}?")
end
end

0 comments on commit 7609bd7

Please sign in to comment.