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
# 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? and filter['input'].nil?
  field = {} if field.nil?
# 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.form['form_name'])
  url = field['html']['data-url']
# create input field object
  klas_string = field['type'].camelize
  klas = DrgcmsFormFields::const_get(klas_string) rescue nil
  return '' if klas.nil?
# return data from object and create html code to display field
  object = klas.new(parent, nil, field).render
  js     = object.js
  "<span class=\"filter_field\" data-url=\"#{url}\">#{object.html} " <<
    parent.fa_icon('filter lg', class: 'record_filter_field_icon dc-link-icon dc-animate') <<
    (js.size > 2 ? parent.javascript_tag(js) : '') << '</span>'
end

Create popup menu for filter options.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/dc_filter.rb', line 157

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.



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/dc_filter.rb', line 192

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