Class: Anoubis::Etc::Field
- Inherits:
-
Object
- Object
- Anoubis::Etc::Field
- 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
-
#autocomplete ⇒ Boolean, Hash
Describes if this field could return data for autocomplete action.
-
#editable ⇒ String
Defines if key of this field can be edited.
-
#error_text ⇒ String
Text is shown when system can’t access to data with presented #field name.
-
#field ⇒ String
Field’s name is used for access field value in model.
-
#key ⇒ String
Field’s identifier.
-
#model ⇒ Model
Defines model’s description for complex field.
-
#options ⇒ FieldOptions
Describes additional field’s options for type ‘checkbox’, ‘listbox’.
-
#order ⇒ FieldOrder
Defines field order options.
-
#table_field ⇒ String
Field’s name is used for operation with table data (like ‘where’, ‘order’ and etc.).
-
#title ⇒ String
Defines field title.
-
#type ⇒ String
Defines field type.
-
#visible ⇒ Boolean
Defines field’s visibility for table representation data.
Instance Method Summary collapse
-
#format ⇒ String
Defines date format for field type ‘datetime’ Possible values of field’s format are ‘date’, ‘full’, ‘datetime’, ‘month’, ‘year’.
-
#hash_to_json(hash) ⇒ Array
Convert hash to array json output.
-
#initialize(key, model, options = {}) ⇒ Field
constructor
Sets default parameters for field.
-
#initialize_boolean(options) ⇒ Object
Initialize additional parameters for ‘boolean’ field type for controller actions.
-
#initialize_datetime(options) ⇒ Object
Initialize additional parameters for ‘datetime’ field type for controller actions.
-
#initialize_hash(options) ⇒ Object
Initialize additional parameters for ‘hash’ field type for controller actions.
-
#initialize_html(options) ⇒ Object
Initialize additional parameters for ‘html’ field type for controller actions.
-
#initialize_key(options) ⇒ Object
Initialize additional parameters for ‘key’ field type for controller actions.
-
#initialize_listbox(options) ⇒ Object
Setups additional parameters for ‘listbox’ field type for controller actions.
-
#initialize_number(options) ⇒ Object
Initialize additional parameters for ‘number’ field type for controller actions.
-
#initialize_string(options) ⇒ Object
Initialize additional parameters for ‘string’ field type for controller actions.
-
#initialize_text(options) ⇒ Object
Initialize additional parameters for ‘text’ field type for controller actions.
-
#precision ⇒ String
Defines precision for field type ‘number’ Possible values of this field is integer numbers between 0 and 6.
-
#properties(model, action) ⇒ Hash
Return field properties for frontend application.
-
#properties_forms(model, action) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’.
-
#properties_forms_datetime(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘datetime’.
-
#properties_forms_html(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘html’.
-
#properties_forms_key(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘key’.
-
#properties_forms_listbox(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘listbox’.
-
#properties_forms_number(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘number’.
-
#properties_forms_string(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘string’.
-
#properties_forms_text(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘text’.
-
#properties_index(model, action) ⇒ Hash
Return field properties for frontend application for action ‘index’.
-
#to_h ⇒ Hash
Generates hash representation of all class parameters,.
Constructor Details
#initialize(key, model, options = {}) ⇒ Field
Sets default parameters for field
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, = {}) #puts key #puts options self.field = nil self.key = key.to_s self.format = '' self.precision = 0 self.title = [:title] if .key? :title self.type = if .key? :type then [:type] else 'string' end self.visible = if .key? :visible then [:visible] else true end self. = if .key? :options then FieldOptions.new([:options]) else nil end if .key? :order if [:order].class == Hash [:order][:field] = Kernel.format('%s.%s', model.table_name, self.key.to_s) if ![:order].key? :field end self.order = if .key? :order then FieldOrder.new([:order]) else nil end end self.model = if .key? :model then Model.new([:model]) else nil end self.editable = if .key? :editable then [:editable] else nil end self.autocomplete = if .key? :autocomplete then [:autocomplete] else nil end self.error_text = if .key? :error_text then [:error_text] else I18n.t('errors.field_error') end self.send Kernel.format('initialize_%s', self.type), if !.key? :table_field if !.key? :field if self.order [:table_field] = self.order.field if self.order.field end end end if !self.field self.field = if .key? :field then [:field] else self.key end end self.table_field = if .key?(:table_field) then [: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
#autocomplete ⇒ Boolean, 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)
72 |
# File 'app/controllers/anoubis/etc/field.rb', line 72 class_attribute :autocomplete, default: false |
#editable ⇒ String
Defines if key of this field can be edited
88 |
# File 'app/controllers/anoubis/etc/field.rb', line 88 class_attribute :editable, default: nil |
#error_text ⇒ String
Text is shown when system can’t access to data with presented #field name
57 |
# File 'app/controllers/anoubis/etc/field.rb', line 57 class_attribute :error_text, default: '' |
#field ⇒ String
Field’s name is used for access field value in model
47 |
# File 'app/controllers/anoubis/etc/field.rb', line 47 class_attribute :field, default: nil |
#key ⇒ String
Field’s identifier
42 |
# File 'app/controllers/anoubis/etc/field.rb', line 42 class_attribute :key, default: nil |
#model ⇒ Model
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: {})
83 |
# File 'app/controllers/anoubis/etc/field.rb', line 83 class_attribute :model, default: nil |
#options ⇒ FieldOptions
Describes additional field’s options for type ‘checkbox’, ‘listbox’.
62 |
# File 'app/controllers/anoubis/etc/field.rb', line 62 class_attribute :options, default: nil |
#order ⇒ FieldOrder
Defines field order options.
32 |
# File 'app/controllers/anoubis/etc/field.rb', line 32 class_attribute :order, default: nil |
#table_field ⇒ String
Field’s name is used for operation with table data (like ‘where’, ‘order’ and etc.)
52 |
# File 'app/controllers/anoubis/etc/field.rb', line 52 class_attribute :table_field, default: nil |
#title ⇒ String
Defines field title.
9 |
# File 'app/controllers/anoubis/etc/field.rb', line 9 class_attribute :title, default: nil |
#type ⇒ String
Defines field type
Possible values of field’s type are ‘string’, ‘integer’, ‘float’, ‘listbox’, ‘checkbox’, ‘longlistbox’ and ‘datetime’
17 |
# File 'app/controllers/anoubis/etc/field.rb', line 17 class_attribute :type, default: '' |
#visible ⇒ Boolean
Defines field’s visibility for table representation data
37 |
# File 'app/controllers/anoubis/etc/field.rb', line 37 class_attribute :visible, default: true |
Instance Method Details
#format ⇒ String
Defines date format for field type ‘datetime’ Possible values of field’s format are ‘date’, ‘full’, ‘datetime’, ‘month’, ‘year’.
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
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.
151 152 153 |
# File 'app/controllers/anoubis/etc/field.rb', line 151 def initialize_boolean () end |
#initialize_datetime(options) ⇒ Object
Initialize additional parameters for ‘datetime’ field type for controller actions.
193 194 195 196 197 |
# File 'app/controllers/anoubis/etc/field.rb', line 193 def initialize_datetime () [:format] = 'datetime' if !.key? :format [:format] = 'datetime' if !%w[date datetime full year month].include? [:format] self.format = [:format] end |
#initialize_hash(options) ⇒ Object
Initialize additional parameters for ‘hash’ field type for controller actions.
158 159 160 |
# File 'app/controllers/anoubis/etc/field.rb', line 158 def initialize_hash () end |
#initialize_html(options) ⇒ Object
Initialize additional parameters for ‘html’ field type for controller actions.
186 187 188 |
# File 'app/controllers/anoubis/etc/field.rb', line 186 def initialize_html () end |
#initialize_key(options) ⇒ Object
Initialize additional parameters for ‘key’ field type for controller actions.
232 233 234 235 236 237 238 239 |
# File 'app/controllers/anoubis/etc/field.rb', line 232 def initialize_key () if self.model self.error_text = '' if [:action] == 'new' self.field = Kernel.format('%s.%s', self.key, self.model.title) if !.key? :field self.table_field = Kernel.format('%s.%s', self.model.model.table_name, self.model.title) if !self.table_field self.autocomplete = {} if !.key? :autocomplete end end |
#initialize_listbox(options) ⇒ Object
Setups additional parameters for ‘listbox’ field type for controller actions.
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 () self. = FieldOptions.new if !self. if !self..list if self.model if !(%w[update create].include?([:action])) self..show = 'update' if self..line self..list = self..line else self..list = {} end self.model.model.select(self.model.select).where(self.model.where).order(self.model.order).each do |dat| self..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 [:format] = 'single' unless .key? :format [:format] = 'single' unless %w[single multiple].include? [:format] self.format = [:format] end |
#initialize_number(options) ⇒ Object
Initialize additional parameters for ‘number’ field type for controller actions.
165 166 167 168 169 170 171 172 173 174 |
# File 'app/controllers/anoubis/etc/field.rb', line 165 def initialize_number () if .key? :error_text self.error_text = [:error_text] else self.error_text = '' end self.precision = [:precision].to_s.to_i if .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.
144 145 146 |
# File 'app/controllers/anoubis/etc/field.rb', line 144 def initialize_string () end |
#initialize_text(options) ⇒ Object
Initialize additional parameters for ‘text’ field type for controller actions.
179 180 181 |
# File 'app/controllers/anoubis/etc/field.rb', line 179 def initialize_text () end |
#precision ⇒ String
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.
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
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’
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’
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.(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’.
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’
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.(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’
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..list) if self. result end |
#properties_forms_number(model, action, mod) ⇒ Hash
Return field properties for frontend application for actions ‘edit’, ‘new’ and type ‘number’
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..key? :greater_than_or_equal_to result[:min] = res.[:greater_than_or_equal_to] errors[:min] = model.human_attribute_name(self.key) + ' ' + mod.errors.(self.key.to_sym, :greater_than_or_equal_to, { count: result[:min] }) end if res..key? :maximum result[:max] = res.[:maximum] errors[:max] = model.human_attribute_name(self.key) + ' ' + mod.errors.(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.(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’
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..key? :minimum result[:min] = res.[:minimum] errors[:min] = model.human_attribute_name(self.key) + ' ' + mod.errors.(self.key.to_sym, :too_short, { count: result[:min] }) end if res..key? :maximum result[:max] = res.[:maximum] errors[:max] = model.human_attribute_name(self.key) + ' ' + mod.errors.(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.(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’.
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’
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..list) if self. result end |
#to_h ⇒ Hash
Generates hash representation of all class parameters,
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. result[:options] = self..to_h end if self.order result[:order] = self.order.to_h end result end |