diff --git a/.gitignore b/.gitignore index 7b0db99..f29db7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .bundle *.gem +Gemfile.lock diff --git a/Gemfile b/Gemfile index 1aa98e4..8caa1b7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,4 @@ source "http://rubygems.org" +gem 'rake' +gem 'sqlite3' gemspec diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index c1f208f..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,28 +0,0 @@ -PATH - remote: . - specs: - liquidizer (0.5.4) - actionpack (~> 2.3.5) - activerecord (~> 2.3.5) - liquid (>= 2.0.0) - -GEM - remote: http://rubygems.org/ - specs: - actionpack (2.3.8) - activesupport (= 2.3.8) - rack (~> 1.1.0) - activerecord (2.3.8) - activesupport (= 2.3.8) - activesupport (2.3.8) - liquid (2.2.2) - rack (1.1.0) - -PLATFORMS - ruby - -DEPENDENCIES - actionpack (~> 2.3.5) - activerecord (~> 2.3.5) - liquid (>= 2.0.0) - liquidizer! diff --git a/lib/liquidizer/controller_extensions.rb b/lib/liquidizer/controller_extensions.rb index ff3cbc9..96e1b13 100644 --- a/lib/liquidizer/controller_extensions.rb +++ b/lib/liquidizer/controller_extensions.rb @@ -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] @@ -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) @@ -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 @@ -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 diff --git a/lib/liquidizer/version.rb b/lib/liquidizer/version.rb index 4cda24b..18c74fe 100644 --- a/lib/liquidizer/version.rb +++ b/lib/liquidizer/version.rb @@ -1,3 +1,3 @@ module Liquidizer - VERSION = '0.5.4' + VERSION = '0.6.0' end diff --git a/liquidizer.gemspec b/liquidizer.gemspec index 5463d6e..c0ac50d 100644 --- a/liquidizer.gemspec +++ b/liquidizer.gemspec @@ -1,4 +1,3 @@ -# -*- encoding: utf-8 -*- lib = File.expand_path('../lib/', __FILE__) $:.unshift lib unless $:.include?(lib) @@ -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' @@ -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}/**/*') diff --git a/test/controller_extensions_test.rb b/test/controller_extensions_test.rb index 7ceb7c3..e7dfdda 100644 --- a/test/controller_extensions_test.rb +++ b/test/controller_extensions_test.rb @@ -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 => '
{{ content_for_layout }}
') + render 'posts/show' + end + +end + class PostsController < BaseController layout 'layout' liquify @@ -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) diff --git a/test/fixtures/rails/edit.html.erb b/test/fixtures/rails/edit.html.erb new file mode 100644 index 0000000..79cd8d1 --- /dev/null +++ b/test/fixtures/rails/edit.html.erb @@ -0,0 +1 @@ +

Rails edit action

diff --git a/test/fixtures/rails/update.html.erb b/test/fixtures/rails/update.html.erb new file mode 100644 index 0000000..6b32361 --- /dev/null +++ b/test/fixtures/rails/update.html.erb @@ -0,0 +1 @@ +

Rails update action