-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathparameters.rb
105 lines (90 loc) · 2.96 KB
/
parameters.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module Api
class BaseController
module Parameters
module ResultsController
def sort_order
params['sort_order'] == 'desc' ? :descending : :ascending
end
def param_result_set?
params.key?(:hash_attribute) && params[:hash_attribute] == "result_set"
end
def report_options
params.merge(:sort_order => sort_order)
end
end
def hash_fetch(hash, element, default = {})
hash[element] || default
end
#
# Returns an MiqExpression based on the filter attributes specified.
#
def filter_param(klass)
return nil if params['filter'].blank?
Filter.parse(params["filter"], klass)
end
def by_tag_param
params['by_tag']
end
def search_options
params['search_options'].to_s.split(",")
end
def search_option?(what)
search_options.map(&:downcase).include?(what.to_s)
end
def format_attributes
params['format_attributes'].to_s.split(",").map { |af| af.split("=").map(&:strip) }
end
def attribute_format(attr)
format_attributes.detect { |af| af.first == attr }.try(:second)
end
def attribute_selection
if @req.attributes.empty? && @additional_attributes
Array(@additional_attributes) | ID_ATTRS
elsif [email protected]?
@req.attributes | ID_ATTRS
else
"all"
end
end
def attribute_selection_for(collection)
Array(attribute_selection).collect do |attr|
/\A#{collection}\.(?<name>.*)\z/.match(attr) { |m| m[:name] }
end.compact
end
def render_attr(attr)
as = attribute_selection
as == "all" || as.include?(attr)
end
#
# Returns the ActiveRecord's option for :order
#
# i.e. ['attr1 [asc|desc]', 'attr2 [asc|desc]', ...]
#
def sort_params(klass)
return [] if params['sort_by'].blank?
orders = String(params['sort_order']).split(",")
options = String(params['sort_options']).split(",")
params['sort_by'].split(",").zip(orders).collect do |attr, order|
if klass.virtual_attribute?(attr) && !klass.attribute_supported_by_sql?(attr)
raise BadRequestError, "#{klass.name} cannot be sorted by #{attr}"
elsif klass.attribute_supported_by_sql?(attr)
sort_directive(klass, attr, order, options)
else
raise BadRequestError, "#{attr} is not a valid attribute for #{klass.name}"
end
end.compact
end
def sort_directive(klass, attr, order, options)
arel = klass.arel_attribute(attr)
if order
arel = arel.lower if options.map(&:downcase).include?("ignore_case")
arel = arel.desc if order.downcase == "desc"
arel = arel.asc if order.downcase == "asc"
else
arel = arel.asc
end
arel
end
end
end
end