Created
June 6, 2018 15:50
-
-
Save elrayle/d5f18ff6e741a839241351123dbb25ec to your computer and use it in GitHub Desktop.
prepending Hyrax::Forms::CollectionForm example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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