Permalink
Browse files

add support for Rails 3.2+ sending custom mime types

  • Loading branch information...
1 parent a1c4164 commit b49fe6ae1f66fea7a48bed72aef7126dd70619aa @databyte databyte committed Jul 9, 2012
@@ -0,0 +1,7 @@
+object @post
+
+attribute :title => :title_v1
+
+child :user do
+ extends "users/show"
+end
@@ -0,0 +1,3 @@
+object @user => :person
+
+attribute :username => :username_v1
@@ -1,5 +1,5 @@
class PostsController < ApplicationController
- respond_to :json, :xml, :html
+ respond_to :json, :xml, :html, :rabl_test_v1
def index
@posts = Post.all(:order => "id ASC")
@@ -3,3 +3,5 @@
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone
+
+Mime::Type.register 'application/vnd.rabl-test_v1+json', :rabl_test_v1
View
@@ -71,6 +71,10 @@ def fetch_rails_source(file, options={})
if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2
context_scope.lookup_context.find(file, [], partial)
else # Rails 3.2 and higher
+ # pull format directly from rails unless it is html
+ rendered_format = context_scope.lookup_context.rendered_format
+ source_format = rendered_format unless rendered_format == :html
+
context_scope.lookup_context.find(file, [], partial, [], {:formats => [source_format]})
end }
template = lookup_proc.call(false) rescue lookup_proc.call(true)
@@ -154,6 +154,15 @@
asserts(:body).includes { "<html>" }
end # show action, html
+ context "mime_type" do
+ setup do
+ get "/posts/#{@post1.id}", format: :rabl_test_v1
+ end
+
+ asserts("contains post title") { json_output['post']['title_v1'] }.equals { @post1.title }
+ asserts("contains username") { json_output['post']['user']['username_v1'] }.equals { @post1.user.username }
+ end
+
context "caching" do
helper(:cache_hit) do |key|
Rails.cache.read(ActiveSupport::Cache.expand_cache_key(key, :rabl))

0 comments on commit b49fe6a

Please sign in to comment.