adjustable_mime_type 0.0.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/adjustable_mime_type.gemspec +1 -1
- data/lib/adjustable_mime_type.rb +16 -8
- data/rails/init.rb +2 -3
- data/spec/adjustable_mime_type_spec.rb +40 -2
- data/spec/spec_helper.rb +12 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
1.0.0
|
data/lib/adjustable_mime_type.rb
CHANGED
@@ -1,21 +1,29 @@
|
|
1
1
|
module AdjustableMimeType
|
2
|
+
def self.init!
|
3
|
+
::Mime::Type.send :include, AdjustableMimeType::RegisterAliasExtension
|
4
|
+
::ActionController::Base.send :include, AdjustableMimeType::ControllerFilter
|
5
|
+
end
|
2
6
|
|
3
7
|
module RegisterAliasExtension
|
4
8
|
def self.included(base)
|
5
9
|
base.send :extend, ClassMethods
|
10
|
+
base.class_eval do
|
11
|
+
class << self
|
12
|
+
alias_method_chain :register_alias, :adjustable_format
|
13
|
+
end
|
14
|
+
end
|
6
15
|
end
|
7
16
|
|
8
17
|
module ClassMethods
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
format
|
18
|
+
def register_alias_with_adjustable_format(string, symbol, extension_synonyms=[], &block)
|
19
|
+
adjustable_formats[symbol] = block
|
20
|
+
register_alias_without_adjustable_format(string, symbol, extension_synonyms)
|
13
21
|
end
|
14
22
|
|
15
23
|
def adjust_format(request)
|
16
|
-
if
|
17
|
-
symbol, block =
|
18
|
-
request.format =
|
24
|
+
if adjustable_format = adjustable_formats.find{|format, block| block.call(request) }
|
25
|
+
symbol, block = adjustable_format
|
26
|
+
request.format = symbol
|
19
27
|
end
|
20
28
|
end
|
21
29
|
|
@@ -35,7 +43,7 @@ module AdjustableMimeType
|
|
35
43
|
private
|
36
44
|
# Adjusts the incoming format based aliases with defined request blocks
|
37
45
|
def adjust_mime_type_format
|
38
|
-
Mime::Type.adjust_format(request)
|
46
|
+
Mime::Type.adjust_format(request) unless params.include?(:format)
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
data/rails/init.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '
|
1
|
+
require File.join(File.dirname(__FILE__), '/../lib/adjustable_mime_type')
|
2
2
|
|
3
|
-
|
4
|
-
ActionController::Base.send :include, AdjustableMimeType::ControllerFilter
|
3
|
+
AdjustableMimeType.init!
|
@@ -1,7 +1,45 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
+
AdjustableMimeType.init!
|
4
|
+
|
5
|
+
Mime::Type.register_alias "text/html", :iphone do |req|
|
6
|
+
req.user_agent =~ /Mobile.*Safari/
|
7
|
+
end
|
8
|
+
|
9
|
+
class PenguinsController < ActionController::Base
|
10
|
+
def index
|
11
|
+
respond_to do |wants|
|
12
|
+
wants.html { render :text => 'html' }
|
13
|
+
wants.iphone { render :text => 'iphone' }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
3
18
|
describe "AdjustableMimeType" do
|
4
|
-
|
5
|
-
|
19
|
+
before(:each) do
|
20
|
+
@controller = PenguinsController.new
|
21
|
+
@request = ActionController::TestRequest.new
|
22
|
+
@response = ActionController::TestResponse.new
|
23
|
+
|
24
|
+
ActionController::Routing::Routes.draw do |map|
|
25
|
+
map.resources :penguins
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should adjust format" do
|
30
|
+
@request.user_agent = 'Mobile Safari'
|
31
|
+
get :index
|
32
|
+
@response.body.should eql('iphone')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not adjust format" do
|
36
|
+
get :index
|
37
|
+
@response.body.should eql('html')
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not ovveride explicit .formats" do
|
41
|
+
@request.user_agent = 'Mobile Safari'
|
42
|
+
get :index, :format => 'html'
|
43
|
+
@response.body.should eql('html')
|
6
44
|
end
|
7
45
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
|
3
|
+
|
4
4
|
require 'spec'
|
5
5
|
require 'spec/autorun'
|
6
6
|
|
7
|
+
# Force RoR 2.3.x
|
8
|
+
require 'rubygems'
|
9
|
+
gem 'actionpack', '2.3.5'
|
10
|
+
|
11
|
+
require 'action_controller'
|
12
|
+
require 'action_controller/test_process'
|
13
|
+
|
14
|
+
require 'adjustable_mime_type'
|
15
|
+
|
16
|
+
include ActionController::TestProcess
|
17
|
+
|
7
18
|
Spec::Runner.configure do |config|
|
8
|
-
|
9
19
|
end
|