implement the path_to liquid tag
did
committed Nov 17, 2013
commit 759c39786647ec208cef37d369d798f6ddebb154
Showing 7
changed files with
139 additions
and 139 deletions
locomotive/wagon/liquid.rb b/lib/locomotive/wagon/liquid.rb
+1
-1
| @@ | @@ -3,7 +3,7 @@ require 'locomotive/mounter' |
| require 'locomotive/wagon/liquid/scopeable' | |
| require 'locomotive/wagon/liquid/drops/base' | |
| require 'locomotive/wagon/liquid/tags/hybrid' | |
| - | require 'locomotive/wagon/liquid/tags/url_helper' |
| + | require 'locomotive/wagon/liquid/tags/path_helper' |
| %w{. drops tags filters}.each do |dir| | |
| Dir[File.join(File.dirname(__FILE__), 'liquid', dir, '*.rb')].each { |lib| require lib } | |
locomotive/wagon/liquid/tags/link_to.rb b/lib/locomotive/wagon/liquid/tags/link_to.rb
+3
-3
| @@ | @@ -6,7 +6,7 @@ module Locomotive |
| Syntax = /(#{::Liquid::Expression}+)(#{::Liquid::TagAttributes}?)/ | |
| - | include UrlHelper |
| + | include PathHelper |
| def initialize(tag_name, markup, tokens, options) | |
| if markup =~ Syntax | |
| @@ | @@ -23,7 +23,7 @@ module Locomotive |
| end | |
| def render(context) | |
| - | render_url(context) do |page, url| |
| + | render_path(context) do |page, path| |
| label = label_from_page(page) | |
| if @render_as_block | |
| @@ | @@ -31,7 +31,7 @@ module Locomotive |
| label = super.html_safe | |
| end | |
| - | %{<a href="#{url}">#{label}</a>} |
| + | %{<a href="#{path}">#{label}</a>} |
| end | |
| end | |
locomotive/wagon/liquid/tags/path_helper.rb b/lib/locomotive/wagon/liquid/tags/path_helper.rb
+98
-0
| @@ | @@ -0,0 +1,98 @@ |
| + | module Locomotive |
| + | module Wagon |
| + | module Liquid |
| + | module Tags |
| + | |
| + | module PathHelper |
| + | |
| + | def render_path(context, &block) |
| + | site = context.registers[:site] |
| + | |
| + | if page = self.retrieve_page_from_handle(context) |
| + | path = self.public_page_fullpath(context, page) |
| + | |
| + | if block_given? |
| + | block.call page, path |
| + | else |
| + | path |
| + | end |
| + | else |
| + | raise Liquid::PageNotTranslated.new(%{[link_to] Unable to find a page for the #{@handle}. Wrong handle or missing template for your content.}) |
| + | end |
| + | end |
| + | |
| + | protected |
| + | |
| + | def retrieve_page_from_handle(context) |
| + | mounting_point = context.registers[:mounting_point] |
| + | |
| + | context.scopes.reverse_each do |scope| |
| + | handle = scope[@handle] || @handle |
| + | |
| + | page = case handle |
| + | when Locomotive::Mounter::Models::Page then handle |
| + | when Liquid::Drops::Page then handle.instance_variable_get(:@_source) |
| + | when String then fetch_page(mounting_point, handle) |
| + | when Liquid::Drops::ContentEntry then fetch_page(mounting_point, handle.instance_variable_get(:@_source), true) |
| + | when Locomotive::Mounter::Models::ContentEntry then fetch_page(mounting_point, handle, true) |
| + | else |
| + | nil |
| + | end |
| + | |
| + | return page unless page.nil? |
| + | end |
| + | |
| + | nil |
| + | end |
| + | |
| + | def fetch_page(mounting_point, handle, templatized = false) |
| + | ::Locomotive::Mounter.with_locale(@_options['locale']) do |
| + | if templatized |
| + | page = mounting_point.pages.values.find do |_page| |
| + | _page.templatized? && |
| + | !_page.templatized_from_parent && |
| + | _page.content_type.slug == handle.content_type.slug && |
| + | (@_options['with'].nil? || _page.handle == @_options['with']) |
| + | end |
| + | |
| + | page.content_entry = handle if page |
| + | |
| + | page |
| + | else |
| + | mounting_point.pages.values.find { |_page| _page.handle == handle } |
| + | end |
| + | end |
| + | end |
| + | |
| + | def public_page_fullpath(context, page) |
| + | mounting_point = context.registers[:mounting_point] |
| + | locale = @_options['locale'] || ::I18n.locale |
| + | |
| + | if !page.translated_in?(locale) |
| + | title = page.title_translations.values.compact.first |
| + | raise Liquid::PageNotTranslated.new(%{the "#{title}" page is not translated in #{locale.upcase}}) |
| + | end |
| + | |
| + | fullpath = ::Locomotive::Mounter.with_locale(locale) do |
| + | page.fullpath.clone |
| + | end |
| + | |
| + | fullpath = "#{::I18n.locale}/#{fullpath}" if ::I18n.locale.to_s != mounting_point.default_locale.to_s |
| + | |
| + | if page.templatized? |
| + | if page.content_entry._slug.nil? |
| + | title = %{#{page.content_entry.content_type.name.singularize} "#{page.content_entry.send(page.content_entry.content_type.label_field_name)}"} |
| + | raise Liquid::ContentEntryNotTranslated.new(%{the #{title} slug is not translated in #{locale.upcase}}) |
| + | end |
| + | fullpath.gsub!(/(content[_-]type[_-]template|template)/, page.content_entry._slug) |
| + | end |
| + | |
| + | File.join('/', fullpath) |
| + | end |
| + | |
| + | end |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| \ No newline at end of file | |
locomotive/wagon/liquid/tags/path_to.rb b/lib/locomotive/wagon/liquid/tags/path_to.rb
+36
-0
| @@ | @@ -0,0 +1,36 @@ |
| + | module Locomotive |
| + | module Wagon |
| + | module Liquid |
| + | module Tags |
| + | |
| + | class PathTo < ::Liquid::Tag |
| + | |
| + | include PathHelper |
| + | |
| + | Syntax = /(#{::Liquid::Expression}+)(#{::Liquid::TagAttributes}?)/ |
| + | |
| + | def initialize(tag_name, markup, tokens, context) |
| + | if markup =~ Syntax |
| + | @handle = $1 |
| + | @_options = {} |
| + | markup.scan(::Liquid::TagAttributes) do |key, value| |
| + | @_options[key] = value |
| + | end |
| + | else |
| + | raise SyntaxError.new("Syntax Error in 'path_to' - Valid syntax: path_to <page|page_handle|content_entry>(, locale: [fr|de|...], with: <page_handle>") |
| + | end |
| + | |
| + | super |
| + | end |
| + | |
| + | def render(context) |
| + | render_path(context) |
| + | end |
| + | |
| + | end |
| + | |
| + | ::Liquid::Template.register_tag('path_to', PathTo) |
| + | end |
| + | end |
| + | end |
| + | end |
| \ No newline at end of file | |
locomotive/wagon/liquid/tags/url_helper.rb b/lib/locomotive/wagon/liquid/tags/url_helper.rb
+0
-98
| @@ | @@ -1,98 +0,0 @@ |
| - | module Locomotive |
| - | module Wagon |
| - | module Liquid |
| - | module Tags |
| - | |
| - | module UrlHelper |
| - | |
| - | def render_url(context, &block) |
| - | site = context.registers[:site] |
| - | |
| - | if page = self.retrieve_page_from_handle(context) |
| - | url = self.public_page_url(context, page) |
| - | |
| - | if block_given? |
| - | block.call page, url |
| - | else |
| - | url |
| - | end |
| - | else |
| - | raise Liquid::PageNotTranslated.new(%{[link_to] Unable to find a page for the #{@handle}. Wrong handle or missing template for your content.}) |
| - | end |
| - | end |
| - | |
| - | protected |
| - | |
| - | def retrieve_page_from_handle(context) |
| - | mounting_point = context.registers[:mounting_point] |
| - | |
| - | context.scopes.reverse_each do |scope| |
| - | handle = scope[@handle] || @handle |
| - | |
| - | page = case handle |
| - | when Locomotive::Mounter::Models::Page then handle |
| - | when Liquid::Drops::Page then handle.instance_variable_get(:@_source) |
| - | when String then fetch_page(mounting_point, handle) |
| - | when Liquid::Drops::ContentEntry then fetch_page(mounting_point, handle.instance_variable_get(:@_source), true) |
| - | when Locomotive::Mounter::Models::ContentEntry then fetch_page(mounting_point, handle, true) |
| - | else |
| - | nil |
| - | end |
| - | |
| - | return page unless page.nil? |
| - | end |
| - | |
| - | nil |
| - | end |
| - | |
| - | def fetch_page(mounting_point, handle, templatized = false) |
| - | ::Locomotive::Mounter.with_locale(@_options['locale']) do |
| - | if templatized |
| - | page = mounting_point.pages.values.find do |_page| |
| - | _page.templatized? && |
| - | !_page.templatized_from_parent && |
| - | _page.content_type.slug == handle.content_type.slug && |
| - | (@_options['with'].nil? || _page.handle == @_options['with']) |
| - | end |
| - | |
| - | page.content_entry = handle if page |
| - | |
| - | page |
| - | else |
| - | mounting_point.pages.values.find { |_page| _page.handle == handle } |
| - | end |
| - | end |
| - | end |
| - | |
| - | def public_page_url(context, page) |
| - | mounting_point = context.registers[:mounting_point] |
| - | locale = @_options['locale'] || ::I18n.locale |
| - | |
| - | if !page.translated_in?(locale) |
| - | title = page.title_translations.values.compact.first |
| - | raise Liquid::PageNotTranslated.new(%{the "#{title}" page is not translated in #{locale.upcase}}) |
| - | end |
| - | |
| - | fullpath = ::Locomotive::Mounter.with_locale(locale) do |
| - | page.fullpath.clone |
| - | end |
| - | |
| - | fullpath = "#{::I18n.locale}/#{fullpath}" if ::I18n.locale.to_s != mounting_point.default_locale.to_s |
| - | |
| - | if page.templatized? |
| - | if page.content_entry._slug.nil? |
| - | title = %{#{page.content_entry.content_type.name.singularize} "#{page.content_entry.send(page.content_entry.content_type.label_field_name)}"} |
| - | raise Liquid::ContentEntryNotTranslated.new(%{the #{title} slug is not translated in #{locale.upcase}}) |
| - | end |
| - | fullpath.gsub!(/(content[_-]type[_-]template|template)/, page.content_entry._slug) |
| - | end |
| - | |
| - | File.join('/', fullpath) |
| - | end |
| - | |
| - | end |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| \ No newline at end of file | |
locomotive/wagon/liquid/tags/url_to.rb b/lib/locomotive/wagon/liquid/tags/url_to.rb
+0
-36
| @@ | @@ -1,36 +0,0 @@ |
| - | module Locomotive |
| - | module Wagon |
| - | module Liquid |
| - | module Tags |
| - | |
| - | class UrlTo < ::Liquid::Tag |
| - | |
| - | include UrlHelper |
| - | |
| - | Syntax = /(#{::Liquid::Expression}+)(#{::Liquid::TagAttributes}?)/ |
| - | |
| - | def initialize(tag_name, markup, tokens, context) |
| - | if markup =~ Syntax |
| - | @handle = $1 |
| - | @_options = {} |
| - | markup.scan(::Liquid::TagAttributes) do |key, value| |
| - | @_options[key] = value |
| - | end |
| - | else |
| - | raise SyntaxError.new("Syntax Error in 'url_to' - Valid syntax: url_to <page|page_handle|content_entry>(, locale: [fr|de|...], with: <page_handle>") |
| - | end |
| - | |
| - | super |
| - | end |
| - | |
| - | def render(context) |
| - | render_url(context) |
| - | end |
| - | |
| - | end |
| - | |
| - | ::Liquid::Template.register_tag('url_to', UrlTo) |
| - | end |
| - | end |
| - | end |
| - | end |
| \ No newline at end of file | |
spec/fixtures/default/app/views/pages/music.liquid.haml
+1
-1
| @@ | @@ -35,7 +35,7 @@ position: 2 |
| {% for s in selected_songs %} | |
| %p.scoped_song {{ s._label }} | |
| %p.scoped_song_link | |
| - | %a{ href: "{% url_to s %}" } {{ s._label }} |
| + | %a{ href: "{% path_to s %}" } {{ s._label }} |
| {% endfor %} | |
| %p.collection_equality {{ contents.songs.all.size }}={{ contents.songs.size }} | |