Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Last active December 26, 2015 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyf/7086692 to your computer and use it in GitHub Desktop.
Save jeremyf/7086692 to your computer and use it in GitHub Desktop.
class Work < ActiveFedora::Base
  has_metadata "descMetadata", type: ArticleMetadataDatastream

  section :required_information, label: "Required Information" do |section|
    section.attribute :title, datastream: :descMetadata, required: true
  end

  section :secondary_information, label: "Secondary Information" do |section|
    section.attribute :contributor, datastream: :descMetadata
  end

  attribute :publisher, datastream: :descMetadata, section: :secondary_information
end
class ArticleMetadataDatastream < ActiveFedora::NtriplesRDFDatastream
  map_predicates do |map|
    map.title(in: RDF::DC) do |index|
      index.as :stored_searchable
    end
    map.contributor(in: RDF::DC) do |index|
      index.as :stored_searchable
    end
  end
end
class WorksController
  def new
    @work = WorkRenderer.new(Work.new, self)
  end
end

View for rendering a work's input form

# Render form for work, partial
<%= form_for(work) do |form| %>
  <% work.each_renderable_section do |section| %>
    <%= section.render(form) %>
  <% end %>
<% end %>
# Render form for attributes in a section
<% section.each_attribute do |attribute| %>
  <%= attribute.render(form) %>
<% end %>
class WorkRenderer

  attr_reader :work, :template_name
  delegate :sections, to: :work
  def initialize(work, template, section_renderer = SectionRenderer)
    @work = work
    @template = template
  end

  def each_renderable_section
    sections.each do |section|
      yield(section_renderer.new(section, template))
    end
  end

end


class SectionRenderer

  delegate :work, :attributes, to: :section
  def initialize(section, template)
    @section = section
    @template = template
  end

  def render(form)
    template.render template_name, section: self, form: form
  end

  def each_renderable_attribute
    attributes.each do |attribute|
      yield(AttributeRenderer.new(attribute, template))
    end
  end

end

class AttributeRenderer
  delegate :work, to: :section

  def initializer(section, attribute_name, template)
    @section = section
    @attribute_name = attribute_name
    @template = template
  end

  def render
    # Ideally this should be looked up in the hierachy for rendering:
    # /app/views/works/instance/_template_name
    # /app/views/works/base/_template_name
    template.render template_name, attribute: self, form: form
  end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment