class Katapult::Attribute
Constants
- MissingOptionError
- TYPES
- UnknownTypeError
Attributes
associated_model[RW]
model[RW]
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
Katapult::Element::new
# 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
Public Instance Methods
assignable_values_as_list?()
click to toggle source
# File lib/katapult/elements/attribute.rb, line 86 def assignable_values_as_list? assignable_values.try(:to_a).present? end
editable?()
click to toggle source
# File lib/katapult/elements/attribute.rb, line 39 def editable? %i[plain_json json].exclude? type end
for_migration()
click to toggle source
# 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}' # {precision,scale} = total digits, decimal places when :json then 'jsonb' # Indexable JSON when :plain_json then 'json' # Only use this if you need to when :foreign_key then 'integer' else type end "#{name}:#{db_type}" end
has_defaults?()
click to toggle source
# File lib/katapult/elements/attribute.rb, line 31 def has_defaults? !default.nil? and not [flag?, assignable_values].any? end
renderable?()
click to toggle source
# File lib/katapult/elements/attribute.rb, line 35 def renderable? %i[plain_json json password].exclude? type end
required?()
click to toggle source
# 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()
click to toggle source
# 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" # Deterministically generate a value from the attribute's name 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
Private Instance Methods
type_inquiry()
click to toggle source
# File lib/katapult/elements/attribute.rb, line 92 def type_inquiry @type.to_s.inquiry end
validate!()
click to toggle source
# File lib/katapult/elements/attribute.rb, line 96 def validate! TYPES.include?(type) or raise UnknownTypeError, "Attribute type :#{type} is not supported. Use one of #{TYPES.inspect}." if flag? and default.nil? raise MissingOptionError, "The :flag attribute '#{name}' requires a default (true or false)." end end