Module: EstoreConventions::ClassMethods

Defined in:
lib/estore_conventions.rb

Instance Method Summary collapse

Instance Method Details

#add_sorted_value_and_sort(foo, opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/estore_conventions.rb', line 18

def add_sorted_value_and_sort(foo, opts={})

  if foo.class == Proc 
    self.all.sort_by do |channel| 
      val = foo.call(channel) 
      channel.instance_eval "def sorted_value; #{val}; end"

      -val            
    end
  elsif foo.class == String || foo.class == Symbol
    order_val = opts[:order] || 'DESC'
    self.order("#{foo} #{order_val}").
          select("#{self.table_name}.*, #{self.table_name}.#{foo} AS sorted_value").
          limit(opts[:limit])
  else  
    raise ArgumentError, "#{foo} needs to be a String, Symbol, or Proc"        
  end
end