Module: Anoubis::Data::Convert

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

Overview

Data conversion moule between database and human representation

Block of conversion database value into human view format collapse

Block of conversion human view values to database format collapse

Instance Method Summary collapse

Instance Method Details

#convert_datetime_to_string(value, format) ⇒ Object

Converts DateTime representation to string value

Parameters:

  • value (DateTime)

    date and time

  • format (String)

    convert representation (‘month’ - return only month and year, ‘date’ - returns only date, ‘datetime’ - returns date and time)

Returns:

  • Returns date and time representation string



184
185
186
187
188
189
190
# File 'app/controllers/anoubis/data/convert.rb', line 184

def convert_datetime_to_string(value, format)
  return I18n.t('anoubis.months.main')[value.month-1]+' '+value.year.to_s if format == 'month'
  return value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s if format == 'date'
  return value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min) if format == 'datetime'

  value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)+':'+('%02d' % value.sec)
end

#convert_db_to_table_value_datetime(key, field, value) ⇒ Hash

Convert value from database to edit form for datetime type

Parameters:

  • key (Symbol)

    field’s identifier

  • field (Hash)

    field’s options

  • value (Any)

    value from database before processing

Returns:

  • (Hash)

    resulting value in format { key: processed_value }



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'app/controllers/anoubis/data/convert.rb', line 266

def convert_db_to_table_value_datetime(key, field, value)
  begin
    value = case field[:format]
            when 'month' then I18n.t('anoubis.months.main')[value.month-1]+' '+value.year.to_s
            when 'date' then value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s
            when 'datetime' then value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)
            else value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)+':'+('%02d' % value.sec)
            end
  rescue
    value = I18n.t('incorrect_field_format')
  end
  return { key => value }
end

#convert_db_to_table_value_datetime1(key, field, value) ⇒ Object

Convert value from database to table view for datetime type



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/controllers/anoubis/data/convert.rb', line 236

def convert_db_to_table_value_datetime1(key, field, value)
  begin
    value = case field[:format]
            when 'month' then I18n.t('anoubis.months.main')[value.month-1]+' '+value.year.to_s
            when 'date' then value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s
            when 'datetime' then value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)
            else value.day.to_s+' '+ I18n.t('anoubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)+':'+('%02d' % value.sec)
            end
  rescue
    value = I18n.t('incorrect_field_format')
  end
  return { key => value }
end

#convert_db_to_table_value_float(key, field, value) ⇒ Hash

Convert value from database to edit form for float type

Parameters:

  • key (Symbol)

    field’s identifier

  • field (Anoubis::Etc::Data#fields)

    field’s options

  • value (Float)

    value from database before processing

Returns:

  • (Hash)

    return resulting value at format { key+‘_view’: processed_value, key: value }



286
287
288
# File 'app/controllers/anoubis/data/convert.rb', line 286

def convert_db_to_table_value_float(key, field, value)
  return { (key.to_s+'_view').to_sym => number_format(value, field[:precision], field[:point], field[:separator]), key => value}
end

#convert_db_to_table_value_float1(key, field, value) ⇒ Hash

Convert value from database to table view for float type

Parameters:

  • key (Symbol)

    key of table field

  • field (Tims::Etc::Table#fields)

    set of options of field by key

  • value (Float)

    value from database before processing

Returns:

  • (Hash)

    return resulting value at format { key: processed_value, ‘raw_’+key: value }



256
257
258
# File 'app/controllers/anoubis/data/convert.rb', line 256

def convert_db_to_table_value_float1(key, field, value)
  return { key => number_format(value, field[:precision], field[:point], field[:separator]), ('raw_'+key.to_s).to_sym => value}
end

#convert_db_to_table_value_integer1(key, field, value) ⇒ Object

Convert value from database to table view for string type



219
220
221
222
# File 'app/controllers/anoubis/data/convert.rb', line 219

def convert_db_to_table_value_integer1(key, field, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_table_value_longlistbox1(key, field, value) ⇒ Object

Convert value from database to table view for longlistbox type



229
230
231
232
# File 'app/controllers/anoubis/data/convert.rb', line 229

def convert_db_to_table_value_longlistbox1(key, field, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_table_value_text1(key, field, value) ⇒ Object

Convert value from database to table view for text type



207
208
209
210
211
# File 'app/controllers/anoubis/data/convert.rb', line 207

def convert_db_to_table_value_text1(key, field, value)
  return { key => '', ('raw_'+key.to_s).to_sym => '' } if !value
  new_value = ERB::Util.html_escape(value).to_s.gsub(/(?:\n\r?|\r\n?)/, '<br/>')
  return { key => new_value, ('raw_'+key.to_s).to_sym => value }
end

#convert_db_to_view_value(key, value) ⇒ Object

Convert value from database to view format according by defining field type and action

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



32
33
34
35
36
37
38
# File 'app/controllers/anoubis/data/convert.rb', line 32

def convert_db_to_view_value(key, value)
  field = self.etc.data.fields[key]
  return { key => value } if !field.type
  proc = format('convert_db_to_view_value_%s', field.type)
  result = self.send proc, key, value
  result
end

#convert_db_to_view_value_boolean(key, value) ⇒ Object

Convert value from database to view format for ‘boolean’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (Boolean)

    value from database



53
54
55
56
# File 'app/controllers/anoubis/data/convert.rb', line 53

def convert_db_to_view_value_boolean(key, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_view_value_datetime(key, value) ⇒ Object

Convert value from database to table view for datetime type



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/controllers/anoubis/data/convert.rb', line 140

def convert_db_to_view_value_datetime(key, value)
  field = self.etc.data.fields[key]
  #puts key
  #puts value.class
  if (value.class == Date) || (value.class == ActiveSupport::TimeWithZone)
    begin
      new_value = convert_datetime_to_string value, field.format

      if %w[month date].include? field.format
        raw_value = value.year.to_s + '-' + ('%02d' % value.month) + '-' + ('%02d' % value.day)
      else
        #raw_value = value.year.to_s + '-' + ('%02d' % value.month) + '-' + ('%02d' % value.day) + ' ' + ('%02d' % value.hour) + ':' + ('%02d' % value.min)
        raw_value = value.iso8601(2)[0..18]
      end
    rescue StandardError => e
      #puts e
      new_value = field.error_text
    end
  else
    new_value = '';
  end

  case self.etc.action
  when 'new', 'edit'
    return { key => raw_value, format('%s_view', key).to_sym => new_value }
  end
  return { key => new_value, format('%s_raw', key).to_sym => value }
end

#convert_db_to_view_value_hash(key, value) ⇒ Hash

Convert value from database to view format for ‘hash’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database

Returns:

  • (Hash)

    returns text representation of database value



174
175
176
177
# File 'app/controllers/anoubis/data/convert.rb', line 174

def convert_db_to_view_value_hash(key, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_view_value_html(key, value) ⇒ Object

Convert value from database to view format for ‘html’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



81
82
83
84
# File 'app/controllers/anoubis/data/convert.rb', line 81

def convert_db_to_view_value_html(key, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_view_value_key(key, value) ⇒ Object

Convert value from database to view format for ‘key’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



133
134
135
136
# File 'app/controllers/anoubis/data/convert.rb', line 133

def convert_db_to_view_value_key(key, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_view_value_listbox(key, value) ⇒ Object

Convert value from database to table format for ‘listbox’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/anoubis/data/convert.rb', line 90

def convert_db_to_view_value_listbox(key, value)
  field = self.etc.data.fields[key]
  new_value = ''
  if field.options
    if field.format == 'single'
      new_value = field.options.list[value.to_s.to_sym] if field.options.list
    else
      new_value = []
      if value
        if field.options.list
          value.each do |key|
            new_value.push field.options.list[key.to_s.to_sym]
          end
        end
      end
    end
  end
  case self.etc.action
  when 'index', 'show', 'export'
    if field.format == 'single'
      return { key => new_value, format('%s_raw', key).to_sym => value }
    else
      return { key => new_value.join(', '), format('%s_raw', key).to_sym => new_value }
    end
  when 'new', 'edit'
    if field.format == 'single'
      return { key => value.to_s, format('%s_view', key).to_sym => new_value }
    else
      return { key => value, format('%s_view', key).to_sym => new_value.join(', ') }
    end
  else
    if field.format == 'single'
      return { key => value.to_s, format('%s_view', key).to_sym => new_value }
    else
      return { key => value, format('%s_view', key).to_sym => new_value.join(', ') }
    end
  end
end

#convert_db_to_view_value_number(key, value) ⇒ Object

Convert value from database to view format for ‘integer’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



62
63
64
65
66
# File 'app/controllers/anoubis/data/convert.rb', line 62

def convert_db_to_view_value_number(key, value)
  return { key => self.etc.data.fields[key].error_text } if !value
  return { key => value.to_s.to_i } if self.etc.data.fields[key].precision == 0
  return { key => format('%.' + self.etc.data.fields[key].precision.to_s + 'f', value) }
end

#convert_db_to_view_value_string(key, value) ⇒ Object

Convert value from database to view format for ‘string’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



44
45
46
47
# File 'app/controllers/anoubis/data/convert.rb', line 44

def convert_db_to_view_value_string(key, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_db_to_view_value_text(key, value) ⇒ Object

Convert value from database to view format for ‘text’ type

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from database



72
73
74
75
# File 'app/controllers/anoubis/data/convert.rb', line 72

def convert_db_to_view_value_text(key, value)
  return { key => '' } if !value
  return { key => value }
end

#convert_view_to_db_value(key, value) ⇒ Object

Converts inputted value to database format. Field type is got from self.etc.data.fields according by key. Resulting data is placed into self.etc.data.data attribute according by key. Errors are placed in self.output.errors array according by key.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    value from user input



302
303
304
305
306
307
308
# File 'app/controllers/anoubis/data/convert.rb', line 302

def convert_view_to_db_value(key, value)
  field = self.etc.data.fields[key]
  return { key => value } unless field
  return { key => value } unless field.type
  proc = format('convert_view_to_db_value_%s', field.type)
  self.send proc, key, value
end

#convert_view_to_db_value_boolean(key, value) ⇒ Object

Converts inputted value to database format for ‘boolean’ field type.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (Boolean)

    inputted value



324
325
326
327
328
# File 'app/controllers/anoubis/data/convert.rb', line 324

def convert_view_to_db_value_boolean(key, value)
  proc = format('self.etc.data.data.%s = value', key)
  #self.etc.data.data[key] = value
  eval proc
end

#convert_view_to_db_value_datetime(key, value) ⇒ Object

Converts inputted value to database format for ‘datetime’ field type.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/controllers/anoubis/data/convert.rb', line 405

def convert_view_to_db_value_datetime(key, value)
  zone = ActiveSupport::TimeZone[self.current_user.timezone]
  offset = if zone.utc_offset/3600 < 0 then (zone.utc_offset/3600).to_s else '+'+(zone.utc_offset/3600).to_s end
  #puts 'convert_view_to_db_value_datetime'
  #puts value
  value = Time.zone.parse value
  #puts value
  #puts zone
  #puts offset
  #puts value.utc_offset if value
  proc = format('self.etc.data.data.%s = value', key)
  #self.etc.data.data[key] = value
  eval proc
end

#convert_view_to_db_value_html(key, value) ⇒ Object

Converts inputted value to database format for ‘html’ field type.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



360
361
362
363
364
# File 'app/controllers/anoubis/data/convert.rb', line 360

def convert_view_to_db_value_html(key, value)
  proc = format('self.etc.data.data.%s = value', key)
  #self.etc.data.data[key] = value
  eval proc
end

#convert_view_to_db_value_key(key, value) ⇒ Object

Converts inputted value to database format for ‘key’ field type for ‘create’ action

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'app/controllers/anoubis/data/convert.rb', line 387

def convert_view_to_db_value_key(key, value)
  field = self.etc.data.fields[key]
  where = {}
  where[field.model.title.to_s.to_sym] = value
  value = field.model.model.where(where).first
  proc = format('self.etc.data.data.%s = value', field.key)
  eval(proc)
  #begin
  #          self.etc.data.data[key] = value
  #        rescue
  #          self.etc.data.data[key] = nil
  #        end
end

#convert_view_to_db_value_listbox(key, value) ⇒ Object

Converts inputted value to database format for ‘listbox’ field type for ‘create’ action

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



371
372
373
374
375
376
377
378
379
380
# File 'app/controllers/anoubis/data/convert.rb', line 371

def convert_view_to_db_value_listbox(key, value)
  field = self.etc.data.fields[key]
  begin
    proc = format('self.etc.data.data.%s = value', field.field)
    #self.etc.data.data[field.field] = value
    eval proc
  rescue
    self.etc.data.data[field.field] = nil
  end
end

#convert_view_to_db_value_number(key, value) ⇒ Object

Converts inputted value to database format for ‘number’ field type.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



334
335
336
337
338
339
340
341
342
343
344
# File 'app/controllers/anoubis/data/convert.rb', line 334

def convert_view_to_db_value_number(key, value)
  field = self.etc.data.fields[key]
  if field.precision == 0
    value = value.to_s.to_i
  else
    value = value.to_s.to_f
  end
  proc = format('self.etc.data.data.%s = value', key)
  #self.etc.data.data[key] = value
  eval proc
end

#convert_view_to_db_value_string(key, value) ⇒ Object

Converts inputted value to database format for ‘string’ field type.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



314
315
316
317
318
# File 'app/controllers/anoubis/data/convert.rb', line 314

def convert_view_to_db_value_string(key, value)
  proc = format('self.etc.data.data.%s = value', key)
  #self.etc.data.data[key] = value
  eval proc
end

#convert_view_to_db_value_text(key, value) ⇒ Object

Converts inputted value to database format for ‘text’ field type.

Parameters:

  • key (Symbol)

    field’s identifier in self.etc.data.fields structure

  • value (String)

    inputted value



350
351
352
353
354
# File 'app/controllers/anoubis/data/convert.rb', line 350

def convert_view_to_db_value_text(key, value)
  proc = format('self.etc.data.data.%s = value', key)
  #self.etc.data.data[key] = value
  eval proc
end

#number_format(number, precision = 2, point = ',', separator = '') ⇒ String

Format a number with grouped thousands

Parameters:

  • number (Float)

    The number being formatted.

  • precision (Integer) (defaults to: 2)

    Sets the number of decimal points.

  • point (Char) (defaults to: ',')

    Sets the separator for the decimal point.

  • separator (Char) (defaults to: '')

    Sets the thousands separator.

Returns:

  • (String)

    A formatted version of number.



13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/anoubis/data/convert.rb', line 13

def number_format(number, precision = 2, point = ',', separator = '')
  val = sprintf('%.'+precision.to_s+'f', number.round(precision)).to_s
  if separator != '' && number >= 1000
    whole_part, decimal_part = val.split('.')
    val = [whole_part.gsub(/(\d)(?=\d{3}+$)/, '\1'+separator), decimal_part].compact.join(point)
  else
    val = val.gsub('.', point)
  end
  val
end