Class: Anoubis::Etc::FieldOptions

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

Overview

Definitions of fields options list for ‘checkbox’ and ‘listbox’ type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FieldOptions

Sets default parameters for field options

Parameters:

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

    initial model options

Options Hash (options):

  • :show (String)

    describes options shoe type

  • :list (Hash<Symbol, String>)

    options list



47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/anoubis/etc/field_options.rb', line 47

def initialize(options = {})
  if options.key? :show
    self.show = options[:show] if %w[init never always update].include? options[:show]
  end
  self.show = 'init' if !self.show || self.show == 'init'
  self.line = if options.key? :line then options[:line] else nil end
  self.list = if options.key? :list then options[:list] else nil end
  self.enum = if options.key? :enum then options[:enum] else nil end
  self.model = if options.key? :model then Model.new(options[:model]) else nil end
  self.generate_list if !self.list && self.model
end

Instance Attribute Details

#enumHash<Symbol, Sring>

Enum list from ActiveRecord. Is defined for correct filtering.

Returns:

  • (Hash<Symbol, Sring>)

    enum options list



23
# File 'app/controllers/anoubis/etc/field_options.rb', line 23

class_attribute :enum, default: nil

#lineHash

Defines selected line. Default nil

Returns:

  • (Hash)

    presence of select line



28
# File 'app/controllers/anoubis/etc/field_options.rb', line 28

class_attribute :line, default: nil

#listHash<Symbol, Sring>

Options list.

Returns:

  • (Hash<Symbol, Sring>)

    options list



18
# File 'app/controllers/anoubis/etc/field_options.rb', line 18

class_attribute :list, 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)

  • :select (Symbol) – special select statement (defaults to: nil)

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

Returns:

  • (Model)

    model’s description for complex field



40
# File 'app/controllers/anoubis/etc/field_options.rb', line 40

class_attribute :model, default: nil

#showString

Describes when options shown in output for ‘edit’ and ‘new’ actions. Possible values of fields are:

  • ‘init’ – Options shown only when time set to 0 in action

  • ‘never’ – Options newer shown

  • ‘always’ – Options always shown

  • ‘update’ – Options shown when time less then updated_at time of options model

Returns:

  • (String)

    options’ show type.



13
# File 'app/controllers/anoubis/etc/field_options.rb', line 13

class_attribute :show, default: 'init'

Instance Method Details

#generate_listObject

Generate options list based on model



61
62
63
64
65
66
67
# File 'app/controllers/anoubis/etc/field_options.rb', line 61

def generate_list
  self.list = {}
  self.model.model.where(self.model.where).order(self.model.order).each do |data|
    proc = format('%s', self.model.title)
    self.list[data.id.to_s.to_sym] = data.send proc
  end
end

#to_hHash

Generates hash representation of all class parameters,

Returns:

  • (Hash)

    hash representation of all data



72
73
74
75
76
77
78
# File 'app/controllers/anoubis/etc/field_options.rb', line 72

def to_h
  {
      show: self.show,
      enum: self.enum,
      list: self.list
  }
end