Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.bundle
*.gem
Gemfile.lock
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
source "http://rubygems.org"
gem 'rake'
gem 'sqlite3'
gemspec
28 changes: 0 additions & 28 deletions Gemfile.lock

This file was deleted.

69 changes: 54 additions & 15 deletions lib/liquidizer/controller_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,74 @@ def self.included(base)
extend ClassMethods
alias_method_chain :render, :liquid

class_inheritable_hash :liquidizer_options
class_inheritable_accessor :liquidizer_options
self.liquidizer_options ||= {}

before_filter :set_liquid_file_system
end
end

def render_with_liquid(options = nil, &block)
if view_template = liquid_template_for_view(options)
options ||= {}
def render_with_liquid(options = nil, extra_options = {}, &block)
# use normal render if "liquify" has not been called in the controller
return render_without_liquid(options, extra_options, &block) unless self.class.liquify_enabled?

liquid_options = handle_multiple_options(options, extra_options)

if view_template = liquid_template_for_view(liquid_options)
assigns = assigns_for_liquify
content = view_template.render!(assigns)

if layout_template = liquid_template_for_layout(options)
if layout_template = liquid_template_for_layout(liquid_options)
content = layout_template.render!(assigns.merge('content_for_layout' => content))
options[:layout] = false
liquid_options[:layout] = false
end

render_without_liquid(options.merge(:text => content))
render_without_liquid(liquid_options.merge(:text => content))
else
if layout_template = liquid_template_for_layout(options)
if layout_template = liquid_template_for_layout(liquid_options)
assigns = assigns_for_liquify
options ||= {}

content = render_to_string(options.merge(:layout => false))
content = render_to_string(liquid_options.merge(:layout => false))
content = layout_template.render!(assigns.merge('content_for_layout' => content))

render_without_liquid(options.merge(:text => content, :layout => false))
render_without_liquid(liquid_options.merge(:text => content, :layout => false))
else
render_without_liquid(options, &block)
render_without_liquid(options, extra_options, &block)
end
end
end

private

# taken from Rails #render
def handle_multiple_options options, extra_options
extra_options = extra_options.dup

if options.nil?
# Rails fetch default template, but that is not possible (there is no template)
options = extra_options

elsif options.is_a?(String) || options.is_a?(Symbol)
case options.to_s.index('/')
when 0
extra_options[:file] = options
when nil
extra_options[:action] = options
else
extra_options[:template] = options
end

options = extra_options

elsif !options.is_a?(Hash)
extra_options[:partial] = options

options = extra_options
end

options
end

def liquid_template_for_view(options)
name = options && options[:template]

Expand All @@ -70,8 +102,6 @@ def liquify?(action)
end

def liquid_template_for_layout(options)
options ||= {}

if liquify_layout?(options)
name = liquid_template_name_for_layout(options)
name && find_and_parse_liquid_template(name)
Expand Down Expand Up @@ -127,7 +157,7 @@ def liquid_template_name_for_action(action)
end

def liquid_template_name_for_layout(options)
options[:layout] || case layout = self.class.read_inheritable_attribute(:layout)
options[:layout] || @_liquid_layout || case layout = self.class.read_inheritable_attribute(:layout)
when Symbol then __send__(layout)
when Proc then layout.call(self)
else layout
Expand Down Expand Up @@ -206,6 +236,15 @@ module ClassMethods
def liquify(options = {})
self.liquidizer_options = options.reverse_merge(:actions => true, :layout => true)
end

def remove_liquify
self.liquidizer_options.clear
end

def liquify_enabled?
self.liquidizer_options.present?
end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/liquidizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Liquidizer
VERSION = '0.5.4'
VERSION = '0.6.0'
end
7 changes: 3 additions & 4 deletions liquidizer.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)

Expand All @@ -8,7 +7,7 @@ Gem::Specification.new do |s|
s.name = 'liquidizer'
s.version = Liquidizer::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Adam Cigánek"]
s.authors = ["Adam Ciganek"]
s.email = 'adam.ciganek@gmail.com'
s.homepage = 'http://github.com/madadam/liquidizer'
s.summary = 'Support for Ruby on Rails views powered by Liquid and loaded from database'
Expand All @@ -18,8 +17,8 @@ END

s.required_rubygems_version = ">= 1.3.7"

s.add_dependency 'actionpack', '~> 2.3.5'
s.add_dependency 'activerecord', '~> 2.3.5'
s.add_dependency 'actionpack'
s.add_dependency 'activerecord'
s.add_dependency 'liquid', '>= 2.0.0'

s.files = Dir.glob('{lib,bin}/**/*')
Expand Down
57 changes: 57 additions & 0 deletions test/controller_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ def current_liquid_templates
end
end

class RailsController < BaseController

layout 'login_layout', :only => [:login]
liquify :only => [:login]

def show
render :edit, :layout => false
end

def edit
render 'posts/show'
end

def update
render
end

def login
LiquidTemplate.create!(:name => 'login_layout',
:content => '<div id="login_layout">{{ content_for_layout }}</div>')
render 'posts/show'
end

end

class PostsController < BaseController
layout 'layout'
liquify
Expand Down Expand Up @@ -315,6 +340,38 @@ def teardown
assert_select 'p', 'This is a partial'
end

test 'call to render with symbol' do
setup_controller(RailsController)

get :show
assert_select 'p', 'Rails edit action'
assert_select '#layout', false
end

test 'call to render with template name' do
setup_controller(RailsController)

get :edit
assert_select 'p', 'This is not liquid template'
end

test 'call to render without anything' do
setup_controller(RailsController)

get :update
assert_select 'p', 'Rails update action'
end

test 'call to render with template and liquid layout' do
setup_controller(RailsController)

get :login
assert_select 'p', 'This is not liquid template'

assert_select '#layout', false
assert_select '#login_layout', true
end

private

def setup_controller(controller_class)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/rails/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Rails edit action</p>
1 change: 1 addition & 0 deletions test/fixtures/rails/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Rails update action</p>