Class: DcFilter

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/dc_filter.rb

Overview

Model and collection for filtering and sorting data.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_field_form_definition(name, parent) ⇒ Object

Will return field form definition if field is defined on form. Field definition will be used for input field on the form.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/dc_filter.rb', line 91

def self.get_field_form_definition(name, parent) #:nodoc:
  form = parent.form
  form['form']['tabs'].each do |tab|
    # Array with 2 elements. First is tabname, second is data
    my_fields = tab.last
    my_fields.each {|k,v| return v if (k.class == Integer and v['name'] == name) }
  end if form['form']['tabs'] #  I know. But nice. 

  form['form']['fields'].each do |field|
    next unless field.first.class == Integer # options
    return field.last if field.last['name'] == name
  end if form['form']['fields']
  nil
end

.get_filter(filter) ⇒ Object

Will return model with filter query set



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
# File 'app/models/dc_filter.rb', line 54

def self.get_filter(filter)
  yaml = YAML.load(filter) rescue nil
  return yaml if yaml.nil?
  return nil if yaml['table'].nil? # old data
#  
  model = yaml['table'].classify.constantize
  field = yaml['field'] == 'id' ? '_id' : yaml['field'] # must be
# evaluate
  if yaml['operation'] == 'eval' and model.respond_to?(yaml['field'])
    return model.send( yaml['field'] )
  end
# if empty
  if yaml['operation'] == 'empty'
    return model.in(field => [nil,''])
  end
# if value == NIL no filter is necessary
  return nil if yaml['value'].class == String and yaml['value'] == '#NIL'
  
# do regex if operation is like  
  value = yaml['operation'] == 'like' ? /#{yaml['value']}/i : yaml['value'] 
# when field type is ObjectId transform value    
  if model.fields[field] and model.fields[field].type == BSON::ObjectId
    value = BSON::ObjectId.from_string(value) rescue nil
  end
#
  if ['eq','like'].include?(yaml['operation'])
    model.where(field => value)
# TODO in operator    
  else
    model.where(field.to_sym.send(yaml['operation']) => value)
  end
end

.get_filter_field(parent) ⇒ Object

Return filter input field for entering variable filter values on index form



109
110
111
112
113
114
115
116
117
118
119
120
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/dc_filter.rb', line 109

def self.get_filter_field(parent)
  return '' if parent.session[ parent.form['table'] ].nil?

  filter = parent.session[ parent.form['table'] ][:filter]
  return '' if filter.nil?

  filter = YAML.load(filter) rescue nil 
  return '' if filter.nil?

  field = get_field_form_definition(filter['field'], parent)
  return '' if field.nil? && filter['input'].nil?

  saved_readonly = parent.form['readonly']
  parent.form['readonly'] = false # must be
  field ||= {}
  # If field has choices available in labels, use them. This is most likely select input field.
  if field['name']
    choices = parent.t('helpers.label.' + parent.form['table'] + '.choices4_' + field['name'] )
    unless choices.match( 'translation missing' ) or choices.match('helpers.label')
      field['choices'] = choices
    end
  end
  # field redefined with input keyword. Name must start with _
  field['name']     = '_filter_field'
  field['type']     = filter['input'] if filter['input'].to_s.size > 5
  field['type']   ||= 'text_field'
  field['readonly'] = false   # must be
  # let text fields size be no more then 20
  field['size'] = 20 if field['type'].match('text') && field['size'].to_i > 20
  field['html']   ||= {}
  # Start with last entered value
  field['html']['value']    = filter['value'] unless filter['value'] == '#NIL'
  field['html']['selected'] = field['html']['value'] # for select field
  # url for filter ON action
  field['html']['data-url'] = parent.url_for(controller: 'cmsedit', action: 'run', control: 'cmsedit.filter_on',
                                             t: CmsHelper.table_param(parent.params), f: CmsHelper.form_param(parent.params))
  url = field['html']['data-url']
  # remove if present
  field['with_new'] = nil if field['with_new']
  # create input field object
  html = ''
  klass_string = field['type'].camelize
  klass = DrgcmsFormFields::const_get(klass_string) rescue nil
  # return data from object and create html code to display field
  if klass
    if drg_field = klass.new(parent, nil, field).render rescue nil
      js = drg_field.js.blank? ? '' : parent.javascript_tag(drg_field.js)
      html = %(<li class="no-background">
<span class="filter_field" data-url="#{url}">#{drg_field.html}
  #{parent.fa_icon('search', class: 'record_filter_field_icon')}
  #{js}</span></li>)
    else
      # Error. Forget filter
      parent.session[ parent.form['table'] ][:filter] = nil
    end
  end
  parent.form['readonly'] = saved_readonly
  html.html_safe
end

Create popup menu for filter options.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/dc_filter.rb', line 172

def self.menu_filter(parent)
  html = '<ul class="menu-filter">' 
  table = parent.form['table']
  documents = self.where(table: table, active: true).to_a
  documents.each do |document|
    description = document.description.match('.') ? I18n.t(document.description) : document.description
    html << "<li data-filter=\"#{document.id}\">#{description}</li>"
  end

  # add filters defined in model
  model = table.classify.constantize
  filters = model.dc_filters if model.respond_to?(:dc_filters)
  if filters
    # only single defined. Convert to array.
    filters = [filters] if filters.class == Hash
    filters.each do |filter|
      url = parent.dc_link_to(filter['title'], nil, controller: 'cmsedit', action: :run, t: table,
                              f: CmsHelper.form_param(parent.params),
                              control: 'cmsedit.filter_on',
                              filter_field: filter['field'],
                              filter_oper: filter['operation'],
                              filter_value: filter['value'])

      url = parent.url_for(controller: 'cmsedit', action: :run, t: table, f: CmsHelper.form_param(parent.params),
                           control: 'cmsedit.filter_on',
                           filter_field: filter['field'],
                           filter_oper: filter['operation'],
                           filter_value: filter['value'])
      html << %(<li class="dc-link-ajax in-menu" data-url="#{url}">#{filter['title']}</li>)
    end
  end
# divide standard and custom filter options  
  html << '<hr>' if html.size > 30 # 
  html << %(<li class="dc-link in-menu" id="open_drgcms_filter">#{I18n.t('drgcms.filter_set')}</li></ul>)
  html.html_safe
end

.title4_filter_off(filter_data) ⇒ Object

Creates title for turn filter off, which consists of displaying curently active filter and text to turn it off.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/models/dc_filter.rb', line 213

def self.title4_filter_off(filter_data)
  return '' unless filter_data&.dig(:filter)

  filter = YAML.load(filter_data[:filter])
  operations = I18n.t('drgcms.choices4_filter_operators').chomp.split(',').inject([]) {|r,v| r << v.split(':') }
  operation = ''
  if filter['operation'] == 'eval'
   filter['field']
  else
    operations.each{|a| (operation = a.first; break) if a.last == filter['operation']}

    '[ ' + I18n.t("helpers.label.#{filter['table']}.#{filter['field']}") + 
    " ] #{operation} [ #{filter['value'].to_s} ] : #{I18n.t('drgcms.filter_off')}"
  end
end

Instance Method Details

#do_before_saveObject

Implementation of before_save callback.



47
48
49
# File 'app/models/dc_filter.rb', line 47

def do_before_save
  self.dc_user_id = nil if self.public
end