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 4 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
8 changes: 8 additions & 0 deletions lib/store_model.rb
Expand Up @@ -10,5 +10,13 @@ class << self
def config
@config ||= Configuration.new
end

# TODO: add documentation
def one_of(&block)
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved
Class.new do
define_singleton_method(:to_type) { Types::PolymorphicType.new(block) }
define_singleton_method(:to_array_type) { Types::PolymorphicArrayType.new(block) }
end
end
end
end
2 changes: 2 additions & 0 deletions lib/store_model/types.rb
Expand Up @@ -3,6 +3,8 @@
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"

module StoreModel
# Contains all custom types.
Expand Down
87 changes: 87 additions & 0 deletions lib/store_model/types/polymorphic_array_type.rb
@@ -0,0 +1,87 @@
# 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 StoreModel::Types::CastError,
"failed casting #{value.inspect}, only String or Array instances are allowed"
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|
object.class.ancestors.include?(StoreModel::Model) ? object : cast_model_type_value(object)
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved
end
end

def cast_model_type_value(value)
@model_wrapper.call(value).to_type.cast_value(value)
end
end
end
end
98 changes: 98 additions & 0 deletions lib/store_model/types/polymorphic_type.rb
@@ -0,0 +1,98 @@
# 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 then @model_wrapper.call(value).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_wrapper.call(value).new(decoded) unless decoded.nil?
HolyWalley marked this conversation as resolved.
Show resolved Hide resolved
rescue ActiveModel::UnknownAttributeError => e
handle_unknown_attribute(decoded, e)
end
# rubocop:enable Style/RescueModifier

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 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