module Poppy::ActiveRecord::ClassMethods
Public Instance Methods
enumeration(attribute_name, **options)
click to toggle source
# File lib/poppy/active_record.rb, line 23 def enumeration(attribute_name, **options) options.assert_valid_keys(:as, :of) column_type = options.fetch(:as, DEFAULT_COLUMN_TYPE) enumeration = options[:of] define_type(attribute_name: attribute_name, column_type: column_type, enumeration: enumeration) add_validation(attribute_name: attribute_name, column_type: column_type, enumeration: enumeration) end
Private Instance Methods
add_validation(attribute_name:, column_type:, enumeration:)
click to toggle source
# File lib/poppy/active_record.rb, line 37 def add_validation(attribute_name:, column_type:, enumeration:) case column_type when :value validates attribute_name, inclusion: { in: enumeration.list } when :array validates attribute_name, enum_array: { as: enumeration } end end
array_type_for_enum(enumeration)
click to toggle source
# File lib/poppy/active_record.rb, line 59 def array_type_for_enum(enumeration) raise InvalidColumnTypeForAdapterTypeError unless postgres_connection? PostgreSQLArrayType.new(EnumType.new(enum: enumeration)) end
define_type(attribute_name:, column_type:, enumeration:)
click to toggle source
# File lib/poppy/active_record.rb, line 33 def define_type(attribute_name:, column_type:, enumeration:) attribute attribute_name, type_for(column_type: column_type, enumeration: enumeration) end
postgres_connection?()
click to toggle source
# File lib/poppy/active_record.rb, line 64 def postgres_connection? ::ActiveRecord::Base.connection.is_a?(PostgreSQLAdapter) end
type_for(column_type:, enumeration:)
click to toggle source
# File lib/poppy/active_record.rb, line 46 def type_for(column_type:, enumeration:) case column_type when :value value_type_for_enum(enumeration) when :array array_type_for_enum(enumeration) end end
value_type_for_enum(enumeration)
click to toggle source
# File lib/poppy/active_record.rb, line 55 def value_type_for_enum(enumeration) EnumType.new(enum: enumeration) end