Forms and Presenters and Indexers! Oh my!
Indexing (The old way)
class Image < ActiveFedora::Base
def to_solr(solr_doc=Hash.new)
super.tap do |solr_doc|
solr_doc['field1_ssi'] = some_value if some_condition?
end
end
end
Indexer (Model)
class Image < ActiveFedora::Base
def self.indexer
ImageIndexer
end
end
Indexer (Indexer)
class ImageIndexer < ActiveFedora::IndexingService
def generate_solr_document
super do |solr_doc|
solr_doc['field1_ssi'] = some_value if some_condition?
solr_doc['field2_ssi'] = other_value if other_condition?
end
end
end
Presenter
Presenter
class ArticlePresenter
include Hydra::Presenter
delegate :title, to: :model
def byline
"#{model.created_at.strftime('%m/%d/%Y')} - by #{author_name}"
end
def author_name
"#{model.author.first_name} #{model.author.last_name}"
end
end
# app/controllers/article_controller.rb
article = Article.find(params[:id])
@presenter = ArticlePresenter.new(article)
<% # app/views/articles/show.html.erb %>
<%= link_to @presenter.title, @presenter %>
<%= @presenter.byline %>
# app/controllers/article_controller.rb
_, document_list = search_results(params,
CatalogController.search_params_logic)
@presenter = ArticlePresenter.new(document_list.first)
<% # app/views/articles/show.html.erb %>
<%= link_to @presenter.title, @presenter %>
<%= @presenter.byline %>
Indexer + Presenter = BFF
https://flic.kr/p/9YW6go - https://creativecommons.org/licenses/by-nc-nd/2.0/
class AudioForm
include HydraEditor::Form
self.model_class = Audio
self.terms = [:title, :creator, :description, :subject, :isPartOf]
self.required_fields = [:title, :creator]
end
audio = Audio.find(param[:id])
@form = AudioForm.new(audio)
As an update helper
AudioForm.model_attributes(params[:audio])
# => { title: 'My new image' }
Why?
- MVC - Works fine for small apps. Bigger apps need more organization
- MVC - Works best with a single data store. Hydra has two, Fedora and Solr.
- Provides a pathway to customization
- Smaller classes, single responsibility
- Faster tests because we're isolating code better