Class: Hyrax::Forms::ResourceForm

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/forms/hyrax/forms/resource_form.rb', line 66

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
    contexts = r.respond_to?(:contexts) ? r.contexts : nil
    current_schema_fields = Hyrax::Schema.m3_schema_loader.form_definitions_for(schema: r.class.name, version: Hyrax::FlexibleSchema.current_schema_id, contexts: contexts)
    current_schema_fields.each 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."
      # Remove form definitions for attributes the model doesn't support,
      # mirroring the cleanup in the resource: keyword path below.
      if deprecated_resource.respond_to?(:flexible?) && deprecated_resource.flexible?
        to_remove = singleton_class.definitions.select { |k, v| !deprecated_resource.respond_to?(k) && v.instance_variable_get("@options")[:display] }
        to_remove.keys.each { |removed_field| singleton_class.definitions.delete(removed_field) }
      end
      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 current schema
      current_field_keys = current_schema_fields.keys.map(&:to_s)
      to_remove = singleton_class.definitions.select { |k, v| !current_field_keys.include?(k.to_s) && 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



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

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

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

Factory for generic, per-work froms

Examples:

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


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/forms/hyrax/forms/resource_form.rb', line 136

def for(deprecated_resource = nil, resource: nil, admin_set_id: 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, admin_set_id: admin_set_id)
  end
  apply_admin_set_contexts(resource: resource, admin_set_id: admin_set_id)
  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



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

def inherited(subclass)
  # Field Behaviors must be prepended onto every subclass so their
  # `deserialize!` overrides land above Reform's base method on the
  # ancestor chain. Each behavior gates itself internally and is a
  # no-op when its feature is off or its property isn't on the
  # subclass's model.
  subclass.prepend(BasedNearFieldBehavior)
  subclass.prepend(RedirectsFieldBehavior)
  super
end

Instance Method Details

#[]=(attr, value) ⇒ Object

Returns the set value.

Parameters:

  • attr (#to_s)
  • value (Object)

Returns:

  • (Object)

    the set value



205
206
207
# File 'app/forms/hyrax/forms/resource_form.rb', line 205

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'



239
240
241
# File 'app/forms/hyrax/forms/resource_form.rb', line 239

def display_additional_fields?
  secondary_terms.any?
end

#model_classClass

Deprecated.

use model.class instead

Returns:

  • (Class)


213
214
215
# File 'app/forms/hyrax/forms/resource_form.rb', line 213

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



220
221
222
223
224
225
226
227
# File 'app/forms/hyrax/forms/resource_form.rb', line 220

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'



231
232
233
234
235
# File 'app/forms/hyrax/forms/resource_form.rb', line 231

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