Class: Hyrax::Forms::ResourceForm

Inherits:
ChangeSet
  • Object
show all
Includes:
BasedNearFieldBehavior
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

Methods included from BasedNearFieldBehavior

#deserialize, included

Constructor Details

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

Forms should be initialized with an explicit resource: parameter to match indexers. rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/forms/hyrax/forms/resource_form.rb', line 52

def initialize(deprecated_resource = nil, resource: nil)
  r = resource || deprecated_resource
  if r.flexible?
    self.class.deserializer_class = nil # need to reload this on first use after schema is loaded
    singleton_class.schema_definitions = self.class.definitions
    context = r.respond_to?(:context) ? r.context : nil
    Hyrax::Schema.m3_schema_loader.form_definitions_for(schema: r.class.name, version: Hyrax::FlexibleSchema.current_schema_id, contexts: context).map do |field_name, options|
      singleton_class.property field_name.to_sym, options.merge(display: options.fetch(:display, true), default: [])
    end
  end

  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
    # make a new resource with all of the existing attributes
    if resource.flexible?
      hash = resource.attributes.dup
      hash[:schema_version] = Hyrax::FlexibleSchema.current_schema_id
      resource = resource.class.new(hash)
      # find any fields removed by the new schema
      to_remove = singleton_class.definitions.select { |k, v| !resource.respond_to?(k) && v.instance_variable_get("@options")[:display] }
      to_remove.keys.each do |removed_field|
        singleton_class.definitions.delete(removed_field)
      end
    end

    super(resource)
  end
end

Class Method Details

.check_if_flexible(model) ⇒ Object



95
96
97
98
99
# File 'app/forms/hyrax/forms/resource_form.rb', line 95

def check_if_flexible(model)
  return unless model.flexible?
  include FlexibleFormBehavior
  include Hyrax::FormFields(model.to_s, definition_loader: Hyrax::Schema.m3_schema_loader)
end

.expose_classObject



153
154
155
# File 'app/forms/hyrax/forms/resource_form.rb', line 153

def expose_class
  @expose_class = Class.new(Disposable::Expose).from(schema_definitions.values)
end

.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)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/forms/hyrax/forms/resource_form.rb', line 109

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

.inherited(subclass) ⇒ Object



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

def inherited(subclass)
  # this is a noop if based near is not defined on a given model
  # we need these to be before and included properties
  subclass.prepend(BasedNearFieldBehavior)
  super
end

.required_fieldsArray<Symbol>

Returns list of required field names as symbols.

Returns:

  • (Array<Symbol>)

    list of required field names as symbols



126
127
128
129
130
# File 'app/forms/hyrax/forms/resource_form.rb', line 126

def required_fields
  schema_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)


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

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

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

  required_fields
end

.schema_definitionsObject



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

def schema_definitions
  @definitions
end

.schema_definitions=(values) ⇒ Object



149
150
151
# File 'app/forms/hyrax/forms/resource_form.rb', line 149

def schema_definitions=(values)
  @definitions = values
end

Instance Method Details

#[]=(attr, value) ⇒ Object

Returns the set value.

Parameters:

  • attr (#to_s)
  • value (Object)

Returns:

  • (Object)

    the set value



162
163
164
# File 'app/forms/hyrax/forms/resource_form.rb', line 162

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’



196
197
198
# File 'app/forms/hyrax/forms/resource_form.rb', line 196

def display_additional_fields?
  secondary_terms.any?
end

#model_classClass

Deprecated.

use model.class instead

Returns:

  • (Class)


170
171
172
# File 'app/forms/hyrax/forms/resource_form.rb', line 170

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



177
178
179
180
181
182
183
184
# File 'app/forms/hyrax/forms/resource_form.rb', line 177

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

  terms = [:schema_version, :contexts] + terms if model.flexible?
  terms
end

#secondary_termsArray<Symbol>

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

Returns:

  • (Array<Symbol>)

    terms for display ‘below-the-fold’



188
189
190
191
192
# File 'app/forms/hyrax/forms/resource_form.rb', line 188

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