Class: Anubis::Etc::Field
- Inherits:
-
Object
- Object
- Anubis::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_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
457 458 459 460 461 462 463 |
# File 'app/controllers/anoubis/etc/field.rb', line 457 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.
186 187 188 189 190 |
# File 'app/controllers/anoubis/etc/field.rb', line 186 def initialize_datetime () [:format] = 'datetime' if !.key? :format [:format] = 'datetime' if !%w[date datetime full year month].include? [:format] self.format = [:format] end |
#initialize_html(options) ⇒ Object
Initialize additional parameters for 'html' field type for controller actions.
179 180 181 |
# File 'app/controllers/anoubis/etc/field.rb', line 179 def initialize_html () end |
#initialize_key(options) ⇒ Object
Initialize additional parameters for 'key' field type for controller actions.
225 226 227 228 229 230 231 232 |
# File 'app/controllers/anoubis/etc/field.rb', line 225 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.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'app/controllers/anoubis/etc/field.rb', line 195 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.
158 159 160 161 162 163 164 165 166 167 |
# File 'app/controllers/anoubis/etc/field.rb', line 158 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.
172 173 174 |
# File 'app/controllers/anoubis/etc/field.rb', line 172 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
239 240 241 242 243 244 |
# File 'app/controllers/anoubis/etc/field.rb', line 239 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'
277 278 279 280 281 282 283 284 285 286 |
# File 'app/controllers/anoubis/etc/field.rb', line 277 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'
408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'app/controllers/anoubis/etc/field.rb', line 408 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'.
362 363 364 |
# File 'app/controllers/anoubis/etc/field.rb', line 362 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'
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'app/controllers/anoubis/etc/field.rb', line 385 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'
372 373 374 375 376 377 |
# File 'app/controllers/anoubis/etc/field.rb', line 372 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'
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'app/controllers/anoubis/etc/field.rb', line 323 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'
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'app/controllers/anoubis/etc/field.rb', line 294 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'.
352 353 354 |
# File 'app/controllers/anoubis/etc/field.rb', line 352 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'
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'app/controllers/anoubis/etc/field.rb', line 251 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,
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'app/controllers/anoubis/etc/field.rb', line 424 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 |