Module: Anubis::Core::Data::Convert

Included in:
Anubis::Core::DataController, Sso::Client::Data::Convert, Tenant::Data::Convert
Defined in:
app/controllers/anoubis/core/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_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 }



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/controllers/anoubis/core/data/convert.rb', line 250

def convert_db_to_table_value_datetime(key, field, value)
  begin
    value = case field[:format]
            when 'month' then I18n.t('months.main')[value.month-1]+' '+value.year.to_s
            when 'date' then value.day.to_s+' '+ I18n.t('months.second')[value.month-1]+' '+value.year.to_s
            when 'datetime' then value.day.to_s+' '+ I18n.t('months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)
            else value.day.to_s+' '+ I18n.t('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



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/controllers/anoubis/core/data/convert.rb', line 220

def convert_db_to_table_value_datetime1(key, field, value)
  begin
    value = case field[:format]
            when 'month' then I18n.t('months.main')[value.month-1]+' '+value.year.to_s
            when 'date' then value.day.to_s+' '+ I18n.t('months.second')[value.month-1]+' '+value.year.to_s
            when 'datetime' then value.day.to_s+' '+ I18n.t('months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)
            else value.day.to_s+' '+ I18n.t('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 (Anubis::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 }



270
271
272
# File 'app/controllers/anoubis/core/data/convert.rb', line 270

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 }



240
241
242
# File 'app/controllers/anoubis/core/data/convert.rb', line 240

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



203
204
205
206
# File 'app/controllers/anoubis/core/data/convert.rb', line 203

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



213
214
215
216
# File 'app/controllers/anoubis/core/data/convert.rb', line 213

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



191
192
193
194
195
# File 'app/controllers/anoubis/core/data/convert.rb', line 191

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



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

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



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

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



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
168
169
170
171
172
# File 'app/controllers/anoubis/core/data/convert.rb', line 141

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 = case field.format
                  when 'month' then I18n.t('anubis.months.main')[value.month-1]+' '+value.year.to_s
                  when 'date' then value.day.to_s+' '+ I18n.t('anubis.months.second')[value.month-1]+' '+value.year.to_s
                  when 'datetime' then value.day.to_s+' '+ I18n.t('anubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)
                  else value.day.to_s+' '+ I18n.t('anubis.months.second')[value.month-1]+' '+value.year.to_s+', '+value.hour.to_s+':'+('%02d' % value.min)+':'+('%02d' % value.sec)
                  end
      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_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



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

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



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

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



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
128
# File 'app/controllers/anoubis/core/data/convert.rb', line 91

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



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

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



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

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



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

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



286
287
288
289
290
291
292
# File 'app/controllers/anoubis/core/data/convert.rb', line 286

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



308
309
310
311
312
# File 'app/controllers/anoubis/core/data/convert.rb', line 308

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



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

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



344
345
346
347
348
# File 'app/controllers/anoubis/core/data/convert.rb', line 344

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



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

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



355
356
357
358
359
360
361
362
363
364
# File 'app/controllers/anoubis/core/data/convert.rb', line 355

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



318
319
320
321
322
323
324
325
326
327
328
# File 'app/controllers/anoubis/core/data/convert.rb', line 318

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



298
299
300
301
302
# File 'app/controllers/anoubis/core/data/convert.rb', line 298

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



334
335
336
337
338
# File 'app/controllers/anoubis/core/data/convert.rb', line 334

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.



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

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