forked from dewski/json_builder
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'active_support/notifications' | ||
|
||
module JSONBuilder | ||
class Partial | ||
class MissingTemplate < StandardError; end; | ||
|
||
attr_accessor :object | ||
attr_accessor :options | ||
attr_accessor :path | ||
|
||
def initialize(*args) | ||
@options = args.extract_options! | ||
@object = args.first | ||
|
||
setup @object, @options | ||
end | ||
|
||
def setup(object, options) | ||
if (Object.const_defined?('ActiveRecord') && ActiveRecord::Base === object.superclass) || object.respond_to?(:to_partial_path) | ||
begin | ||
@path = object.to_partial_path | ||
rescue | ||
raise ArgumentError.new("'#{object.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.") | ||
end | ||
end | ||
|
||
if String === object | ||
@path = object | ||
end | ||
end | ||
|
||
def source | ||
instrument :template do | ||
begin | ||
File.read @path | ||
rescue Errno::ENOENT | ||
raise MissingTemplate | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def instrument(name, options = {}) | ||
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options) { yield } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
embedded "partial" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# encoding: UTF-8 | ||
|
||
require 'test_helper' | ||
|
||
class User | ||
def to_partial_path | ||
'users' | ||
end | ||
end | ||
|
||
class TestPartial < Test::Unit::TestCase | ||
def partial(*args) | ||
JSONBuilder::Partial.new(*args) | ||
end | ||
|
||
def test_partial_file_path_from_model | ||
assert_equal 'users', partial(User.new).path | ||
end | ||
|
||
def test_passed_in_partial_path | ||
assert_equal 'custom/path', partial('custom/path').path | ||
end | ||
|
||
def test_missing_partial_file | ||
assert_raise(JSONBuilder::Partial::MissingTemplate) { | ||
partial('test/me').source | ||
} | ||
end | ||
end |