Class: Hyrax::Forms::ResourceForm

Inherits:
ChangeSet
  • Object
show all
Defined in:
app/forms/hyrax/forms/resource_form.rb

Overview

This form wraps Hyrax::ChangeSet in the HydraEditor::Form interface.

Constant Summary collapse

LockKeyPrepopulator =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Note:

includes special handling for Wings, to support compatibility with ‘etag`-driven, application-side lock checks. for non-wings adapters we want to move away from application side lock validation and rely on the adapter/database features instead.

proc do |_options|
  if Hyrax.config.disable_wings || !Hyrax..is_a?(Wings::Valkyrie::MetadataAdapter)
    Hyrax.logger.info "trying to prepopulate a lock token for " \
                      "#{self.class.inspect}, but optimistic locking isn't " \
                      "supported for the configured adapter: #{Hyrax..class}"
    self.version = ''
  else
    self.version =
      model.persisted? ? Wings::ActiveFedoraConverter.convert(resource: model).etag : ''
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deprecated_resource = nil, resource: nil) ⇒ ResourceForm

Forms should be initialized with an explicit resource: parameter to match indexers.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/forms/hyrax/forms/resource_form.rb', line 50

def initialize(deprecated_resource = nil, resource: nil)
  if resource.nil?
    if !deprecated_resource.nil?
      Deprecation.warn "Initializing Valkyrie forms without an explicit resource parameter is deprecated. Pass the resource with `resource:` instead."
      super(deprecated_resource)
    else
      super()
    end
  else
    super(resource)
  end
end

Class Method Details

.for(deprecated_resource = nil, resource: nil) ⇒ Object

Factory for generic, per-work froms

Examples:

monograph  = Monograph.new
change_set = Hyrax::Forms::ResourceForm.for(resource: monograph)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/forms/hyrax/forms/resource_form.rb', line 72

def for(deprecated_resource = nil, resource: nil)
  if resource.nil? && !deprecated_resource.nil?
    Deprecation.warn "Initializing Valkyrie forms without an explicit resource parameter is deprecated. Pass the resource with `resource:` instead."
    return self.for(resource: deprecated_resource)
  end
  klass = "#{resource.class.name}Form".safe_constantize
  klass ||= Hyrax::Forms::ResourceForm(resource.class)
  begin
    klass.new(resource: resource)
  rescue ArgumentError
    Deprecation.warn "Initializing Valkyrie forms without an explicit resource parameter is deprecated. #{klass} should be updated accordingly."
    klass.new(resource)
  end
end

.required_fieldsArray<Symbol>

Returns list of required field names as symbols.

Returns:

  • (Array<Symbol>)

    list of required field names as symbols



89
90
91
92
93
# File 'app/forms/hyrax/forms/resource_form.rb', line 89

def required_fields
  definitions
    .select { |_, definition| definition[:required] }
    .keys.map(&:to_sym)
end

.required_fields=(fields) ⇒ Array<Symbol>

Returns list of required field names as symbols.

Parameters:

  • fields (Enumerable<#to_s>)

Returns:

  • (Array<Symbol>)

    list of required field names as symbols

Raises:

  • (KeyError)


99
100
101
102
103
104
105
106
# File 'app/forms/hyrax/forms/resource_form.rb', line 99

def required_fields=(fields)
  fields = fields.map(&:to_s)
  raise(KeyError) unless fields.all? { |f| definitions.key?(f) }

  fields.each { |field| definitions[field].merge!(required: true) }

  required_fields
end

Instance Method Details

#[]=(attr, value) ⇒ Object

Returns the set value.

Parameters:

  • attr (#to_s)
  • value (Object)

Returns:

  • (Object)

    the set value



114
115
116
# File 'app/forms/hyrax/forms/resource_form.rb', line 114

def []=(attr, value)
  public_send("#{attr}=".to_sym, value)
end

#display_additional_fields?Boolean

Returns whether there are terms to display ‘below-the-fold’.

Returns:

  • (Boolean)

    whether there are terms to display ‘below-the-fold’



145
146
147
# File 'app/forms/hyrax/forms/resource_form.rb', line 145

def display_additional_fields?
  secondary_terms.any?
end

#model_classClass

Deprecated.

use model.class instead

Returns:

  • (Class)


122
123
124
# File 'app/forms/hyrax/forms/resource_form.rb', line 122

def model_class # rubocop:disable Rails/Delegate
  model.class
end

#primary_termsArray<Symbol>

Returns terms for display ‘above-the-fold’, or in the most prominent form real estate.

Returns:

  • (Array<Symbol>)

    terms for display ‘above-the-fold’, or in the most prominent form real estate



129
130
131
132
133
# File 'app/forms/hyrax/forms/resource_form.rb', line 129

def primary_terms
  _form_field_definitions
    .select { |_, definition| definition[:primary] }
    .keys.map(&:to_sym)
end

#secondary_termsArray<Symbol>

Returns terms for display ‘below-the-fold’.

Returns:

  • (Array<Symbol>)

    terms for display ‘below-the-fold’



137
138
139
140
141
# File 'app/forms/hyrax/forms/resource_form.rb', line 137

def secondary_terms
  _form_field_definitions
    .select { |_, definition| definition[:display] && !definition[:primary] }
    .keys.map(&:to_sym)
end