Skip to content

Commit

Permalink
Move template handler registration into railtie
Browse files Browse the repository at this point in the history
  • Loading branch information
rwz committed Oct 18, 2014
1 parent 973b382 commit c8acc5c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 1 addition & 2 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,5 @@ def _blank?(value=@attributes)
end
end

require 'jbuilder/jbuilder_template' if defined?(ActionView::Template)
require 'jbuilder/dependency_tracker'
require 'jbuilder/railtie' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 4
require 'jbuilder/railtie' if defined?(Rails)
2 changes: 0 additions & 2 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,3 @@ def self.call(template)
json.target! unless (__already_defined && __already_defined != "method")}
end
end

ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
19 changes: 14 additions & 5 deletions lib/jbuilder/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
require 'rails/railtie'
require 'jbuilder/jbuilder_template'

class Jbuilder
class Railtie < ::Rails::Railtie
generators do |app|
Rails::Generators.configure! app.config.generators
Rails::Generators.hidden_namespaces.uniq!
require 'generators/rails/scaffold_controller_generator'
initializer :jbuilder do |app|
ActiveSupport.on_load :action_view do
ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
end
end

if Rails::VERSION::MAJOR == 4
generators do |app|
Rails::Generators.configure! app.config.generators
Rails::Generators.hidden_namespaces.uniq!
require 'generators/rails/scaffold_controller_generator'
end
end
end
end
end
3 changes: 2 additions & 1 deletion test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'active_support/cache'
require 'jbuilder/jbuilder_template'


BLOG_POST_PARTIAL = <<-JBUILDER
json.extract! blog_post, :id, :body
json.author do
Expand All @@ -25,6 +24,8 @@
BLOG_POST_COLLECTION = 10.times.map{ |i| BlogPost.new(i+1, "post body #{i+1}", blog_authors.next) }
COLLECTION_COLLECTION = 5.times.map{ |i| Collection.new(i+1, "collection #{i+1}") }

ActionView::Template.register_template_handler :jbuilder, JbuilderHandler

module Rails
def self.cache
@cache ||= ActiveSupport::Cache::MemoryStore.new
Expand Down

0 comments on commit c8acc5c

Please sign in to comment.