Module: Hyrax::AttributesHelper
- Included in:
- HyraxHelperBehavior
- Defined in:
- app/helpers/hyrax/attributes_helper.rb
Instance Method Summary collapse
-
#conform_field(field_name, options_hash) ⇒ String
The field name to be used for rendering.
-
#conform_options(field_name, view_options) ⇒ Hash
The transformed options for the field.
-
#field_visible?(view_options, presenter) ⇒ Boolean
Returns true when the field should render on the work show page.
-
#schema(model) ⇒ Hash
The schema for the model or nil if not found.
- #view_options_for(presenter) ⇒ Object
- #view_options_without_flexibility(presenter, model_name) ⇒ Object
Instance Method Details
#conform_field(field_name, options_hash) ⇒ String
Returns the field name to be used for rendering.
62 63 64 |
# File 'app/helpers/hyrax/attributes_helper.rb', line 62 def conform_field(field_name, ) &.with_indifferent_access&.fetch('render_term', nil) || field_name end |
#conform_options(field_name, view_options) ⇒ Hash
Returns the transformed options for the field.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/helpers/hyrax/attributes_helper.rb', line 70 def (field_name, ) hash_of_locales = .delete(:display_label) || {} if hash_of_locales.present? [:label] = hash_of_locales[locale] || hash_of_locales[:default] else [:label] = ([:render_term] || field_name).to_s end field = [:label] translate = I18n.t(field, default: field) if field.match(/\./) if translate && translate != field [:label] = translate else [:label] = field_label( :"blacklight.search.fields.index.#{field}", :"blacklight.search.fields.show.#{field}", :"blacklight.search.fields.#{field}", field.to_s.humanize ) end [:base_url] = request.base_url if respond_to?(:request) && request.respond_to?(:base_url) end |
#field_visible?(view_options, presenter) ⇒ Boolean
Returns true when the field should render on the work show page. Honors these view options:
* `show_page: false` — suppresses the field on the show page for everyone.
* `admin_only: true` — only visible to admins.
* `editor_only: true` — only visible to users the presenter reports as editors.
Setting both admin_only and editor_only is effectively equivalent to
admin_only alone: admins are editors of every record, so they pass both
guards, but a plain editor of the record still fails the admin guard.
102 103 104 105 106 107 |
# File 'app/helpers/hyrax/attributes_helper.rb', line 102 def field_visible?(, presenter) return false if [:show_page] == false return false if [:admin_only] && !current_user&.admin? return false if [:editor_only] && !presenter.try(:editor?) true end |
#schema(model) ⇒ Hash
Returns the schema for the model or nil if not found.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/helpers/hyrax/attributes_helper.rb', line 44 def schema(model) return nil unless model.present? # If this is an ActiveFedoraDummyModel, get the schema from the actual model class if model.is_a?(Hyrax::ActiveFedoraDummyModel) actual_model = model.instance_variable_get(:@model) return schema(actual_model) if actual_model end # using respond_to? check because try? does not succeed with Dry::Types object that is returned by schema method return nil unless model.respond_to?(:schema) model.schema end |
#view_options_for(presenter) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'app/helpers/hyrax/attributes_helper.rb', line 5 def (presenter) model_name = presenter.model.model_name.klass.to_s if presenter.respond_to?(:flexible?) && presenter.flexible? Hyrax::Schema.m3_schema_loader.view_definitions_for(schema: model_name, version: presenter.solr_document.schema_version, contexts: presenter.solr_document.contexts) else (presenter, model_name) end end |
#view_options_without_flexibility(presenter, model_name) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/helpers/hyrax/attributes_helper.rb', line 14 def (presenter, model_name) model = presenter.model schema = schema(model) # no schema found for given model - try a few more fallbacks # if wings is enabled we may be able to use the model registry to find the new model if schema.nil? && !Hyrax.config.disable_wings new_model = Wings::ModelRegistry.reverse_lookup(model) schema = schema(new_model) if new_model end # Check if the model has a resource version if schema.nil? new_model = "#{model_name}Resource".safe_constantize schema = schema(new_model) if new_model end # If we still don't have a schema, assume we have a pre-valkyrie model if schema.nil? if model.respond_to?(:local_attributes) return model.local_attributes else return [] end end Hyrax::Schema.simple_schema_loader.view_definitions_for(schema:) end |