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
-
#convert_datetime_to_string(value, format) ⇒ Object
Converts DateTime representation to string value.
-
#convert_db_to_table_value_datetime(key, field, value) ⇒ Hash
Convert value from database to edit form for datetime type.
-
#convert_db_to_table_value_datetime1(key, field, value) ⇒ Object
Convert value from database to table view for datetime type.
-
#convert_db_to_table_value_float(key, field, value) ⇒ Hash
Convert value from database to edit form for float type.
-
#convert_db_to_table_value_float1(key, field, value) ⇒ Hash
Convert value from database to table view for float type.
-
#convert_db_to_table_value_integer1(key, field, value) ⇒ Object
Convert value from database to table view for string type.
-
#convert_db_to_table_value_longlistbox1(key, field, value) ⇒ Object
Convert value from database to table view for longlistbox type.
-
#convert_db_to_table_value_text1(key, field, value) ⇒ Object
Convert value from database to table view for text type.
-
#convert_db_to_view_value(key, value) ⇒ Object
Convert value from database to view format according by defining field type and action.
-
#convert_db_to_view_value_boolean(key, value) ⇒ Object
Convert value from database to view format for ‘boolean’ type.
-
#convert_db_to_view_value_datetime(key, value) ⇒ Object
Convert value from database to table view for datetime type.
-
#convert_db_to_view_value_hash(key, value) ⇒ Hash
Convert value from database to view format for ‘hash’ type.
-
#convert_db_to_view_value_html(key, value) ⇒ Object
Convert value from database to view format for ‘html’ type.
-
#convert_db_to_view_value_key(key, value) ⇒ Object
Convert value from database to view format for ‘key’ type.
-
#convert_db_to_view_value_listbox(key, value) ⇒ Object
Convert value from database to table format for ‘listbox’ type.
-
#convert_db_to_view_value_number(key, value) ⇒ Object
Convert value from database to view format for ‘integer’ type.
-
#convert_db_to_view_value_string(key, value) ⇒ Object
Convert value from database to view format for ‘string’ type.
-
#convert_db_to_view_value_text(key, value) ⇒ Object
Convert value from database to view format for ‘text’ type.
Block of conversion human view values to database format collapse
-
#convert_view_to_db_value(key, value) ⇒ Object
Converts inputted value to database format.
-
#convert_view_to_db_value_boolean(key, value) ⇒ Object
Converts inputted value to database format for ‘boolean’ field type.
-
#convert_view_to_db_value_datetime(key, value) ⇒ Object
Converts inputted value to database format for ‘datetime’ field type.
-
#convert_view_to_db_value_html(key, value) ⇒ Object
Converts inputted value to database format for ‘html’ field type.
-
#convert_view_to_db_value_key(key, value) ⇒ Object
Converts inputted value to database format for ‘key’ field type for ‘create’ action.
-
#convert_view_to_db_value_listbox(key, value) ⇒ Object
Converts inputted value to database format for ‘listbox’ field type for ‘create’ action.
-
#convert_view_to_db_value_number(key, value) ⇒ Object
Converts inputted value to database format for ‘number’ field type.
-
#convert_view_to_db_value_string(key, value) ⇒ Object
Converts inputted value to database format for ‘string’ field type.
-
#convert_view_to_db_value_text(key, value) ⇒ Object
Converts inputted value to database format for ‘text’ field type.
Instance Method Summary collapse
-
#number_format(number, precision = 2, point = ',', separator = '') ⇒ String
Format a number with grouped thousands.
Instance Method Details
#convert_datetime_to_string(value, format) ⇒ Object
Converts DateTime representation to string value
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
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
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
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
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
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
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
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
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
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. if field.format == 'single' new_value = field..list[value.to_s.to_sym] if field..list else new_value = [] if value if field..list value.each do |key| new_value.push field..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
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
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
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.
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.
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.
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.
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
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
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.
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.
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.
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
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 |