added capacity for pulling static pages from multiple directory source trees.
[leap_website.git] / lib / static_page_array.rb
1 #
2 # Array of StaticPages
3 #
4 class StaticPageArray < Array
5   def limit(num)
6     StaticPageArray.new(self[0..(num-1)])
7   end
8   def order_by(attr, options={})
9     locale = options[:locale] || I18n.locale
10     direction = options[:direction] || :asc
11     array = sort do |a,b|
12       if direction == :desc
13         a, b = b, a
14       end
15       a_prop = a.props.locale(locale).send(attr)
16       b_prop = b.props.locale(locale).send(attr)
17       if a_prop.nil? && b_prop.nil?
18         0
19       elsif a_prop.nil?
20         1
21       elsif b_prop.nil?
22         -1
23       else
24         a_prop <=> b_prop
25       end
26     end
27     array.delete_if do |page|
28       page.props.locale(locale).send(attr).nil?
29     end
30     return StaticPageArray.new.replace array
31   end
32 end