Class: Katapult::Attribute
Constant Summary
collapse
- UnknownTypeError =
Class.new(StandardError)
- MissingOptionError =
Class.new(StandardError)
- TYPES =
%i[string email password url integer money text flag datetime json
plain_json foreign_key]
Constants inherited
from Element
Element::UnknownFormattingError, Element::UnknownOptionError
Instance Attribute Summary collapse
Attributes inherited from Element
#application_model, #name, #options
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Attribute
Returns a new instance of Attribute.
19
20
21
22
23
24
25
26
27
|
# File 'lib/katapult/elements/attribute.rb', line 19
def initialize(*args)
super
self.type ||= :email if name.to_s =~ /email/
self.type ||= :password if name.to_s =~ /password/
self.type ||= :string
validate!
end
|
Instance Attribute Details
#associated_model ⇒ Object
Returns the value of attribute associated_model.
12
13
14
|
# File 'lib/katapult/elements/attribute.rb', line 12
def associated_model
@associated_model
end
|
#model ⇒ Object
Returns the value of attribute model.
12
13
14
|
# File 'lib/katapult/elements/attribute.rb', line 12
def model
@model
end
|
Instance Method Details
#assignable_values_as_list? ⇒ Boolean
86
87
88
|
# File 'lib/katapult/elements/attribute.rb', line 86
def assignable_values_as_list?
assignable_values.try(:to_a).present?
end
|
#editable? ⇒ Boolean
39
40
41
|
# File 'lib/katapult/elements/attribute.rb', line 39
def editable?
%i[plain_json json].exclude? type
end
|
#for_migration ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/katapult/elements/attribute.rb', line 51
def for_migration
db_type = case type
when :email, :url, :password then 'string'
when :flag then 'boolean'
when :money then 'decimal{10,2}' when :json then 'jsonb' when :plain_json then 'json' when :foreign_key then 'integer'
else type end
"#{name}:#{db_type}"
end
|
#has_defaults? ⇒ Boolean
31
32
33
|
# File 'lib/katapult/elements/attribute.rb', line 31
def has_defaults?
!default.nil? and not [flag?, assignable_values].any?
end
|
#renderable? ⇒ Boolean
35
36
37
|
# File 'lib/katapult/elements/attribute.rb', line 35
def renderable?
%i[plain_json json password].exclude? type
end
|
#required? ⇒ Boolean
43
44
45
46
47
48
49
|
# File 'lib/katapult/elements/attribute.rb', line 43
def required?
if assignable_values.present?
default.blank? && allow_blank.blank?
else
false
end
end
|
#test_value ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/katapult/elements/attribute.rb', line 64
def test_value
if type == :foreign_key
associated_model.label_attr.test_value
elsif assignable_values
assignable_values.first
else
case type
when :string then "#{name}-string"
when :password then "#{name}-password"
when :email then "#{name}@example.com"
when :url then "#{name}.example.com"
when :text then "#{name}-text"
when :integer then Zlib.crc32(name).modulo(1000)
when :money then Zlib.crc32(name).modulo(1000) / 100.0
when :datetime then Time.at(Zlib.crc32(name))
end
end
end
|