# encoding: UTF-8 module Spontaneous::Collections class FieldSet < PrototypeSet attr_reader :owner def initialize(owner, initial_values) super() @owner = owner @field_data = initial_values initialize_from_prototypes end def serialize_db self.map { |field| field.serialize_db } end def export(user) owner.class.field_names. select { |name| owner.field_writable?(user, name) }. map { |name| self[name].export(user) } end def saved self.each { |field| field.mark_unmodified } end # Lazily load fields by name def named(name) super || load_field(owner.field_prototypes[name.to_sym]) end # A call to ${ fields } within a template will call # this #render method. # This should only be used during development # def render(format = :html, locals = {}, parent_context = nil) map { |field| wrap_field_value(field, field.render(format, locals), format) }.join("\n") end def render_using(renderer, format = :html, locals = {}, parent_context = nil) map { |field| wrap_field_value(field, field.render_using(renderer, format, locals), format) }.join("\n") end protected def wrap_field_value(field, value, format) case format when "html", :html classes = [owner.class.to_s.dasherize.downcase, "field", field.name].join(" ") id = "field-#{field.id.gsub(/\//, "-")}" [ %(