Sha256: bddfe6e30ef5c84b3c6a5ef7754ce923f17bd7e2351012f2f41b22295b032c52

Contents?: true

Size: 1.32 KB

Versions: 21

Compression:

Stored size: 1.32 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]
      def self.inspect
        return "Hyrax::ChangeSet(#{model_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

21 entries across 21 versions & 1 rubygems

Version Path
hyrax-5.0.0.rc2 app/models/hyrax/change_set.rb
hyrax-5.0.0.rc1 app/models/hyrax/change_set.rb
hyrax-3.6.0 app/models/hyrax/change_set.rb
hyrax-4.0.0 app/models/hyrax/change_set.rb
hyrax-4.0.0.rc3 app/models/hyrax/change_set.rb
hyrax-4.0.0.rc2 app/models/hyrax/change_set.rb
hyrax-4.0.0.rc1 app/models/hyrax/change_set.rb
hyrax-3.5.0 app/models/hyrax/change_set.rb
hyrax-4.0.0.beta2 app/models/hyrax/change_set.rb
hyrax-3.4.2 app/models/hyrax/change_set.rb
hyrax-4.0.0.beta1 app/models/hyrax/change_set.rb
hyrax-3.4.1 app/models/hyrax/change_set.rb
hyrax-3.4.0 app/models/hyrax/change_set.rb
hyrax-3.3.0 app/models/hyrax/change_set.rb
hyrax-3.2.0 app/models/hyrax/change_set.rb
hyrax-3.1.0 app/models/hyrax/change_set.rb
hyrax-3.0.2 app/models/hyrax/change_set.rb
hyrax-3.0.1 app/models/hyrax/change_set.rb
hyrax-3.0.0 app/models/hyrax/change_set.rb
hyrax-3.0.0.pre.rc4 app/models/hyrax/change_set.rb