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'] = nil # 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
  field['html']   ||= {}
  field['html']['size'] = 20
  # 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: :index, filter: 'on',
    table: parent.form['table'], form_name: parent.params['form_name'])
  url = field['html']['data-url']
  # remove if present
  field['with_new'] = nil if field['with_new']
  # create input field object
  klas_string = field['type'].camelize
  klas = DrgcmsFormFields::const_get(klas_string) rescue nil
  parent.form['readonly'] = saved_readonly
  return '' if klas.nil?

  # return data from object and create html code to display field
  object = klas.new(parent, nil, field).render rescue nil
  # Error. Forget filter and return 
  if object.nil?
    parent.session[ parent.form['table'] ][:filter] = nil
    return ''
  end
  js = object.js.blank? ? '' : parent.javascript_tag(object.js)
%(<li class="no-background">
<span class="filter_field" data-url="#{url}">#{object.html}
#{parent.fa_icon('search lg', class: 'record_filter_field_icon')}
#{js}</span>
</li>)
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
# 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 rescue nil
  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: :index, table: table,
                           form_name: parent.params[:form_name], 
                           filter_field: filter['field'],
                           filter_oper: filter['operation'],
                           filter_value: filter['value'],
                           filter: 'on')
      html << "<li>#{url}</li>"
    end
  end
# divide standard and custom filter options  
  html << '<hr>' if html.size > 30 # 
  html << '<li id="open_drgcms_filter">' + I18n.t('drgcms.filter_set') + '</li>'
  html << '</ul>'
end

.title4_filter_off(filter_yaml) ⇒ Object

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



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/models/dc_filter.rb', line 207

def self.title4_filter_off(filter_yaml)
  filter = YAML.load(filter_yaml)
  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