lib/wcc/contentful/test/factory.rb in wcc-contentful-0.4.0.pre.rc vs lib/wcc/contentful/test/factory.rb in wcc-contentful-1.0.0.pre.rc1
- old
+ new
@@ -5,79 +5,55 @@
module WCC::Contentful::Test::Factory
##
# Builds a in-memory instance of the Contentful model for the given content_type.
# All attributes that are known to be required fields on the content type
# will return a default value based on the field type.
- def contentful_create(content_type, **attrs)
+ def contentful_create(content_type, context = nil, **attrs)
const = WCC::Contentful::Model.resolve_constant(content_type.to_s)
attrs = attrs.transform_keys { |a| a.to_s.camelize(:lower) }
id = attrs.delete('id')
+ sys = attrs.delete('sys')
+ raw = attrs.delete('raw') || default_raw(const, id)
bad_attrs = attrs.reject { |a| const.content_type_definition.fields.key?(a) }
raise ArgumentError, "Attribute(s) do not exist on #{const}: #{bad_attrs.keys}" if bad_attrs.any?
- default_instance(const, id).tap do |instance|
- attrs.each do |k, v|
- field = const.content_type_definition.fields[k]
+ raw['sys'].merge!(sys) if sys
- raw = v
- if %i[Asset Link].include?(field.type)
- raw = to_raw(v, field.type)
+ attrs.each do |k, v|
+ field = const.content_type_definition.fields[k]
- unless field.array ? v.any? { |i| i.is_a?(String) } : v.is_a?(String)
- instance.instance_variable_set("@#{field.name}_resolved", v)
- end
- end
-
- instance.raw['fields'][field.name][instance.sys.locale] = raw
- instance.instance_variable_set("@#{field.name}", raw)
- end
-
- def instance.to_s
- "#<#{self.class.name} id=\"#{id}\">"
- end
+ raw_value = v
+ raw_value = to_raw(v, field.type) if %i[Asset Link].include?(field.type)
+ raw['fields'][field.name][raw.dig('sys', 'locale')] = raw_value
end
- end
- class Link
- attr_reader :id
- attr_reader :link_type
- attr_reader :raw
+ instance = const.new(raw, context)
- LINK_TYPES = {
- Asset: 'Asset',
- Link: 'Entry'
- }.freeze
+ attrs.each do |k, v|
+ field = const.content_type_definition.fields[k]
+ next unless %i[Asset Link].include?(field.type)
- def initialize(model, link_type = nil)
- @id = model.try(:id) || model
- @link_type = link_type
- @link_type ||= model.is_a?(WCC::Contentful::Model::Asset) ? :Asset : :Link
- @raw =
- {
- 'sys' => {
- 'type' => 'Link',
- 'linkType' => LINK_TYPES[@link_type],
- 'id' => @id
- }
- }
+ unless field.array ? v.any? { |i| i.is_a?(String) } : v.is_a?(String)
+ instance.instance_variable_set("@#{field.name}_resolved", v)
+ end
end
- alias_method :to_h, :raw
+ instance
end
private
def default_instance(model, id = nil)
model.new(default_raw(model, id))
end
def default_raw(model, id = nil)
- { sys: sys(model, id), fields: fields(model) }.as_json
+ { sys: contentful_sys(model, id), fields: contentful_fields(model) }.as_json
end
- def sys(model, id = nil)
+ def contentful_sys(model, id = nil)
{
space: {
sys: {
type: 'Link',
linkType: 'Space',
@@ -105,20 +81,20 @@
},
locale: 'en-US'
}
end
- def fields(model)
+ def contentful_fields(model)
WCC::Contentful::Test::Attributes.defaults(model).each_with_object({}) do |(k, v), h|
h[k] = { 'en-US' => v }
end
end
def to_raw(val, field_type)
if val.is_a? Array
val.map { |i| to_raw(i, field_type) }
elsif val.is_a? String
- Link.new(val, field_type).raw
+ WCC::Contentful::Link.new(val, field_type).raw
elsif val
val.raw
end
end
end