Module: Anoubis::Data::Get

Included in:
Anoubis::DataController
Defined in:
app/controllers/anoubis/data/get.rb

Overview

Module gets system data for Anoubis::DataController

Block of table data getters collapse

Block of {Anoubis::Data::Actions#edit edit} and {Anoubis::Data::Actions#update update} getters collapse

Block of {Anoubis::Data::Actions#new new} or {Anoubis::Data::Actions#create create} getters collapse

Instance Method Summary collapse

Instance Method Details

#get_autocomplete_data(field, value) ⇒ Hash

Get autocomplete data for field

Parameters:

  • field (Anoubis::Etc::Field)
    • field for loading data

  • value (String)
    • search value for load data

Returns:

  • (Hash)

    resulting hash for selected data



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'app/controllers/anoubis/data/get.rb', line 474

def get_autocomplete_data(field, value)
  value = value.to_s
  if value.index(' ')
    words = value.split(' ')

    max_count = 0;
    words.each do |word|
      max_count = word.length if word.length > max_count
      if field.autocomplete[:where].count == 0
        field.autocomplete[:where].push field.table_field+' LIKE ?'
      else
        field.autocomplete[:where][0] += ' AND '+field.table_field+' LIKE ?'
      end
      field.autocomplete[:where].push("%#{word}%")
    end
    if max_count < field.autocomplete[:count]
      field.autocomplete[:where] = []
    end
  else
    if value.length >= field.autocomplete[:count]
      field.autocomplete[:where] = [field.table_field+' LIKE ?', '%'+value+'%']
    end
  end
  if field.autocomplete[:where].count > 0
    self.load_autocomplete_data field
  end
  if self.etc.data.data
    self.etc.data.data.each do |item|
      if item.respond_to? field.model.title
        self.output.values.push(
          #id: item.id,
          value: item.send(field.model.title)
        )
      else
        self.output.values.push(
          #id: item.id,
          value: item.id
        )
      end
    end
  end
  return self.output.values
end

#get_data_options(time) ⇒ Hash

Get defined fields options

Parameters:

  • time (Number)

    last execution time of action

Returns:

  • (Hash)

    hash of fields options



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'app/controllers/anoubis/data/get.rb', line 368

def get_data_options(time)
  time = time.to_s.to_i
  self.setup_fields
  result = {}
  self.etc.data.fields.each do |key, field|
    if field.options
      if field.options.list
        if time == 0
          result[key] = field.options.list if field.options.show != 'never'
        else
          if field.model
            if field.options.show == 'always'
              result[key] = field.options.list
            else
              if field.options.show == 'update' && field.model.updated_at > time
                result[key] = field.options.list
              end
            end
          else
            result[key] = field.options.list if field.options.show == 'update' || field.options.show == 'always'
          end
        end
      end
    end
  end
  result
end

#get_data_row(row) ⇒ Hash

Get data fields defined in custom controller for single row

Parameters:

  • row (ActiveRecord)

    single row of model data

Returns:

  • (Hash)

    calculated hash of model row



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/anoubis/data/get.rb', line 211

def get_data_row(row)
  fields = self.get_fields

  new_row = {}

  case self.etc.action
  when 'show'
    new_row = { id: row.id, sys_title: row.sys_title }
  when 'index', 'export'
    new_row = { id: row.id, sys_title: row.sys_title, actions: self.get_table_actions(row) }
  when 'new', 'create'
    new_row = {}
  when 'edit', 'update'
    new_row = { id: row.id, sys_title: row.sys_title }
  end

  fields.each_key do |key|
    begin
      value = eval 'row.' + fields[key].field
      error = false
    rescue
      new_row[key] = fields[key].error_text
      error = true
      if fields[key].type == 'key'
        error = false
      end
    end

    new_row = new_row.merge(self.convert_db_to_view_value(key, value)) if !error
  end

  return new_row
end

#get_eager_loadObject

Get default eager load definition for controller action



95
96
97
98
99
# File 'app/controllers/anoubis/data/get.rb', line 95

def get_eager_load
  return self.etc.data.eager_load if self.etc.data.model
  self.get_model
  return self.etc.data.eager_load
end

#get_edit_data_row(row) ⇒ Hash

Get table data for single row for edit or update actions.

Parameters:

  • row (ActiveRecord)

    single row of model data

Returns:

  • (Hash)

    calculated hash of model row



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/controllers/anoubis/data/get.rb', line 311

def get_edit_data_row(row)
  self.setup_edit_fields
  new_row = { id: row.id, sys_title: row.sys_title }
  self.etc.data.fields.each do |key, field|
    if row.respond_to? field.field
      value = row.send field.field
      error = false
    else
      new_row[key] = field.error_text
      error = true
    end
    new_row = new_row.merge(self.convert_db_to_edit_value(key, value)) if !error
  end
  return new_row
end

#get_edit_eager_loadObject

Get default eager load definition for edit or update actions.



291
292
293
294
295
# File 'app/controllers/anoubis/data/get.rb', line 291

def get_edit_eager_load
  return self.etc.data.eager_load if self.etc.data.model
  self.get_edit_model
  return self.etc.data.eager_load
end

#get_edit_fieldsHash

Return current table fields hash for edit or update actions.

Returns:

  • (Hash)

    current defined table fields



301
302
303
304
# File 'app/controllers/anoubis/data/get.rb', line 301

def get_edit_fields
  self.setup_edit_fields
  self.etc.data.fields
end

#get_edit_modelObject

Get model that is used for edit or update actions.



281
282
283
284
285
286
# File 'app/controllers/anoubis/data/get.rb', line 281

def get_edit_model
  return self.etc.data.model if self.etc.data.model
  self.etc.data.model = self.edit_model
  self.etc.data.eager_load = self.edit_eager_load
  return self.etc.data.model
end

#get_fieldsHash

Return current fields hash for defined action.

Returns:

  • (Hash)

    current defined table fields



430
431
432
433
# File 'app/controllers/anoubis/data/get.rb', line 430

def get_fields
  self.setup_fields
  self.etc.data.fields
end

#get_fields_properties(fields = nil) ⇒ Hash

Returns fields for table output.

Returns:

  • (Hash)

    calculated hash for fields properties for current action



173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/anoubis/data/get.rb', line 173

def get_fields_properties(fields = nil)
  fields = fields_order if !fields
  result = []
  fields.each do |value|
    if self.etc.data.fields
      if self.etc.data.fields.key? value.to_sym
        result.push self.etc.data.fields[value.to_sym].properties(self.etc.data.model, self.etc.action) if self.etc.data.fields[value.to_sym].visible
      end
    end
  end
  result
end

#get_filter_propertiesHash

Returns fields for filter form.

Returns:

  • (Hash)

    calculated hash for fields properties for current action



189
190
191
192
# File 'app/controllers/anoubis/data/get.rb', line 189

def get_filter_properties
  fields = filter_order
  self.get_fields_properties fields
end

#get_filter_whereObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/anoubis/data/get.rb', line 119

def get_filter_where
  #puts 'get_filter_where!'
  #puts self.etc.data.filter.to_h
  return if self.etc.data.filter
  filter = {}
  if params.key? :filter
    begin
      filter = JSON.parse(params[:filter]).with_indifferent_access.to_h
    rescue
      filter = {}
    end
  end
  self.setup_fields
  #puts 'get_filter_where'
  #puts self.etc.data.fields
  self.etc.data.filter = Anoubis::Etc::Filter.new({ data: filter, fields: self.etc.data.fields })

  #puts self.etc.data.filter.to_h
end

#get_filter_where_arrayObject



144
145
146
147
# File 'app/controllers/anoubis/data/get.rb', line 144

def get_filter_where_array
  self.get_filter_where
  return self.etc.data.filter.array
end

#get_filter_where_hashObject



139
140
141
142
# File 'app/controllers/anoubis/data/get.rb', line 139

def get_filter_where_hash
  self.get_filter_where
  return self.etc.data.filter.hash
end

#get_formatted_string_field(options) ⇒ Hash

Returns formatted field hash for field type ‘string’

Parameters:

  • options (Hash)

    initial filed options

Options Hash (options):

  • :min (String)

    defines minimum string length (default: 0)

  • :max (String)

    defines maximum string length (default: 0)

Returns:

  • (Hash)

    resulting hash for field type ‘string’



464
465
466
467
# File 'app/controllers/anoubis/data/get.rb', line 464

def get_formatted_string_field(options)
  field = {}
  return field
end

#get_frame_button(key, button) ⇒ Hash

Get frame button

Parameters:

  • key (String)

    button identificator

  • button (Hash)

    initial button options

Options Hash (button):

  • :type (String) — default: 'default'

    type of the button (‘primary’, ‘danger’, ‘default’)

  • :mode (String) — default: 'single'

    button action object (‘single’, ‘multiple’)

  • :title (String)

    title of the frame. If title isn’t defined then system is trying to take value from yml translation file at path [<language>.<controller with dot>.frame.buttons.<key>.title] (eg. en.anubis.tenants.frame.buttons.new.title for english language ‘anubis/tenants’ controller ‘new’ button). If path absents then value isn’t set.

  • :hint (String)

    hint of the frame. If hint isn’t defined then system is trying to take value from yml translation file at path [<language>.<controller with dot>.frame.buttons.<key>.hint] (eg. en.anubis.tenants.frame.buttons.new.hint for english language ‘anubis/tenants’ controller ‘new’ button). If path absents then value isn’t set.

Returns:

  • (Hash)

    resulting button options



34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/anoubis/data/get.rb', line 34

def get_frame_button(key, button)
  button[:key] = key.to_s
  button[:type] = 'default' unless button.has_key? :type
  button[:mode] = 'single' unless button.has_key? :mode
  button[:decoration] = 'none' unless button.has_key? :decoration
  text = I18n.t('.buttons.'+button[:key]+'.title', default: '')
  button[:title] = text if text != ''
  text = I18n.t('.buttons.'+button[:key]+'.hint', default: '')
  button[:hint] = text if text != ''
  button
end

#get_frame_buttons(args = {}) ⇒ Hash

Get frame buttons data based on passed arguments.

Parameters:

  • args (Hash) (defaults to: {})

    additional parameters are used for define frame buttons.

Options Hash (args):

  • :tab (String)

    tab is used for generation buttons

Returns:

  • (Hash)

    returns resulting button hash



11
12
13
14
15
16
17
# File 'app/controllers/anoubis/data/get.rb', line 11

def get_frame_buttons(args = {})
  buttons = self.frame_buttons(args)
  buttons.each do |key, button|
    buttons[key] = self.get_frame_button key, button
  end
  buttons
end

#get_modelObject

Get model that is used for controller action



84
85
86
87
88
89
90
91
# File 'app/controllers/anoubis/data/get.rb', line 84

def get_model
  return self.etc.data.model if self.etc.data.model
  if defined? self.model
    self.etc.data.model = self.model
    self.etc.data.eager_load = self.eager_load
  end
  return self.etc.data.model
end

#get_new_data_row1(row) ⇒ Hash

Get table data for single row for new or create actions.

Parameters:

  • row (ActiveRecord)

    single row of model data

Returns:

  • (Hash)

    calculated hash of model row



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/controllers/anoubis/data/get.rb', line 346

def get_new_data_row1(row)
  self.setup_new_fields
  new_row = {}
  self.etc.data.fields.each do |key, field|
    if row.respond_to? field.field
      value = row.send field.field
      error = false
    else
      new_row[key] = field.error_text
      error = true
    end
    new_row = new_row.merge(self.convert_db_to_new_value(key, value)) if !error
  end
  return new_row
end

#get_new_fieldsHash

Return current table fields hash for new or create actions

Returns:

  • (Hash)

    current defined table fields



336
337
338
339
# File 'app/controllers/anoubis/data/get.rb', line 336

def get_new_fields
  self.setup_new_fields
  self.etc.data.fields
end

#get_orderHash, String

Returns order for current tab

Returns:

  • (Hash, String)

    order fore current tab



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'app/controllers/anoubis/data/get.rb', line 399

def get_order
  return {id: :desc} if self.etc.tab.sort == nil

  result = {}

  field = self.etc.data.fields[self.etc.tab.sort.to_sym].order
  if field.field.class == Symbol
    result[field.field] = self.etc.tab.order
  else
    if field.field.class == String
      if field.field.index(',')
        result = field.field.gsub(',', ' ' + self.etc.tab.order.to_s.upcase + ',') + ' ' + self.etc.tab.order.to_s.upcase
      else
        result = field.field + ' ' + self.etc.tab.order.to_s.upcase
      end
    else
      if field.field.class == Array
        field.field.each do |item|
          if item.class == Symbol
            result[item] = self.etc.tab.order
          end
        end
      end
    end
  end
  result
end

#get_parent_dataActiveRecord

Returns current parent data. If data not loaded then load it.

Returns:

  • (ActiveRecord)

    resulting parent data



521
522
523
524
525
526
# File 'app/controllers/anoubis/data/get.rb', line 521

def get_parent_data
  if !self.etc.data.parent
    self.load_parent_data
  end
  self.etc.data.parent
end

#get_permited_paramsHash<Symbol, string>

Returns permitted parameters. Parameters is got from standard parameters output and checks according by described self.etc.data.fields.

Returns:

  • (Hash<Symbol, string>)

    permitted paramters’ collection



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'app/controllers/anoubis/data/get.rb', line 439

def get_permited_params
  permit = []
  allowed = self.fields_order
  self.etc.data.fields.each_key do | key |
    single = true
    if self.etc.data.fields[key].type == 'listbox'
      single = false if self.etc.data.fields[key].format == 'multiple'
    end
    if single
      permit.push key
    else
      data = {}
      data[key] = []
      permit.push data
    end
  end
  params[:data].permit(permit).to_h.symbolize_keys
end

#get_tab(tab, options = {}) ⇒ Object

Get tab parameters

Parameters:

  • tab (String)

    identifier

  • options (Hash) (defaults to: {})

    initial tab options

Options Hash (options):

  • :title (String)

    title of the frame. If title isn’t defined then value is taken from yml translation file at path [<language>.<controller with dot>.frame.tabs.<tab>.title] (eg. en.anubis.tenants.frame.tabs.all.title for english language ‘anubis/tenants’ controller ‘all’ tab). If path absents then value is set into humanized form of tab identifier (eg. ‘All’ for ‘all’ tab).

  • :hint (String)

    hint of the frame. If hint isn’t defined then system is trying to take value from yml translation file at path [<language>.<controller with dot>.frame.tabs.<tab>.hint] (eg. en.anubis.tenants.frame.tabs.all.hint for english language ‘anubis/tenants’ controller ‘all’ tab). If path absents then value isn’t set.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/anoubis/data/get.rb', line 58

def get_tab(tab, options = {})
  options[:tab] = tab.to_s
  options[:title] = I18n.t('.tabs.'+options[:tab]+'.title', default: options[:tab].humanize) if !options.key? :title
  if !options.has_key? :hint
    hint = I18n.t('.tabs.'+options[:tab]+'.hint', default: '')
    options[:hint] = hint if hint != ''
  end

  if options.key? :export
    options[:export] = true if options[:export].class != FalseClass
  else
    options[:export] = self.is_export({ tab: tab.to_s })
  end

  if options.key? :filter
    options[:filter] = true if options[:filter].class != FalseClass
  else
    options[:filter] = self.is_filter({ tab: tab.to_s })
  end
  options[:buttons] = self.get_frame_buttons({ tab: options[:tab] })
  #options[:where] = self.where if !options.key? :where
  options
end

#get_tab_whereObject

Get where for controller action fro defined tab



103
104
105
106
# File 'app/controllers/anoubis/data/get.rb', line 103

def get_tab_where
  return self.etc.tab.where if self.etc.tab.where
  []
end

#get_table_action(action, row) ⇒ Boolean

Returns posibility of using action for table data

Parameters:

  • action (String)

    desired action

  • row (ActiveRecord)

    single active record row

Returns:

  • (Boolean)

    is action present or not



265
266
267
268
269
270
271
272
273
# File 'app/controllers/anoubis/data/get.rb', line 265

def get_table_action(action, row)
  result = false
  if self.respond_to?(('table_action_'+action).to_sym)
    result = send 'table_action_'+action, row
  else
    result = true
  end
  result
end

#get_table_actions(row) ⇒ Hash

Returns current table actions for selected row

Parameters:

  • row (ActiveRecord)

    single row of model data

Returns:

  • (Hash)

    resulting has of buttons



249
250
251
252
253
254
255
256
257
258
# File 'app/controllers/anoubis/data/get.rb', line 249

def get_table_actions(row)
  self.etc.data.actions = self.table_actions if !self.etc.data.actions
  result = {}
  self.etc.data.actions.each do |value|
    if self.get_table_action value, row
      result[value.to_sym] = I18n.t(params[:controller].sub('/', '.')+'.table.actions.'+value, title: row.sys_title, default: I18n.t('actions.'+value, title: row.sys_title))
    end
  end
  result
end

#get_table_dataObject

Load data into the system variable self.etc.data.data and return fields defined in controller.



196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/anoubis/data/get.rb', line 196

def get_table_data
  self.load_table_data self.etc.data.limit, self.etc.data.offset
  data = []
  if self.etc.data.data
    self.etc.data.data.each do |row|
      data.push get_data_row row
    end
  end
  data
end

#get_table_data_countInteger

Get total number of rows in defined model. Also sets additional system properties self.etc.data.limit, self.etc.data.offset and self.etc.data.count

Returns:

  • (Integer)

    the total number of rows.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/anoubis/data/get.rb', line 156

def get_table_data_count
  self.load_table_data_count
  self.etc.data.limit = params[:limit] if params.has_key? :limit
  self.etc.data.offset = params[:offset] if params.has_key? :offset
  if self.etc.data.offset >= self.etc.data.count
    if self.etc.data.count > 0
      self.etc.data.offset = ((self.etc.data.count-1) / self.etc.data.limit).truncate * self.etc.data.limit
    else
      self.etc.data.offset = 0
    end
  end
  self.etc.data.count
end

#get_tenant_where(model) ⇒ Object



114
115
116
117
# File 'app/controllers/anoubis/data/get.rb', line 114

def get_tenant_where(model)
  return { tenant_id: self.current_user.tenant_id } if model.new.respond_to? :tenant_id
  return {}
end

#get_whereObject

Get where for controller action



110
111
112
# File 'app/controllers/anoubis/data/get.rb', line 110

def get_where
  self.where
end