lib/compony/model_fields/association.rb in compony-0.0.9 vs lib/compony/model_fields/association.rb in compony-0.1.0
- old
+ new
@@ -22,31 +22,24 @@
end
end
def schema_line
local_schema_key = @schema_key # Capture schema_key as it will not be available within the lambda
- target_primary_key_type = @target_class.primary_key_type_key
if multi?
return proc do
ary? local_schema_key do
- if target_primary_key_type == :integer
- list :integer, cast_str: true
- elsif target_primary_key_type == :string
- list :string
- else
- fail("Unsupported target primary_key_type_key #{target_primary_key_type}")
+ list :any_of do
+ int cast_str: true
+ str
end
end
end
else
return proc do
- if target_primary_key_type == :integer
- int? local_schema_key, cast_str: true
- elsif target_primary_key_type == :string
- str? local_schema_key
- else
- fail("Unsupported target primary_key_type_key #{target_primary_key_type}")
+ any_of? local_schema_key do
+ str
+ int cast_str: true
end
end
end
end
@@ -64,14 +57,11 @@
# This can be auto-inferred without accessing the database.
def resolve_association!
@association = true
association_info = @model_class.reflect_on_association(@name) || fail("Association #{@name.inspect} does not exist for #{@model_class.inspect}.")
@multi = association_info.macro == :has_many
- @target_class = association_info.klass
id_name = "#{@name.to_s.singularize}_id"
@schema_key = @multi ? id_name.pluralize.to_sym : id_name.to_sym
- rescue ActiveRecord::NoDatabaseError
- Rails.logger.warn('Warning: Compony could not auto-detect fields due to missing database. This is ok when running db:create.')
end
end
end
end