Skip to content

Instantly share code, notes, and snippets.

@mankind
Last active August 17, 2018 14:57
Show Gist options
  • Save mankind/e3c39b8026d91243b1659e2408fc5c9e to your computer and use it in GitHub Desktop.
Save mankind/e3c39b8026d91243b1659e2408fc5c9e to your computer and use it in GitHub Desktop.
samvera multi-part field
<%=
f.input :contributor_group, as: :contributor_group_type,
wrapper_html: { class: 'multi_value' },
input_html: { class: 'form-control', multiple: true },
include_blank: true,
required: f.object.required?(key)
%>
class ContributorGroupService < Hyrax::QaSelectService
def initialize(_authority_name = nil)
super('contributor_group')
end
end
#
#Credits Chris Colvard of Avalon Media System
# fir guidance and code samples
# --- END LICENSE_HEADER BLOCK ---
class ContributorGroupTypeInput < MultiValueInput
# Override to include contributor_group_type class
def inner_wrapper
<<-HTML
<li class="field-wrapper contributor_group_type">
#{yield}
</li>
HTML
end
def common_field_options
options = {}
options[:class] ||= []
options[:class] += ["#{input_dom_id} form-control multi-text-field multi_value"]
options[:'aria-labelledby'] = label_id
options
end
def select_input_html_options
common_field_options.dup.merge(
#name: "#{@builder.object_name}[contributor_group][][contributor_list]",
name: "#{@builder.object_name}[contributor_list][]",
#id: "#{@builder.object_name}_contributor_group_contributor_list",
id: nil,
required: nil
)
end
def text_area_input_html_options(value)
puts "text area value is #{value}"
common_field_options.dup.merge(
#name: "#{@builder.object_name}[contributor_group][][contributor_name]",
name: "#{@builder.object_name}[contributor_name][]",
id: nil,
required: nil,
placeholder: 'Add contributor name',
value: value
)
end
def text_input_html_options(value)
puts "text input value is #{value}"
common_field_options.merge(
#name: "#{@builder.object_name}[contributor_group][][contributor_id]",
name: "#{@builder.object_name}[contributor_id][]",
id: nil,
required: nil,
placeholder: 'Add contributor orcid',
value: value
)
end
def build_field(value, _index)
@rendered_first_element = true
contributor_list_choices = ContributorGroupService.new.select_active_options
output = @builder.select(:contributor_list, contributor_list_choices, { selected: value[0] }, select_input_html_options)
output += @builder.text_area(:contributor_name, text_area_input_html_options(value[1]))
output += @builder.text_field(:contributor_id, text_input_html_options(value[2]))
output
end
end
class JournalArticle < ActiveFedora::Base
include ::Hyrax::WorkBehavior
self.indexer = JournalArticleIndexer
validates :title, presence: { message: 'Your work must have a title.' }
self.human_readable_type = 'Journal Article'
property :contributor_group, predicate: ::RDF::Vocab::EBUCore.Type do |index|
index.as :stored_searchable
end
before_save :save_contributor_group
def save_contributor_group
submitted_values = [{"contributor_name": self.contributor_given_name, "contributor_id": self.contributor_orcid,
"contributor_type": self.contributor_type}]
submitted_values_json = [submitted_values.to_json]
self.contributor_group = submitted_values_json
end
# This must be included at the end, because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include ::Hyrax::BasicMetadata
end
module Hyrax
class JournalArticleForm < Hyrax::Forms::WorkForm
include Hyrax::FormTerms
self.model_class = ::JournalArticle
self.terms += %i[contributor_group]
self.required_fields += %i[journal_title institution publisher date_published]
self.required_fields -= %i[keyword rights_statement]
attr_accessor :contributor_name, :contributor_list, :contributor_id
def contributor_group
# Parse json from contributor_group field
return [] if contributor_group.blank?
JSON.parse(contributor_group).map { |action| [action.name, action_label(action)] }
end
def contributor_list
contributor_list = contributor_group_types["contributor_list"]
@contributor_list = contributor_list
end
def self.permitted_params
super.tap do |permitted_params|
permitted_params << { contributor_name: []}
permitted_params << {contributor_list: []}
permitted_params << {contributor_id: []}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment