Class: Basepack::Forms::Query
- Inherits:
-
Base
- Object
- Base
- Basepack::Forms::Query
show all
- Defined in:
- lib/basepack/forms/query.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#association_chain, #factory, #groups, #nested_in, #partial, #resource, #resource_class, #view
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#build_from_factory, #chain, #chain_with_class, #configure, #content_for_field, #default_group, #field, #field_names, #fields, #fields_hash, #group, #has_field?, #hide_field, #hide_fields, #inspect, #inverse_of_nested_in?, #label, #label_plural, #new_form, #new_record?, #permit_params, #render_field, #sanitize_params, #show_fields, #show_only_fields, #translate, #visible_field, #visible_fields, #visible_groups, #with_resource
Constructor Details
#initialize(factory, chain, options = {}) ⇒ Query
Returns a new instance of Query.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/basepack/forms/query.rb', line 15
def initialize(factory, chain, options = {})
super(factory, chain, options)
@scope = options[:scope]
@auth_object = options[:auth_object]
@filterql_options = options[:filterql_options]
@collection_includes = options[:collection_includes]
if params = options[:params]
@params = params.slice(:query, :ql, :f, :page, :per).reject {|k, v| v.empty?}
@query = params[:query]
if has_errors?
@ql = params[:ql]
elsif params[:edit_ql]
@edit_ql = params[:edit_ql]
end
end
end
|
Instance Attribute Details
#auth_object ⇒ Object
Returns the value of attribute auth_object.
12
13
14
|
# File 'lib/basepack/forms/query.rb', line 12
def auth_object
@auth_object
end
|
Returns the value of attribute date_format.
11
12
13
|
# File 'lib/basepack/forms/query.rb', line 11
def date_format
@date_format
end
|
#edit_ql ⇒ Object
Returns the value of attribute edit_ql.
8
9
10
|
# File 'lib/basepack/forms/query.rb', line 8
def edit_ql
@edit_ql
end
|
#filterql_options ⇒ Object
Returns the value of attribute filterql_options.
13
14
15
|
# File 'lib/basepack/forms/query.rb', line 13
def filterql_options
@filterql_options
end
|
#params ⇒ Object
parameters required to build similar form
9
10
11
|
# File 'lib/basepack/forms/query.rb', line 9
def params
@params
end
|
#ql ⇒ Object
Returns the value of attribute ql.
10
11
12
|
# File 'lib/basepack/forms/query.rb', line 10
def ql
@ql
end
|
#query ⇒ Object
Returns the value of attribute query.
7
8
9
|
# File 'lib/basepack/forms/query.rb', line 7
def query
@query
end
|
Class Method Details
.localize_value(field, predicate_name, value) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/basepack/forms/query.rb', line 191
def self.localize_value(field, predicate_name, value)
return value if value.is_a? Array
return "" if FilterQL.predicates[predicate_name][:type] == :boolean
case field.type
when :date, :datetime
I18n.l(value.to_date)
when :time
I18n.l(value.to_time, :format => "%H:%M")
else
value
end
end
|
Instance Method Details
#collection ⇒ Object
35
36
37
38
39
40
|
# File 'lib/basepack/forms/query.rb', line 35
def collection
@collection || begin
query_from_params
@collection
end
end
|
42
43
44
|
# File 'lib/basepack/forms/query.rb', line 42
def
collection.offset(nil).limit(nil)
end
|
#conditions_to_ql ⇒ Object
154
155
156
157
158
|
# File 'lib/basepack/forms/query.rb', line 154
def conditions_to_ql
FilterQL.conditions_to_ql(filtered_fields.map do |field, predicate_name, value|
[field_nested_name(field), predicate_name, value]
end)
end
|
#default_partial ⇒ Object
179
180
181
|
# File 'lib/basepack/forms/query.rb', line 179
def default_partial
'forms/query'
end
|
#enum_options(fields = fields) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/basepack/forms/query.rb', line 160
def enum_options(fields = fields)
res = {}
fields.each do |f|
options = f.enum_options
if options and f.filterable?
res[field_nested_name(f)] = options
elsif (f.association? and !nested_in and !f.polymorphic?)
res.update(f.nform.enum_options)
end
end
res
end
|
#field_nested_name(field) ⇒ Object
69
70
71
|
# File 'lib/basepack/forms/query.rb', line 69
def field_nested_name(field)
field.form.nested_in ? "#{field_nested_name(field.form.nested_in)}_#{field.name}" : field.name
end
|
#filterable_field(name) ⇒ Object
73
74
75
76
|
# File 'lib/basepack/forms/query.rb', line 73
def filterable_field(name)
f = field(name)
f.try(:filterable?) ? f : nil
end
|
#filterable_fields ⇒ Object
78
79
80
|
# File 'lib/basepack/forms/query.rb', line 78
def filterable_fields
fields.select {|f| f.filterable? }
end
|
#filtered_fields ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/basepack/forms/query.rb', line 97
def filtered_fields
@filtered_fields || begin
if nested_in
@filtered_fields = []
else
@filtered_fields = resource_filter.c.map do |condition|
if condition.valid? and condition.attributes.size == 1
if field = nested_filterable_field(condition.attributes.first.name.to_sym)
[field, condition.predicate_name, Query.localize_value(field, condition.predicate_name, condition.value)]
end
end
end.concat(Array.wrap(resource_filter.custom_filters).map do |name, predicate_name, values|
if field = nested_filterable_field(name.to_sym)
[field, predicate_name, values]
end
end).compact
end
end
end
|
#filtered_fields_find(nested_field_name, condition = nil) ⇒ Object
117
118
119
120
121
122
|
# File 'lib/basepack/forms/query.rb', line 117
def filtered_fields_find(nested_field_name, condition = nil)
filtered_fields.find do |f|
(field_nested_name(f[0]) == nested_field_name) and
(condition ? (f[1] == condition) : true)
end
end
|
#has_errors? ⇒ Boolean
53
54
55
|
# File 'lib/basepack/forms/query.rb', line 53
def has_errors?
resource_filter.errors[:base].present?
end
|
#initial_data ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/basepack/forms/query.rb', line 124
def initial_data
if @edit_ql
init = []
else
init = filtered_fields.map do |field, predicate_name, values|
{
label: field.nested_label,
name: field_nested_name(field),
type: field.type,
value: values,
predicate: predicate_name,
template: field.render.to_s,
}
end
end
if @ql or @edit_ql
init << {
label: I18n.t('basepack.query.query'),
name: 'ql',
type: 'ql',
value: @ql || conditions_to_ql,
predicate: 'eq',
template: '',
}
end
init
end
|
#nested_filterable_field(name) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/basepack/forms/query.rb', line 57
def nested_filterable_field(name)
filterable_field(name) || begin
n = name.to_s.split('_')
(n.size - 1).downto(1) do |count|
if f = filterable_field(n[0, count].join('_').to_sym)
return f.nform.nested_filterable_field(n[count..-1].join('_').to_sym)
end
end
nil
end
end
|
#path ⇒ Object
187
188
189
|
# File 'lib/basepack/forms/query.rb', line 187
def path
view.polymorphic_path([:query, association_chain, resource_class].flatten)
end
|
#present? ⇒ Boolean
82
83
84
|
# File 'lib/basepack/forms/query.rb', line 82
def present?
@query or @ql or filtered_fields.present?
end
|
#render_field!(field) ⇒ Object
183
184
185
|
# File 'lib/basepack/forms/query.rb', line 183
def render_field!(field)
end
|
#resource_filter ⇒ Object
46
47
48
49
50
51
|
# File 'lib/basepack/forms/query.rb', line 46
def resource_filter
@resource_filter || begin
query_from_params
@resource_filter
end
end
|
#setup ⇒ Object
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/basepack/forms/query.rb', line 86
def setup
{
options: {
regional: { datePicker: { dateFormat: date_format }.reverse_merge!(Basepack::Forms::Edit.data_picker_options) },
predicates: FilterQL.predicates,
enum_options: enum_options,
},
initial: initial_data,
}
end
|