Skip to content

Commit

Permalink
Merge 77afad2 into c57b8db
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Oct 16, 2015
2 parents c57b8db + 77afad2 commit c9a8100
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ language: ruby
script: 'bundle exec rake test:coverage'
sudo: false
cache: bundler
before_script:
- locale
- env
- file --mime test/fixtures/templates/encoding_view.html.erb
rvm:
- 2.0.0
- 2.1.0
Expand Down
29 changes: 23 additions & 6 deletions lib/lotus/view/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class Configuration
# @api private
DEFAULT_ROOT = '.'.freeze

# Default encoding
#
# @since x.x.x
# @api private
DEFAULT_ENCODING = 'utf-8'.freeze

attr_reader :load_paths
attr_reader :views
attr_reader :layouts
Expand Down Expand Up @@ -237,6 +243,14 @@ def layout(value = nil)
end
end

def default_encoding(value = nil)
if value.nil?
@default_encoding
else
@default_encoding = value.to_s
end
end

# Prepare the views.
#
# The given block will be yielded when `Lotus::View` will be included by
Expand Down Expand Up @@ -347,11 +361,12 @@ def add_layout(layout)
# @api private
def duplicate
Configuration.new.tap do |c|
c.namespace = namespace
c.root = root
c.layout = @layout # lazy loading of the class
c.load_paths = load_paths.dup
c.modules = modules.dup
c.namespace = namespace
c.root = root
c.layout = @layout # lazy loading of the class
c.default_encoding = default_encoding
c.load_paths = load_paths.dup
c.modules = modules.dup
end
end

Expand All @@ -370,7 +385,8 @@ def load!
# @since 0.2.0
# @api private
def reset!
root(DEFAULT_ROOT)
root DEFAULT_ROOT
default_encoding DEFAULT_ENCODING

@views = Set.new
@layouts = Set.new
Expand Down Expand Up @@ -400,6 +416,7 @@ def copy!(base)
attr_writer :root
attr_writer :load_paths
attr_writer :layout
attr_writer :default_encoding
attr_writer :modules
end
end
Expand Down
15 changes: 9 additions & 6 deletions lib/lotus/view/rendering/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def initialize(view, options)
#
# @return [String] the output of the rendering process.
#
# @raise [Lotus::View::MissingTemplateError] if template can't be found
#
# @api private
# @since 0.1.0
def render
found_template = template
raise_missing_template_error if found_template.nil?
found_template.render(scope)
(template or raise_missing_template_error).render(scope)
end

protected
Expand All @@ -53,10 +53,13 @@ def scope
Scope.new(@view, @options[:locals])
end

# @since x.x.x
# @api private
def raise_missing_template_error
template_name = @options[:template] if @options.key?(:template)
template_name = @options[:partial] if @options.key?(:partial)
raise MissingTemplateError.new(template_name, @options[:format])
raise MissingTemplateError.new(
@options.fetch(:template) { @options.fetch(:partial, nil) },
@options[:format]
)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class RenderViewWithMissingPartialTemplate
include Lotus::View
end

class EncodingView
include Lotus::View
end

class JsonRenderView
include Lotus::View
format :json
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/templates/encoding_view.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div id="header" class="col-md-12">
<div class="container">
<!-- CONFIGURAÇÃO -->
<li class="menu-level-1">
<a href="/configs">Configuração</a>
</li>
<!-- / CONFIGURAÇÃO -->
</div>
</div>
5 changes: 5 additions & 0 deletions test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
RenderView.render(format: :html, planet: 'Mars').must_include %(<h1>Hello, Mars!</h1>)
end

# See https://github.com/lotus/view/issues/76
it 'renders a template with different encoding' do
EncodingView.render(format: :html).must_include %(Configuração)
end

it 'renders a template according to the declared format' do
JsonRenderView.render(format: :json, planet: 'Moon').must_include %("greet":"Hello, Moon!")
end
Expand Down
26 changes: 22 additions & 4 deletions test/view/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ class LazyLayout
end
end

describe "#default_encoding" do
it 'defaults to "utf-8"' do
@configuration.default_encoding.must_equal "utf-8"
end

it 'allows to set different value' do
@configuration.default_encoding "koi-8"
@configuration.default_encoding.must_equal "koi-8"
end
end

describe '#prepare' do
before do
module FooRendering
Expand Down Expand Up @@ -182,6 +193,7 @@ class PrepareView
@configuration.root 'test'
@configuration.load_paths << '..'
@configuration.layout :application
@configuration.default_encoding 'latin-1'
@configuration.add_view(HelloWorldView)
@configuration.add_layout(ApplicationLayout)
@configuration.prepare { include Kernel }
Expand All @@ -190,10 +202,11 @@ class PrepareView
end

it 'returns a copy of the configuration' do
@config.root.must_equal @configuration.root
@config.load_paths.must_equal @configuration.load_paths
@config.layout.must_equal @configuration.layout
@config.modules.must_equal @configuration.modules
@config.root.must_equal @configuration.root
@config.load_paths.must_equal @configuration.load_paths
@config.layout.must_equal @configuration.layout
@config.default_encoding.must_equal @configuration.default_encoding
@config.modules.must_equal @configuration.modules
@config.views.must_be_empty
@config.layouts.must_be_empty
end
Expand All @@ -202,6 +215,7 @@ class PrepareView
@config.root '.'
@config.load_paths << '../..'
@config.layout :global
@config.default_encoding 'iso-8859'
@config.add_view(RenderView)
@config.add_layout(GlobalLayout)
@config.prepare { include Comparable }
Expand All @@ -223,6 +237,8 @@ class PrepareView
@configuration.load_paths.must_include '..'
@configuration.load_paths.wont_include '../..'

@configuration.default_encoding.must_equal 'latin-1'

@configuration.layout.must_equal ApplicationLayout

@configuration.views.must_include HelloWorldView
Expand Down Expand Up @@ -317,6 +333,7 @@ class MockView < MockLayout
@configuration.root 'test'
@configuration.load_paths << '..'
@configuration.layout :application
@configuration.default_encoding 'Windows-1253'
@configuration.add_view(HelloWorldView)
@configuration.add_layout(ApplicationLayout)

Expand All @@ -329,6 +346,7 @@ class MockView < MockLayout
@configuration.root.must_equal root
@configuration.load_paths.must_include root
@configuration.layout.must_equal Lotus::View::Rendering::NullLayout
@configuration.default_encoding 'utf-8'
@configuration.views.must_be_empty
@configuration.layouts.must_be_empty
end
Expand Down

0 comments on commit c9a8100

Please sign in to comment.