Skip to content

Commit

Permalink
Remove rendering partial outside of Rails.
Browse files Browse the repository at this point in the history
Implementation was partial and completing it requires to rely on internal of
action_view.
  • Loading branch information
ccocchi committed Apr 9, 2016
1 parent 09e6278 commit 12a3f0f
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 204 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Drop support for Rails < 4.2
* Replace `thread_safe` with `concurrent_ruby`
* Remove custom responder
* Remove rendering outside of Rails

## 0.4.3
* Fix custom responder compatibility with responders 2.1 (itkin)
Expand Down
16 changes: 10 additions & 6 deletions lib/rabl-rails.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
require 'rails/railtie'

require 'active_support'

require 'rabl-rails/version'
require 'rabl-rails/helpers'
require 'rabl-rails/exceptions'
require 'rabl-rails/template'
require 'rabl-rails/nodes'
require 'rabl-rails/compiler'

require 'rabl-rails/visitors'
require 'rabl-rails/renderer'
require 'rabl-rails/renderers/hash'
require 'rabl-rails/renderers/json'
require 'rabl-rails/renderers/xml'
require 'rabl-rails/renderers/plist'
require 'rabl-rails/library'

require 'rabl-rails/handler'
require 'rabl-rails/railtie'

if defined?(Rails)
require 'rails/railtie'
require 'rabl-rails/railtie'
end

require 'rabl-rails/configuration'

Expand All @@ -25,8 +31,6 @@
end

module RablRails
extend Renderer

class << self
def configure
yield configuration
Expand Down
3 changes: 3 additions & 0 deletions lib/rabl-rails/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module RablRails
class PartialError < StandardError; end
end
97 changes: 0 additions & 97 deletions lib/rabl-rails/renderer.rb

This file was deleted.

3 changes: 1 addition & 2 deletions lib/rabl-rails/renderers/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module Hash
def render(template, context, locals = nil)
visitor = Visitors::ToHash.new(context)

collection_or_resource = locals[:resource] if locals
collection_or_resource ||= if template.data
collection_or_resource = if template.data
if context.respond_to?(template.data)
context.send(template.data)
else
Expand Down
4 changes: 2 additions & 2 deletions lib/rabl-rails/visitors/to_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def visit_Code n
result = instance_exec _resource, &(n.block)

if n.merge?
raise RablRails::Renderer::PartialError, '`merge` block should return a hash' unless result.is_a?(Hash)
raise RablRails::PartialError, '`merge` block should return a hash' unless result.is_a?(Hash)
@_result.merge!(result)
else
@_result[n.name] = result
Expand Down Expand Up @@ -98,7 +98,7 @@ def locals
# rendering time).
#
def partial(template_path, options = {})
raise RablRails::Renderer::PartialError.new("No object was given to partial #{template_path}") unless options[:object]
raise RablRails::PartialError.new("No object was given to partial #{template_path}") unless options[:object]
object = options[:object]
@_locals = options[:locals].freeze

Expand Down
12 changes: 3 additions & 9 deletions test/renderers/test_hash_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

class TestHashRenderer < Minitest::Test
describe 'hash renderer' do
def render(locals = nil)
RablRails::Renderers::Hash.render(@template, @context, locals)
def render
RablRails::Renderers::Hash.render(@template, @context, {})
end

def with_cache
Expand Down Expand Up @@ -71,12 +71,6 @@ def @resource.cache_key; 'marty_cache' end
assert_equal({ name: 'Marty' }, render)
end

it "uses resource passed in locals if template don't have data" do
@template.data = nil
resource = User.new(2, 'Biff')
assert_equal({ name: 'Biff' }, render(resource: resource))
end

it 'uses template root_name option' do
@template.root_name = :user
assert_equal({ user: { name: 'Marty' } }, render)
Expand All @@ -87,4 +81,4 @@ def @resource.cache_key; 'marty_cache' end
assert_equal([{ name: 'Marty' }], render)
end
end
end
end
4 changes: 2 additions & 2 deletions test/test_hash_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def @context.context_method; end
it 'raises an exception when trying to merge a non hash object' do
proc = ->(c) { c.name }
@nodes << RablRails::Nodes::Code.new(nil, proc)
assert_raises(RablRails::Renderer::PartialError) { visitor_result }
assert_raises(RablRails::PartialError) { visitor_result }
end

it 'renders partial defined in node' do
Expand Down Expand Up @@ -216,7 +216,7 @@ def @context.context_method; end
it 'raises an exception when calling a partial without a target' do
proc = ->(u) { partial('users/base') }
@nodes << RablRails::Nodes::Code.new(:user, proc)
assert_raises(RablRails::Renderer::PartialError) { visitor_result }
assert_raises(RablRails::PartialError) { visitor_result }
end

describe 'when hash options are set' do
Expand Down
86 changes: 0 additions & 86 deletions test/test_render.rb

This file was deleted.

0 comments on commit 12a3f0f

Please sign in to comment.