Skip to content

Commit

Permalink
add support for Rails 3.2+ sending custom mime types
Browse files Browse the repository at this point in the history
  • Loading branch information
databyte committed Jul 9, 2012
1 parent a1c4164 commit b49fe6a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions fixtures/ashared/views_rails_3/posts/show.rabl_test_v1.rabl
@@ -0,0 +1,7 @@
object @post

attribute :title => :title_v1

child :user do
extends "users/show"
end
3 changes: 3 additions & 0 deletions fixtures/ashared/views_rails_3/users/show.rabl_test_v1.rabl
@@ -0,0 +1,3 @@
object @user => :person

attribute :username => :username_v1
2 changes: 1 addition & 1 deletion fixtures/rails3_2/app/controllers/posts_controller.rb
@@ -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")
Expand Down
2 changes: 2 additions & 0 deletions fixtures/rails3_2/config/initializers/mime_types.rb
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/rabl/partials.rb
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/integration/rails3_2/posts_controller_test.rb
Expand Up @@ -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))
Expand Down

0 comments on commit b49fe6a

Please sign in to comment.