Skip to content

Commit

Permalink
Merge branch 'master' into 0.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Dec 4, 2014
2 parents cdf2a45 + 9a1d730 commit be8cf57
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*.gem
*.rbc
.bundle
Expand Down
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: ruby
script: 'bundle exec rake test:coverage'
install:
- bundle install --retry=3
rvm:
- 2.0.0
- 2.1.0
Expand All @@ -8,8 +10,16 @@ rvm:
- 2.1.3
- 2.1.4
- 2.1.5
- 2.2
- rbx-2

matrix:
include:
- rvm: jruby
env: JRUBY_OPTS="--2.0"
- rvm: jruby-head
env: JRUBY_OPTS="--2.1"
allow_failures:
- rvm: rbx-2
- rvm: jruby
- rvm: jruby-head
2 changes: 2 additions & 0 deletions lib/lotus/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ def self.included(base)

self.configuration = conf.duplicate
end

conf.copy!(base)
end

# Load the framework
Expand Down
73 changes: 72 additions & 1 deletion lib/lotus/view/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,57 @@ def layout(value = nil)
end
end

# Specify the default modules to be included when `Lotus::View`
# is included.
#
# If not set, this option will be ignored.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload modules(blk)
# Adds the given block
# @param value [Proc] specify the modules to be included
#
# @overload modules
# Gets the value
# @return [Array] the list of the specified procs
#
# @since 0.3.0
#
# @see Lotus::View#duplicate
#
# @example Getting the value
# require 'lotus/view'
#
# Lotus::View.configuration.modules # => []
#
# @example Setting the value
# require 'lotus/view'
#
# Lotus::View.configure do
# modules do
# include MyCustomModule
# end
# end
#
# class Articles
# class Index
# include Lotus::View
#
# # It includes:
# # * MyCustomModule
# end
# end
def modules(&blk)
if block_given?
@modules.push(blk)
else
@modules
end
end

# Add a view to the registry
#
# @since 0.2.0
Expand Down Expand Up @@ -264,6 +315,7 @@ def duplicate
c.root = root
c.layout = @layout # lazy loading of the class
c.load_paths = load_paths.dup
c.modules = modules.dup
end
end

Expand All @@ -288,12 +340,31 @@ def reset!
@layouts = Set.new
@load_paths = Utils::LoadPaths.new(root)
@layout = nil
@modules = []
end

# Copy the configuration for the given action
#
# @param base [Class] the target action
#
# @return void
#
# @since 0.3.0
# @api private
def copy!(base)
modules.each do |mod|
base.class_eval(&mod)
end
end

alias_method :unload!, :reset!

protected
attr_writer :namespace, :root, :load_paths, :layout
attr_writer :namespace
attr_writer :root
attr_writer :load_paths
attr_writer :layout
attr_writer :modules
end
end
end
11 changes: 11 additions & 0 deletions lib/lotus/view/rendering/layout_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
module Lotus
module View
module Rendering
# Missing template layout error
#
# This is raised at the runtime when Lotus::Layout cannot find it's template.
#
# @since x.x.x
class MissingTemplateLayoutError < ::StandardError
def initialize(template)
super("Can't find layout template '#{ template }'")
end
end
# Holds the references of all the registered layouts.
# As now the registry is unique at the level of the framework.
#
Expand Down Expand Up @@ -48,6 +58,7 @@ def prepare!
templates.each do |template|
merge! template.format => template
end
self.any? or raise MissingTemplateLayoutError.new(@view)
end

def templates
Expand Down
21 changes: 21 additions & 0 deletions lib/lotus/view/rendering/layout_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ def initialize(layout, scope)
@layout, @scope = layout, scope
end

# Returns the classname as string
#
# @return classname
#
# @since x.x.x
def class
(class << self; self end).superclass
end

# Returns an inspect String
#
# @return [String] inspect String (contains classname, objectid in hex, available ivars)
#
# @since x.x.x
def inspect
base = "#<#{ self.class }:#{'%x' % (self.object_id << 1)}"
base << " @layout=\"#{@layout}\"" if @layout
base << " @scope=\"#{@scope}\"" if @scope
base << ">"
end

# Render a partial or a template within a layout template.
#
# @param options [Hash]
Expand Down
12 changes: 12 additions & 0 deletions lib/lotus/view/rendering/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ def initialize(view, locals = {})
@view, @locals = view, locals
end

# Returns an inspect String
#
# @return [String] inspect String (contains classname, objectid in hex, available ivars)
#
# @since x.x.x
def inspect
base = "#<#{ self.class }: #{'%x' % (self.object_id << 1)}"
base << " @view=\"#{@view}\"" if @view
base << " @locals=\"#{@locals}\"" if @locals
base << ">"
end

# Returns the requested format.
#
# @return [Symbol] the requested format (eg. :html, :json, :xml, etc..)
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,21 @@ class Parent
end
end

module MyCustomModule
end

module MyOtherCustomModule
end

module CardDeck
View = Lotus::View.duplicate(self) do
namespace CardDeck
root __dir__ + '/fixtures/templates/card_deck/app/templates'
layout :application
modules do
include MyCustomModule
include MyOtherCustomModule
end
end

class ApplicationLayout
Expand Down Expand Up @@ -270,6 +280,7 @@ def bar

module Store
View = Lotus::View.duplicate(self)
View.extend Unloadable

module Helpers
module AssetTagHelpers
Expand Down
6 changes: 6 additions & 0 deletions test/integration/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
view_configuration.layout.must_equal(CardDeck::ApplicationLayout)
end

it 'includes modules from configuration' do
modules = CardDeck::Views::Home::Index.included_modules
modules.must_include(::MyCustomModule)
modules.must_include(::MyOtherCustomModule)
end

it 'allow views to specify a layout'
# TODO move all the values into the configuration:
#
Expand Down
2 changes: 2 additions & 0 deletions test/integration/framework_freeze_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe 'Framework freeze' do
describe 'Lotus::View' do
before do
Lotus::View.unload!
Lotus::View.load!
end

Expand All @@ -25,6 +26,7 @@

describe 'duplicated framework' do
before do
Store::View.unload!
Store::View.load!
end

Expand Down
13 changes: 13 additions & 0 deletions test/layout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
end
end

it "raise error if template isn't found" do
Lotus::View.unload!

class MissingLayout
include Lotus::Layout
end

error = -> {
Lotus::View.load!
}.must_raise(Lotus::View::Rendering::MissingTemplateLayoutError)
error.message.must_include "Can't find layout template 'MissingLayout'"
end

it 'concrete methods are available in layout template' do
rendered = Store::Views::Home::Index.render(format: :html)
rendered.must_match %(script)
Expand Down
12 changes: 8 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@
root Pathname.new __dir__ + '/fixtures/templates'
end

module Unloadable
def unload!
self.configuration = configuration.duplicate
configuration.unload!
end
end

require 'fixtures'
Lotus::View.load!

Lotus::View.class_eval do
def self.unload!
self.configuration = configuration.duplicate
configuration.unload!
end
extend Unloadable
end

Lotus::Utils::LoadPaths.class_eval do
Expand Down
26 changes: 20 additions & 6 deletions test/view/rendering/layout_scope_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'test_helper'

describe Lotus::View::Rendering::LayoutScope do
describe '#respond_to?' do
before do
layout = LayoutForScopeTest.new
view_scope = Lotus::View::Rendering::Scope.new(ViewForScopeTest.new)
@scope = Lotus::View::Rendering::LayoutScope.new(layout, view_scope)
end
before do
layout = LayoutForScopeTest.new
view_scope = Lotus::View::Rendering::Scope.new(ViewForScopeTest.new)
@scope = Lotus::View::Rendering::LayoutScope.new(layout, view_scope)
end

describe '#respond_to?' do
describe 'when the layout implements the method' do
it 'returns true' do
assert @scope.respond_to?(:foo), "Expected @scope to respond to `#foo'"
Expand All @@ -26,4 +26,18 @@
end
end
end

describe '#class' do
it 'returns proper class name' do
@scope.class.must_equal Lotus::View::Rendering::LayoutScope
end
end

describe '#inspect' do
it 'returns proper inspect String' do
@scope.inspect.must_include '@layout'
@scope.inspect.must_include '@scope'
@scope.inspect.must_include '%x' % (@scope.object_id << 1)
end
end
end
16 changes: 16 additions & 0 deletions test/view/rendering/scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@
end
end
end

describe '#class' do
it 'returns proper class name' do
scope = Lotus::View::Rendering::Scope.new(ViewForScopeTest.new)
scope.class.must_equal Lotus::View::Rendering::Scope
end
end

describe '#inspect' do
it 'returns proper inspect String' do
scope = Lotus::View::Rendering::Scope.new(ViewForScopeTest.new, {x: 23})
scope.inspect.must_include '@view'
scope.inspect.must_include '@locals'
scope.inspect.must_include '%x' % (scope.object_id << 1)
end
end
end

0 comments on commit be8cf57

Please sign in to comment.