lib/roar/representer/feature/model_representing.rb in roar-0.8.0 vs lib/roar/representer/feature/model_representing.rb in roar-0.8.1

- old
+ new

@@ -37,50 +37,42 @@ end # ClassMethods # Properties that are mapped to a model attribute. class ModelDefinition < ::Representable::Definition def compute_attribute_for(represented, attributes) - value = represented.send(accessor) + value = represented.send(getter) if typed? value = apply(value) do |v| sought_type.for_model(v) # applied to each typed attribute (even in collections). end end - attributes[accessor] = value + attributes[name] = value end end end module ActiveRecordMethods - def to_nested_attributes # FIXME: works on first level only, doesn't check if we really need to suffix _attributes and is horriby implemented. just for protoyping. - attrs = {} - - to_attributes.each do |k,v| - - - - #next if k.to_s == "links" # FIXME: how to skip virtual attributes that are not mapped in a model? - clear_attributes(attrs) - - attrs[k] = v - if v.is_a?(Hash) or v.is_a?(Array) - attrs["#{k}_attributes"] = attrs.delete(k) + def to_nested_attributes # FIXME: extract iterating with #to_attributes. + {}.tap do |attributes| + self.class.representable_attrs.each do |definition| + next unless definition.kind_of?(ModelRepresenting::ModelDefinition) + + value = public_send(definition.getter) + + if definition.typed? + value = definition.apply(value) do |v| + v.to_nested_attributes # applied to each typed attribute (even in collections). + end + end + + key = definition.name + key = "#{key}_attributes" if definition.typed? + + attributes[key] = value end - end - - attrs - end - - private - def clear_attributes(attrs) - puts "clearing #{attrs.inspect}" - attrs.each do |k,v| - attrs.delete(k) if k == "links" - - clear_attributes(v) if v.is_a?(Hash) end end end end end