Sha256: 4e75c37762b59b0282fd0e97789cfc097299adc82b5086d58416f099434f55ff

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Hyrax
  ##
  # @api private
  #
  # Build a changeset class for the given resource class. The ChangeSet will
  # have fields to match the resource class given.
  #
  # To define a custom changeset with validations, use naming convention with "ChangeSet" appended to the end
  # of the resource class name. (e.g. for BookResource, name the change set BookResourceChangeSet)
  #
  # @example
  #   Hyrax::ChangeSet(Monograph)
  def self.ChangeSet(resource_class)
    klass = (resource_class.to_s + "ChangeSet").safe_constantize || Hyrax::ChangeSet
    Class.new(klass) do
      (resource_class.fields - resource_class.reserved_attributes).each do |field|
        property field, default: nil
      end

      ##
      # @return [String]
      define_singleton_method :inspect do
        return "Hyrax::ChangeSet(#{resource_class})" if name.blank?
        super
      end
    end
  end

  class ChangeSet < Valkyrie::ChangeSet
    ##
    # @api public
    #
    # Factory for resource ChangeSets
    #
    # @example
    #   monograph  = Monograph.new
    #   change_set = Hyrax::ChangeSet.for(monograph)
    #
    #   change_set.title = 'comet in moominland'
    #   change_set.sync
    #   monograph.title # => 'comet in moominland'
    #
    def self.for(resource)
      Hyrax::ChangeSet(resource.class).new(resource)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hyrax-5.0.3 app/models/hyrax/change_set.rb
hyrax-5.0.2 app/models/hyrax/change_set.rb
hyrax-5.0.1 app/models/hyrax/change_set.rb
hyrax-5.0.0 app/models/hyrax/change_set.rb
hyrax-5.0.0.rc3 app/models/hyrax/change_set.rb