Module: Hyrax::AttributesHelper

Included in:
HyraxHelperBehavior
Defined in:
app/helpers/hyrax/attributes_helper.rb

Instance Method Summary collapse

Instance Method Details

#conform_field(field_name, options_hash) ⇒ String

Returns the field name to be used for rendering.

Parameters:

  • field (String)

    name

  • a (Hash<Hash>)

    nested hash of view options… :label=>{“en”=>“Title”, “es”=>“Título”, :html_dl=>true}

Returns:

  • (String)

    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, options_hash)
  options_hash&.with_indifferent_access&.fetch('render_term', nil) || field_name
end

#conform_options(field_name, view_options) ⇒ Hash

Returns the transformed options for the field.

Parameters:

  • field (String)

    name

  • a (Hash<Hash>)

    nested hash of view options… “es”=>“Título”, :html_dl=>true}

Returns:

  • (Hash)

    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
# File 'app/helpers/hyrax/attributes_helper.rb', line 70

def conform_options(field_name, view_options)
  hash_of_locales = view_options.delete(:display_label) || {}

  if hash_of_locales.present?
    view_options[:label] = hash_of_locales[locale] || hash_of_locales[:default]
  else
    view_options[:label] = (view_options[:render_term] || field_name).to_s
  end
  field = view_options[:label]
  translate = I18n.t(field, default: field) if field.match(/\./)
  if translate && translate != field
    view_options[:label] = translate
  else
    view_options[:label] = field_label(
      :"blacklight.search.fields.index.#{field}",
      :"blacklight.search.fields.show.#{field}",
      :"blacklight.search.fields.#{field}",
      field.to_s.humanize
    )
  end
  view_options
end

#schema(model) ⇒ Hash

Returns the schema for the model or nil if not found.

Parameters:

  • model's (String)

    class name

Returns:

  • (Hash)

    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 view_options_for(presenter)
  model_name = presenter.model.model_name.name
  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
    view_options_without_flexibility(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 view_options_without_flexibility(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