Skip to content

Commit

Permalink
Merge branch 'master' into 0.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Trung Lê committed Oct 16, 2015
2 parents 321ca76 + 1b83cae commit 49ee1f5
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 31 deletions.
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ language: ruby
script: 'bundle exec rake test:coverage'
sudo: false
cache: bundler
install: true
env:
global:
- JRUBY_OPTS=--dev
script:
- 'if [[ "$TRAVIS_RUBY_VERSION" =~ "jruby" ]]; then rvm get head && rvm reload && rvm use --install $TRAVIS_RUBY_VERSION; fi'
- 'bundle install'
- 'bundle exec rake test:coverage'
rvm:
- 2.0.0
- 2.1.0
Expand All @@ -16,13 +24,11 @@ rvm:
- 2.2.1
- 2.2.2
- 2.2.3
- jruby-9000
- jruby-head
- rbx-2

matrix:
include:
- rvm: jruby-head
- rvm: jruby-9000
allow_failures:
- rvm: rbx-2
- rvm: jruby-head
- rvm: jruby-9000
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ unless ENV['TRAVIS']
gem 'yard', require: false
end

gem 'lotus-utils', '~> 0.5', require: false, github: 'lotus/utils', branch: '0.5.x'
gem 'lotus-utils', '~> 0.6', require: false, github: 'lotus/utils', branch: '0.6.x'
gem 'haml', require: false
gem 'simplecov', require: false
gem 'coveralls', require: false
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ Lotus::View.configure do
#
root '/path/to/root'

# Default encoding for templates
# Argument: String, defaults to utf-8
#
default_encoding 'koi-8'

# Set the Ruby namespace where to lookup for views
# Argument: Class, Module, String, defaults to Object
#
Expand Down
8 changes: 4 additions & 4 deletions lib/lotus/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ def self.included(base)
conf.add_view(base)

base.class_eval do
extend Inheritable.dup
extend Dsl.dup
extend Rendering.dup
extend Escape.dup
extend Inheritable
extend Dsl
extend Rendering
extend Escape

include Utils::ClassAttribute
class_attribute :configuration
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
4 changes: 2 additions & 2 deletions lib/lotus/view/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ def format(value = nil)
# Bookshelf::Api::Views::Books::Index.template # => 'books/index'
def template(value = nil)
if value.nil?
@@template ||= Rendering::TemplateName.new(name, configuration.namespace).to_s
@template ||= Rendering::TemplateName.new(name, configuration.namespace).to_s
else
@@template = value
@template = value
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/lotus/view/inheritable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def inherited(base)
# @api private
# @since 0.1.0
def subclasses
@@subclasses ||= Set.new
@subclasses ||= Set.new
end

protected
Expand All @@ -47,7 +47,7 @@ def load!
# @api private
# @since 0.1.0
def views
@@views ||= [ self ] + subclasses.to_a
@views ||= [ self ] + subclasses.to_a
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/view/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def load!
#
# @see Lotus::View::Rendering::Registry
def registry
@@registry ||= Registry.new(self)
@registry ||= Registry.new(self)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/view/rendering/partial_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PartialFinder < TemplateFinder
# @api private
def find
if path = partial_template_under_view_path
View::Template.new path
View::Template.new(path, @view.configuration.default_encoding)
else
super
end
Expand Down
13 changes: 12 additions & 1 deletion lib/lotus/view/rendering/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +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
template.render(scope)
(template or raise_missing_template_error).render(scope)
end

protected
Expand All @@ -50,6 +52,15 @@ def template
def scope
Scope.new(@view, @options[:locals])
end

# @since x.x.x
# @api private
def raise_missing_template_error
raise MissingTemplateError.new(
@options.fetch(:template) { @options.fetch(:partial, nil) },
@options[:format]
)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/view/rendering/templates_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def initialize(view)
# # => [#<Lotus::View::Template:0x007f8a0a86a970 ... @file="/path/to/templates/articles/show.html.erb">]
def find
_find.map do |template|
View::Template.new(template)
View::Template.new(template, @view.configuration.default_encoding)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/lotus/view/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module View
#
# @since 0.1.0
class Template
def initialize(template)
@_template = Tilt.new(template)
def initialize(template, encoding)
@_template = Tilt.new(template, encoding)
end

# Returns the format that the template handles.
Expand Down
2 changes: 1 addition & 1 deletion lotus-view.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.0.0'

spec.add_runtime_dependency 'tilt', '~> 2.0', '>= 2.0.1'
spec.add_runtime_dependency 'lotus-utils', '~> 0.5'
spec.add_runtime_dependency 'lotus-utils', '~> 0.6'

spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'minitest', '~> 5'
Expand Down
2 changes: 1 addition & 1 deletion test/escape_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'Escape' do
before do
path = Pathname.new(__dir__ + '/fixtures/templates/users/show.html.erb')
template = Lotus::View::Template.new(path)
template = Lotus::View::Template.new(path, 'utf-8')

@user = User.new(%(<script>alert('username')</script>))
@book = Book.new(%(<script>alert('title')</script>))
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def each_thing
end
end

class RenderViewWithMissingPartialTemplate
include Lotus::View
end

class EncodingView
include Lotus::View
end

class JsonRenderView
include Lotus::View
format :json
Expand Down Expand Up @@ -209,6 +217,7 @@ def map

class IndexView
include Lotus::View

layout :application
end

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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: 'shared/missing_template' %>
12 changes: 12 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 Expand Up @@ -107,6 +112,13 @@
rendered.must_match %(<input type="hidden" name="secret" value="23" />)
end

it 'raises an error when the partial template is missing' do
-> {
RenderViewWithMissingPartialTemplate.render(format: :html)
}.must_raise(Lotus::View::MissingTemplateError)
.message.must_match("Can't find template 'shared/missing_template' for 'html' format.")
end

# @issue https://github.com/lotus/view/issues/3
it 'renders a template within another template' do
parent = OpenStruct.new(children: [], name: 'parent')
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler/setup'
require 'tilt/erb'

if ENV['COVERAGE'] == 'true'
require 'simplecov'
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 49ee1f5

Please sign in to comment.