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, #translate, #visible_field, #visible_fields, #visible_groups, #with_resource
Constructor Details
#initialize(factory, chain, options = {}) ⇒ Query
Returns a new instance of Query.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/basepack/forms/query.rb', line 12
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.
9
10
11
|
# File 'lib/basepack/forms/query.rb', line 9
def auth_object
@auth_object
end
|
Returns the value of attribute date_format.
8
9
10
|
# File 'lib/basepack/forms/query.rb', line 8
def date_format
@date_format
end
|
#edit_ql ⇒ Object
Returns the value of attribute edit_ql.
5
6
7
|
# File 'lib/basepack/forms/query.rb', line 5
def edit_ql
@edit_ql
end
|
#filterql_options ⇒ Object
Returns the value of attribute filterql_options.
10
11
12
|
# File 'lib/basepack/forms/query.rb', line 10
def filterql_options
@filterql_options
end
|
#params ⇒ Object
parameters required to build similar form
6
7
8
|
# File 'lib/basepack/forms/query.rb', line 6
def params
@params
end
|
#ql ⇒ Object
Returns the value of attribute ql.
7
8
9
|
# File 'lib/basepack/forms/query.rb', line 7
def ql
@ql
end
|
#query ⇒ Object
Returns the value of attribute query.
4
5
6
|
# File 'lib/basepack/forms/query.rb', line 4
def query
@query
end
|
Class Method Details
.localize_value(field, predicate_name, value) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/basepack/forms/query.rb', line 182
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
32
33
34
35
36
37
|
# File 'lib/basepack/forms/query.rb', line 32
def collection
@collection || begin
query_from_params
@collection
end
end
|
39
40
41
|
# File 'lib/basepack/forms/query.rb', line 39
def
collection.offset(nil).limit(nil)
end
|
#conditions_to_ql ⇒ Object
151
152
153
154
155
|
# File 'lib/basepack/forms/query.rb', line 151
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
170
171
172
|
# File 'lib/basepack/forms/query.rb', line 170
def default_partial
'forms/query'
end
|
#enum_options ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/basepack/forms/query.rb', line 157
def enum_options
Hash[filterable_fields.map do |f|
if options = f.enum_options
[ f.name, options ]
end
end.compact]
end
|
#field_nested_name(field) ⇒ Object
66
67
68
|
# File 'lib/basepack/forms/query.rb', line 66
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
70
71
72
73
|
# File 'lib/basepack/forms/query.rb', line 70
def filterable_field(name)
f = field(name)
f.try(:filterable?) ? f : nil
end
|
#filterable_fields ⇒ Object
75
76
77
|
# File 'lib/basepack/forms/query.rb', line 75
def filterable_fields
fields.select {|f| f.filterable? }
end
|
#filtered_fields ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/basepack/forms/query.rb', line 94
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
114
115
116
117
118
119
|
# File 'lib/basepack/forms/query.rb', line 114
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
50
51
52
|
# File 'lib/basepack/forms/query.rb', line 50
def has_errors?
resource_filter.errors[:base].present?
end
|
#initial_data ⇒ Object
121
122
123
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
|
# File 'lib/basepack/forms/query.rb', line 121
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: 'Dotaz',
name: 'ql',
type: 'ql',
value: @ql || conditions_to_ql,
predicate: 'eq',
template: '',
}
end
init
end
|
#nested_filterable_field(name) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/basepack/forms/query.rb', line 54
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
178
179
180
|
# File 'lib/basepack/forms/query.rb', line 178
def path
view.polymorphic_path([:query, association_chain, resource_class].flatten)
end
|
#present? ⇒ Boolean
79
80
81
|
# File 'lib/basepack/forms/query.rb', line 79
def present?
@query or @ql or filtered_fields.present?
end
|
#render_field!(field) ⇒ Object
174
175
176
|
# File 'lib/basepack/forms/query.rb', line 174
def render_field!(field)
end
|
#resource_filter ⇒ Object
43
44
45
46
47
48
|
# File 'lib/basepack/forms/query.rb', line 43
def resource_filter
@resource_filter || begin
query_from_params
@resource_filter
end
end
|
#setup ⇒ Object
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/basepack/forms/query.rb', line 83
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
|