Class: Anoubis::Etc::Field

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/anoubis/etc/field.rb

Overview

Definitions of field options for table column or new (edit) form field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, model, options = {}) ⇒ Field

Sets default parameters for field

Parameters:

  • key (Symbol)

    field’s identifier

  • model (ActiveRecord)

    field’s model

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

    field’s initial options



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
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/anoubis/etc/field.rb', line 95

def initialize(key, model, options = {})
  #puts key
  #puts options
  self.field = nil
  self.key = key.to_s
  self.format = ''
  self.precision = 0
  self.title = options[:title] if options.key? :title
  self.type = if options.key? :type then options[:type] else 'string' end
  self.visible = if options.key? :visible then options[:visible] else true end
  self.options = if options.key? :options then FieldOptions.new(options[:options]) else nil end
  if options.key? :order
    if options[:order].class == Hash
      options[:order][:field] = Kernel.format('%s.%s', model.table_name, self.key.to_s) if !options[:order].key? :field
    end
    self.order = if options.key? :order then FieldOrder.new(options[:order]) else nil end
  end
  self.model = if options.key? :model then Model.new(options[:model]) else nil end
  self.editable = if options.key? :editable then options[:editable] else nil end
  self.autocomplete = if options.key? :autocomplete then options[:autocomplete] else nil end
  self.error_text = if options.key? :error_text then options[:error_text] else I18n.t('errors.field_error') end

  self.send Kernel.format('initialize_%s', self.type), options

  if !options.key? :table_field
    if !options.key? :field
      if self.order
        options[:table_field] = self.order.field if self.order.field
      end
    end
  end

  if !self.field
    self.field = if options.key? :field then options[:field] else self.key end
  end

  self.table_field = if options.key?(:table_field) then options[:table_field] else Kernel.format('%s.%s', model.table_name, self.field) end

  if self.autocomplete
    self.autocomplete[:limit] = 10 if !autocomplete.key? :limit
    self.autocomplete[:count] = 3 if !autocomplete.key? :count
    self.autocomplete[:where] = []
  end
  #puts self.to_h
end

Instance Attribute Details

#autocompleteBoolean, Hash

Describes if this field could return data for autocomplete action. Options:

  • :limit (Integer) – maximum number of elements (defaults to: 10)

  • :count (Integer) – Minimum symbols count for output (defaults to: 3)

Returns:

  • (Boolean)

    possibility for return data on autocomplete action.

  • (Hash)

    autocomplete definitions for field



72
# File 'app/controllers/anoubis/etc/field.rb', line 72

class_attribute :autocomplete, default: false

#editableString

Defines if key of this field can be edited

Returns:

  • (String)

    returns path for edit field options



88
# File 'app/controllers/anoubis/etc/field.rb', line 88

class_attribute :editable, default: nil

#error_textString

Text is shown when system can’t access to data with presented #field name

Returns:

  • (String)

    field’s error_text



57
# File 'app/controllers/anoubis/etc/field.rb', line 57

class_attribute :error_text, default: ''

#fieldString

Field’s name is used for access field value in model

Returns:

  • (String)

    field’s name



47
# File 'app/controllers/anoubis/etc/field.rb', line 47

class_attribute :field, default: nil

#keyString

Field’s identifier

Returns:

  • (String)

    field’s identifier



42
# File 'app/controllers/anoubis/etc/field.rb', line 42

class_attribute :key, default: nil

#modelModel

Defines model’s description for complex field

Options:

  • :model (ActiveRecord) – model class

  • :title (Symbol) – field name is used for receive options titles (defaults to: :title)

  • :order (Symbol) – field name is used for order options (defaults to: :title option)

  • :where (Hash) – where parameters for select data from model (defaults to: {})

Returns:

  • (Model)

    model’s description for complex field



83
# File 'app/controllers/anoubis/etc/field.rb', line 83

class_attribute :model, default: nil

#optionsFieldOptions

Describes additional field’s options for type ‘checkbox’, ‘listbox’.

Returns:



62
# File 'app/controllers/anoubis/etc/field.rb', line 62

class_attribute :options, default: nil

#orderFieldOrder

Defines field order options.

Returns:



32
# File 'app/controllers/anoubis/etc/field.rb', line 32

class_attribute :order, default: nil

#table_fieldString

Field’s name is used for operation with table data (like ‘where’, ‘order’ and etc.)

Returns:

  • (String)

    field’s name in the table



52
# File 'app/controllers/anoubis/etc/field.rb', line 52

class_attribute :table_field, default: nil

#titleString

Defines field title.

Returns:

  • (String)

    field’s title



9
# File 'app/controllers/anoubis/etc/field.rb', line 9

class_attribute :title, default: nil

#typeString

Defines field type

Possible values of field’s type are ‘string’, ‘integer’, ‘float’, ‘listbox’, ‘checkbox’, ‘longlistbox’ and ‘datetime’

Returns:

  • (String)

    field’s type.



17
# File 'app/controllers/anoubis/etc/field.rb', line 17

class_attribute :type, default: ''

#visibleBoolean

Defines field’s visibility for table representation data

Returns:

  • (Boolean)

    field’s visibility



37
# File 'app/controllers/anoubis/etc/field.rb', line 37

class_attribute :visible, default: true

Instance Method Details

#formatString

Defines date format for field type ‘datetime’ Possible values of field’s format are ‘date’, ‘full’, ‘datetime’, ‘month’, ‘year’.

Returns:

  • (String)

    field’s type.



22
# File 'app/controllers/anoubis/etc/field.rb', line 22

class_attribute :format, default: ''

#hash_to_json(hash) ⇒ Array

Convert hash to array json output

Parameters:

  • hash (Hash)

    hash representation

Returns:

  • (Array)

    array representation



464
465
466
467
468
469
470
# File 'app/controllers/anoubis/etc/field.rb', line 464

def hash_to_json(hash)
  result = []
  hash.each_key do |key|
    result.push({ key: key.to_s, value: hash[key] })
  end
  result
end

#initialize_boolean(options) ⇒ Object

Initialize additional parameters for ‘boolean’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



151
152
153
# File 'app/controllers/anoubis/etc/field.rb', line 151

def initialize_boolean (options)

end

#initialize_datetime(options) ⇒ Object

Initialize additional parameters for ‘datetime’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



193
194
195
196
197
# File 'app/controllers/anoubis/etc/field.rb', line 193

def initialize_datetime (options)
  options[:format] = 'datetime' if !options.key? :format
  options[:format] = 'datetime' if !%w[date datetime full year month].include? options[:format]
  self.format = options[:format]
end

#initialize_hash(options) ⇒ Object

Initialize additional parameters for ‘hash’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



158
159
160
# File 'app/controllers/anoubis/etc/field.rb', line 158

def initialize_hash (options)

end

#initialize_html(options) ⇒ Object

Initialize additional parameters for ‘html’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



186
187
188
# File 'app/controllers/anoubis/etc/field.rb', line 186

def initialize_html (options)

end

#initialize_key(options) ⇒ Object

Initialize additional parameters for ‘key’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



232
233
234
235
236
237
238
239
# File 'app/controllers/anoubis/etc/field.rb', line 232

def initialize_key (options)
  if self.model
    self.error_text = '' if options[:action] == 'new'
    self.field = Kernel.format('%s.%s', self.key, self.model.title) if !options.key? :field
    self.table_field = Kernel.format('%s.%s', self.model.model.table_name, self.model.title) if !self.table_field
    self.autocomplete = {} if !options.key? :autocomplete
  end
end

#initialize_listbox(options) ⇒ Object

Setups additional parameters for ‘listbox’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/anoubis/etc/field.rb', line 202

def initialize_listbox (options)
  self.options = FieldOptions.new if !self.options
  if !self.options.list
    if self.model
      if !(%w[update create].include?(options[:action]))
        self.options.show = 'update'
        if self.options.line
          self.options.list = self.options.line
        else
          self.options.list = {}
        end
        self.model.model.select(self.model.select).where(self.model.where).order(self.model.order).each do |dat|
          self.options.list[dat.id.to_s.to_sym] = dat.send(self.model.title)
          if dat.respond_to? :updated_at
            if self.model.updated_at < dat.updated_at.to_time.utc.to_i
              self.model.updated_at = dat.updated_at.to_time.utc.to_i
            end
          end
        end
      end
    end
  end
  options[:format] = 'single' unless options.key? :format
  options[:format] = 'single' unless %w[single multiple].include? options[:format]
  self.format = options[:format]
end

#initialize_number(options) ⇒ Object

Initialize additional parameters for ‘number’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/anoubis/etc/field.rb', line 165

def initialize_number (options)
  if options.key? :error_text
    self.error_text = options[:error_text]
  else
    self.error_text = ''
  end
  self.precision = options[:precision].to_s.to_i if options.key? :precision
  self.precision = 0 if self.precision < 0
  self.precision = 16 if self.precision > 16
end

#initialize_string(options) ⇒ Object

Initialize additional parameters for ‘string’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



144
145
146
# File 'app/controllers/anoubis/etc/field.rb', line 144

def initialize_string (options)

end

#initialize_text(options) ⇒ Object

Initialize additional parameters for ‘text’ field type for controller actions.

Parameters:

  • options (Hash)

    field’s initial options



179
180
181
# File 'app/controllers/anoubis/etc/field.rb', line 179

def initialize_text (options)

end

#precisionString

Defines precision for field type ‘number’ Possible values of this field is integer numbers between 0 and 6. If precision is 0 then number is integer.

Returns:

  • (String)

    field’s type.



27
# File 'app/controllers/anoubis/etc/field.rb', line 27

class_attribute :precision, default: 0

#properties(model, action) ⇒ Hash

Return field properties for frontend application

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

Returns:

  • (Hash)

    field’s properties for defined action



246
247
248
249
250
251
# File 'app/controllers/anoubis/etc/field.rb', line 246

def properties (model, action)
  if %w[new edit].include? action
    return self.properties_forms model, action
  end
  self.properties_index model, action
end

#properties_forms(model, action) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

Returns:

  • (Hash)

    field’s properties for defined action



284
285
286
287
288
289
290
291
292
293
# File 'app/controllers/anoubis/etc/field.rb', line 284

def properties_forms (model, action)
  mod = model.new
  result = {
      id: self.key,
      title: model.human_attribute_name(self.key),
      type: self.type
  }
  result.merge!(self.send(Kernel.format('properties_forms_%s', self.type), model, action, mod))
  result
end

#properties_forms_datetime(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘datetime’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/controllers/anoubis/etc/field.rb', line 415

def properties_forms_datetime (model, action, mod)
  result = {}
  errors = {}
  res = model.validators_on(self.key.to_sym).detect { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
  if res
    result[:required] = true
    errors[:required] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :blank)
  end
  result[:format] = self.format
  result[:errors] = errors if errors.length > 0
  result
end

#properties_forms_html(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘html’.

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



369
370
371
# File 'app/controllers/anoubis/etc/field.rb', line 369

def properties_forms_html (model, action, mod)
  self.properties_forms_text model, action, mod
end

#properties_forms_key(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘key’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'app/controllers/anoubis/etc/field.rb', line 392

def properties_forms_key (model, action, mod)
  result = {
      type: 'string',
      autocomplete: true,
      editable: self.editable
  }
  errors = {}
  result[:field] = self.model.title if self.editable
  res = model.validators_on(self.key.to_sym).detect { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
  if res
    result[:required] = true
    errors[:required] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :blank)
  end
  result[:errors] = errors if errors.length > 0
  result
end

#properties_forms_listbox(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘listbox’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



379
380
381
382
383
384
# File 'app/controllers/anoubis/etc/field.rb', line 379

def properties_forms_listbox (model, action, mod)
  result = {}
  result[:format] = self.format if self.format == 'multiple'
  result[:options] = self.hash_to_json(self.options.list) if self.options
  result
end

#properties_forms_number(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘number’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'app/controllers/anoubis/etc/field.rb', line 330

def properties_forms_number (model, action, mod)
  result = {}
  errors = {}
  res = model.validators_on(self.key.to_sym).detect { |v| v.is_a?(ActiveModel::Validations::NumericalityValidator) }
  if res
    if res.options.key? :greater_than_or_equal_to
      result[:min] = res.options[:greater_than_or_equal_to]
      errors[:min] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :greater_than_or_equal_to, { count: result[:min] })
    end
    if res.options.key? :maximum
      result[:max] = res.options[:maximum]
      errors[:max] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :too_long, { count: result[:max] })
    end
  end
  res = model.validators_on(self.key.to_sym).detect { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
  if res
    result[:required] = true
    errors[:required] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :blank)
  end
  result[:errors] = errors if errors.length > 0
  result
end

#properties_forms_string(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘string’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/controllers/anoubis/etc/field.rb', line 301

def properties_forms_string (model, action, mod)
  result = {}
  errors = {}
  res = model.validators_on(self.key.to_sym).detect { |v| v.is_a?(ActiveModel::Validations::LengthValidator) }
  if res
    if res.options.key? :minimum
      result[:min] = res.options[:minimum]
      errors[:min] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :too_short, { count: result[:min] })
    end
    if res.options.key? :maximum
      result[:max] = res.options[:maximum]
      errors[:max] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :too_long, { count: result[:max] })
    end
  end
  res = model.validators_on(self.key.to_sym).detect { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
  if res
    result[:required] = true
    errors[:required] = model.human_attribute_name(self.key) + ' ' + mod.errors.generate_message(self.key.to_sym, :blank)
  end
  result[:errors] = errors if errors.length > 0
  result
end

#properties_forms_text(model, action, mod) ⇒ Hash

Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘text’.

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

  • mod (ActiveRecord)

    initialized new model element

Returns:

  • (Hash)

    field’s properties for defined action



359
360
361
# File 'app/controllers/anoubis/etc/field.rb', line 359

def properties_forms_text (model, action, mod)
  self.properties_forms_string model, action, mod
end

#properties_index(model, action) ⇒ Hash

Return field properties for frontend application for action ‘index’

Parameters:

  • model (ActiveRecord)

    field’s model

  • action (Srting)

    current field action

Returns:

  • (Hash)

    field’s properties for defined action



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/controllers/anoubis/etc/field.rb', line 258

def properties_index (model, action)
  result = {
      id: self.key,
      type: self.type,
      sortable: self.order != nil
  }

  if self.title
    result[:title] = self.title
  else
    result[:title] = model.human_attribute_name(self.key)
  end

  result[:editable] = self.editable if self.editable != nil
  result[:editable] = false if self.type == 'key'
  result[:format] = self.format if self.type == 'datetime'
  result[:precision] = self.precision if self.type == 'number'
  result[:options] = self.hash_to_json(self.options.list) if self.options
  result
end

#to_hHash

Generates hash representation of all class parameters,

Returns:

  • (Hash)

    hash representation of all data



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'app/controllers/anoubis/etc/field.rb', line 431

def to_h
  result = {
      key: self.key,
      type: self.type,
      visible: self.visible,
      field: self.field,
      table_field: self.table_field,
      error_text: self.error_text,
      autocomplete: self.autocomplete
  }
  result[:format] = self.format if self.type == 'datetime'
  if self.editable
    result[:editable] = self.editable
    result[:field] = self.model.title if self.model
  else
    result[:editable] = self.editable if self.editable != nil
  end
  if self.model
    result[:model] = self.model.to_h
  end
  if self.options
    result[:options] = self.options.to_h
  end
  if self.order
    result[:order] = self.order.to_h
  end
  result
end