Skip to content

Commit

Permalink
Add sorting for liquid for statements.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2531 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Dec 7, 2006
1 parent b312360 commit ce99258
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG
Expand Up @@ -9,6 +9,11 @@
end

* fix that only published articles are available as section pages.
* SVN *

Add sorting for liquid for statements.

{% for page in section.pages limit: 3 sort_by: title %}

* 0.7.3 *

Expand Down
22 changes: 22 additions & 0 deletions lib/mephisto/liquid/for_with_sorting.rb
@@ -0,0 +1,22 @@
module Mephisto
module Liquid
module ForWithSorting
def self.included(base)
base.alias_method_chain :render, :sorting
end

def render_with_sorting(context)
context.registers[:for] ||= Hash.new(0)

collection = context[@collection_name]
collection = collection.to_a if collection.is_a?(Range)

return '' if collection.nil? or collection.empty?

context[@collection_name] = collection.sort_by { |i| i[@attributes['sort_by']] } if @attributes['sort_by']

render_without_sorting(context)
end
end
end
end
2 changes: 1 addition & 1 deletion test/functional/feed_controller_test.rb
Expand Up @@ -103,7 +103,7 @@ def setup
specify "should sanitize content" do
text = @contents.first.get_text.to_s.strip
evil = "<script>hi</script><a onclick=\"foo\" href=\"#\">linkage</a></p>"
good = "&lt;script>hi&lt;/script><a href='#'>linkage</a></p>"
good = "<a href='#'>linkage</a></p>"
assert !text.ends_with(CGI::escapeHTML(evil)), "'#{text.inspect}' was not sanitized"
assert text.ends_with(CGI::escapeHTML(good)), "'#{text.inspect}' was not sanitized"
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Expand Up @@ -91,6 +91,10 @@ class Test::Unit::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false

def assert_template_result(expected, template, assigns={}, message=nil)
assert_equal expected, Liquid::Template.parse(template).render(assigns)
end

def host!(hostname)
@request.host = hostname
end
Expand Down
1 change: 1 addition & 0 deletions vendor/plugins/mephisto/init.rb
Expand Up @@ -122,6 +122,7 @@ def self.to_fixture(fixture_path = nil)
expiring_attr_reader :referenced_cache_key, '"[#{[id, self.class.name] * ":"}]"'
end

Liquid::For.send :include, Mephisto::Liquid::ForWithSorting

require 'mephisto_core/admin'
require 'mephisto_core/plugin'

0 comments on commit ce99258

Please sign in to comment.