Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created June 6, 2018 15:50
Show Gist options
  • Save elrayle/d5f18ff6e741a839241351123dbb25ec to your computer and use it in GitHub Desktop.
Save elrayle/d5f18ff6e741a839241351123dbb25ec to your computer and use it in GitHub Desktop.
prepending Hyrax::Forms::CollectionForm example
# Based on the Module#prepend pattern in ruby which is used in some Hyrax.
# Uses the to_prepare Rails hook in application.rb to inject this module to override Hyrax::Forms::CollectionForm module
module PrependedForms::CollectionForm
module ClassMethods
# Override Hyrax::Forms::CollectionForm.terms method
# self.terms = [:parent_agency, :title, :acronym, :description, :contact_email, :contact_phone,
# :location_city, :location_state, :homepage_url, :is_division]
def terms
[:title, :acronym, :description, :contact_email, :contact_phone,
:location_city, :location_state, :homepage_url, :thumbnail_id]
end
# Override Hyrax::Forms::CollectionForm.multiple? method
def multiple?(field)
if [:title, :description].include? field.to_sym
false
else
super
end
end
# Override Hyrax::Forms::CollectionForm.sanitize_params method
def sanitize_params(_)
attrs = super
attrs[:title] = Array(attrs[:title]) if attrs[:title]
attrs[:description] = Array(attrs[:description]) if attrs[:description]
attrs
end
end
def self.prepended(base)
class << base
prepend ClassMethods
end
end
delegate :creator, :acronym, :contact_email, :contact_phone, :location_city, :location_state, :homepage_url, to: :model
# Override Hyrax::Forms::CollectionForm#primary_terms method
# Terms that appear within the accordion. Show all terms without an additional metadata button.
def primary_terms
if CollectionTypeService.organization? collection
[:title,
:acronym,
:description,
:contact_email,
:contact_phone,
:location_city,
:location_state,
:homepage_url]
else
[:title,
:description]
end
end
# Override Hyrax::Forms::CollectionForm#secondary_terms method
# Terms that appear within the accordion. All metadata is considered primary. No additional metadata.
def secondary_terms
[]
end
# Override Hyrax::Forms::CollectionForm#multiple? method
def multiple?(field)
if [:title, :description].include? field.to_sym
false
else
super
end
end
# Override Hyrax::Forms::CollectionForm#title method
def title
super.first || ""
end
# Override Hyrax::Forms::CollectionForm#description method
def description
super.first || ""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment