Class: AbAdmin::Models::TypeModel

Inherits:
Object
  • Object
show all
Includes:
EnumField::DefineEnum
Defined in:
lib/ab_admin/models/type_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ TypeModel

Returns a new instance of TypeModel


12
13
14
# File 'lib/ab_admin/models/type_model.rb', line 12

def initialize(code)
  @code = code.to_sym
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code


6
7
8
# File 'lib/ab_admin/models/type_model.rb', line 6

def code
  @code
end

Class Method Details

.define_enum_by_codesObject


17
18
19
20
21
22
23
24
# File 'lib/ab_admin/models/type_model.rb', line 17

def define_enum_by_codes
  define_enum do |builder|
    codes.each do |kind|
      builder.member kind, object: new(kind.to_s)
    end
  end
  define_question_methods
end

.define_question_methodsObject


26
27
28
29
30
31
32
# File 'lib/ab_admin/models/type_model.rb', line 26

def define_question_methods
  codes.each do |code_check|
    define_method "#{code_check}?" do
      self.code == code_check
    end
  end
end

.legal?(value) ⇒ Boolean

Returns:

  • (Boolean)

34
35
36
37
# File 'lib/ab_admin/models/type_model.rb', line 34

def legal?(value)
  ActiveSupport::Deprecation.warn('legal? is deprecated, use valid? instead')
  valid?(value)
end

.valid?(c_id) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/ab_admin/models/type_model.rb', line 39

def valid?(c_id)
  all.map(&:id).include?(c_id.to_i)
end

.valid_code?(code) ⇒ Boolean

Returns:

  • (Boolean)

43
44
45
46
# File 'lib/ab_admin/models/type_model.rb', line 43

def valid_code?(code)
  return unless code
  codes.include?(code.to_sym)
end

Instance Method Details

#titleObject


49
50
51
52
53
# File 'lib/ab_admin/models/type_model.rb', line 49

def title
  I18n.t!(@code, scope: i18n_scope)
rescue I18n::MissingTranslationData
  @code.to_s.humanize
end