group_by now works in liquid

did committed Jan 08, 2013
commit 34093f1809f70c24c114369447d37790bc1a89d0
Showing 2 changed files with 25 additions and 7 deletions
TODO +3 -3
@@ @@ -12,9 +12,9 @@ x I18n keys
x fr/en
x other locales
x static js/css assets (non coffeescript or sass files) are not reloaded if got changed.
- - content types / liquid
- - group_contents_by
- - select_names
+ x content types / liquid
+ x group_contents_by
+ x select_names
- nice error page (replace the default exception middleware) to display:
- liquid errors
- other exceptions
locomotive/builder/liquid/drops/content_types.rb b/lib/locomotive/builder/liquid/drops/content_types.rb +22 -4
@@ @@ -46,11 +46,9 @@ module Locomotive
def before_method(meth)
if (meth.to_s =~ /^group_by_(.+)$/) == 0
- # TODO
- @content_type.group_contents_by($1)
+ self.group_entries_by(@content_type, $1)
elsif (meth.to_s =~ /^(.+)_options$/) == 0
- # TODO
- @content_type.select_names($1)
+ self.select_options_for(@content_type, $1)
else
@content_type.send(meth)
end
@@ @@ -58,6 +56,26 @@ module Locomotive
protected
+ def group_entries_by(content_type, name)
+ field = @content_type.find_field(name)
+
+ return {} if field.nil? || !%w(belongs_to select).include?(field.type.to_s)
+
+ (@content_type.entries || []).group_by do |entry|
+ entry.send(name.to_sym)
+ end.to_a.collect do |group|
+ { name: group.first, entries: group.last }.with_indifferent_access
+ end
+ end
+
+ def select_options_for(content_type, name)
+ field = @content_type.find_field(name)
+
+ return {} if field.nil? || field.type.to_s != 'select'
+
+ field.select_options.map(&:name)
+ end
+
def paginate(options = {})
@collection ||= self.collection.paginate(options)
{