Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polymorfic associations #61

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/active_model/validations/store_model_validator.rb
Expand Up @@ -21,9 +21,9 @@ def validate_each(record, attribute, value)
end

case record.type_for_attribute(attribute).type
when :json
when :json, :polymorphic
call_json_strategy(attribute, record.errors, value)
when :array
when :array, :polymorphic_array
call_array_strategy(attribute, record.errors, value)
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/store_model.rb
Expand Up @@ -10,5 +10,10 @@ class << self
def config
@config ||= Configuration.new
end

# @return instance [Types::OneOf]
def one_of(&block)
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved
Types::OneOf.new(&block)
end
end
end
4 changes: 4 additions & 0 deletions lib/store_model/types.rb
Expand Up @@ -3,10 +3,14 @@
require "store_model/types/json_type"
require "store_model/types/array_type"
require "store_model/types/enum_type"
require "store_model/types/polymorphic_type"
require "store_model/types/polymorphic_array_type"
require "store_model/types/one_of"

module StoreModel
# Contains all custom types.
module Types
class CastError < StandardError; end
class ExpandWrapperError < StandardError; end
end
end
23 changes: 23 additions & 0 deletions lib/store_model/types/one_of.rb
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "active_model"

module StoreModel
module Types
# Implements ActiveModel::Type::Value type for handling an array of
# StoreModel::Model
class OneOf
def initialize(&block)
@block = block
end

def to_type
Types::PolymorphicType.new(@block)
end

def to_array_type
Types::PolymorphicArrayType.new(@block)
end
end
end
end
103 changes: 103 additions & 0 deletions lib/store_model/types/polymorphic_array_type.rb
@@ -0,0 +1,103 @@
# frozen_string_literal: true

require "active_model"

module StoreModel
module Types
# Implements ActiveModel::Type::Value type for handling an array of
# StoreModel::Model
class PolymorphicArrayType < ActiveModel::Type::Value
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved
# Initializes type for model class
#
# @param model_wrapper [Proc] class to handle
#
# @return [StoreModel::Types::PolymorphicArrayType ]
def initialize(model_wrapper)
@model_wrapper = model_wrapper
end

# Returns type
#
# @return [Symbol]
def type
:polymorphic_array
end

# Casts +value+ from DB or user to StoreModel::Model instance
#
# @param value [Object] a value to cast
#
# @return StoreModel::Model
def cast_value(value)
case value
when String then decode_and_initialize(value)
when Array then ensure_model_class(value)
when nil then value
else
raise_cast_error(value)
end
end

# Casts a value from the ruby type to a type that the database knows how
# to understand.
#
# @param value [Object] value to serialize
#
# @return [String] serialized value
def serialize(value)
case value
when Array
ActiveSupport::JSON.encode(value)
else
super
end
end

# Determines whether the mutable value has been modified since it was read
#
# @param raw_old_value [Object] old value
# @param new_value [Object] new value
#
# @return [Boolean]
def changed_in_place?(raw_old_value, new_value)
cast_value(raw_old_value) != new_value
end

private

# rubocop:disable Style/RescueModifier
def decode_and_initialize(array_value)
decoded = ActiveSupport::JSON.decode(array_value) rescue []
decoded.map { |attributes| cast_model_type_value(attributes) }
end
# rubocop:enable Style/RescueModifier

def ensure_model_class(array)
array.map do |object|
next object if object.class.ancestors.include?(StoreModel::Model)

cast_model_type_value(object)
end
end

def cast_model_type_value(value)
model_klass = @model_wrapper.call(value)

raise_expand_wrapper_error(model_klass) unless model_klass&.respond_to?(:to_type)
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved

model_klass.to_type.cast_value(value)
end

def raise_cast_error(value)
raise StoreModel::Types::CastError,
"failed casting #{value.inspect}, only String, " \
"Hash or instances which implement StoreModel::Model are allowed"
end

def raise_expand_wrapper_error(invalid_klass)
raise StoreModel::Types::ExpandWrapperError,
"#{invalid_klass.inspect} is an invalid model klass"
end
end
end
end
120 changes: 120 additions & 0 deletions lib/store_model/types/polymorphic_type.rb
@@ -0,0 +1,120 @@
# frozen_string_literal: true

require "active_model"

module StoreModel
module Types
# Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
class PolymorphicType < ActiveModel::Type::Value
# Initializes type for model class
#
# @param model_wrapper [Proc] class to handle
#
# @return [StoreModel::Types::PolymorphicType ]
def initialize(model_wrapper)
@model_wrapper = model_wrapper
end

# Returns type
#
# @return [Symbol]
def type
:polymorphic
end

# Casts +value+ from DB or user to StoreModel::Model instance
#
# @param value [Object] a value to cast
#
# @return StoreModel::Model
def cast_value(value)
case value
when String then decode_and_initialize(value)
when Hash
model_klass = extract_model_klass(value)
model_klass.new(value)
when nil then value
else
raise_cast_error(value) unless value.class.ancestors.include?(StoreModel::Model)

value
end
rescue ActiveModel::UnknownAttributeError => e
handle_unknown_attribute(value, e)
end

# Casts a value from the ruby type to a type that the database knows how
# to understand.
#
# @param value [Object] value to serialize
#
# @return [String] serialized value
def serialize(value)
case value
when Hash
ActiveSupport::JSON.encode(value)
else
return ActiveSupport::JSON.encode(value) if value.class.ancestors.include?(StoreModel::Model)

super
end
end

# Determines whether the mutable value has been modified since it was read
#
# @param raw_old_value [Object] old value
# @param new_value [Object] new value
#
# @return [Boolean]
def changed_in_place?(raw_old_value, new_value)
cast_value(raw_old_value) != new_value
end

private

# rubocop:disable Style/RescueModifier
def decode_and_initialize(value)
decoded = ActiveSupport::JSON.decode(value) rescue nil
model_klass = extract_model_klass(value)
model_klass.new(decoded) unless decoded.nil?
rescue ActiveModel::UnknownAttributeError => e
handle_unknown_attribute(decoded, e)
end
# rubocop:enable Style/RescueModifier

# Check if block returns an appropriate class and raise cast error if not
#
# @param value [Object] raw data
#
# @return [Class] which implements StoreModel::Model
def extract_model_klass(value)
model_klass = @model_wrapper.call(value)

model_klass&.ancestors&.include?(StoreModel::Model) ||
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved
raise_expand_wrapper_error(model_klass)

model_klass
end

def raise_cast_error(value)
raise StoreModel::Types::CastError,
"failed casting #{value.inspect}, only String, " \
"Hash or instances which implement StoreModel::Model are allowed"
end

def raise_expand_wrapper_error(invalid_klass)
raise StoreModel::Types::ExpandWrapperError,
"#{invalid_klass.inspect} is an invalid model klass"
end

def handle_unknown_attribute(value, exception)
attribute = exception.attribute.to_sym
value_symbolized = value.symbolize_keys

cast_value(value_symbolized.except(attribute)).tap do |configuration|
configuration.unknown_attributes[attribute.to_s] = value_symbolized[attribute]
end
end
end
end
end