Module: DigitalAssetsHelper

Included in:
DigitalAssetsController
Defined in:
app/helpers/digital_assets_helper.rb

Instance Method Summary collapse

Instance Method Details

#search_da(params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/digital_assets_helper.rb', line 3

def search_da(params)
  digital_assets = []
  # loop thru each parameter that matches one of the method signatures
  # x_is for singular, x_in for arrays, x_since for dates
  digital_assets = params.keys.select do |pk|
    DigitalAsset.respond_to?("#{pk}_is".to_sym) or DigitalAsset.respond_to?("#{pk}_in".to_sym) or DigitalAsset.respond_to?("#{pk}_since".to_sym) 
  end.reduce(DigitalAsset) do |sum, key|
    # for each key, call the 'named query' method with the value given and chain...
    method = case 
      when DigitalAsset.respond_to?("#{key}_in".to_sym)
        "#{key}_in".to_sym 
      when DigitalAsset.respond_to?("#{key}_since".to_sym)
        "#{key}_since".to_sym  
      else
        "#{key}_is".to_sym
      end
    # method = DigitalAsset.respond_to?("#{key}_in".to_sym) ? "#{key}_in".to_sym : "#{key}_is".to_sym
    sum.send(method, method.to_s.end_with?('in') ? params[key].to_a : params[key]) # should return result of the send call for chaining
  end
  digital_assets.respond_to?(:each) ?  digital_assets : []
end