Class: Valkyrie::ChangeSet

Inherits:
Reform::Form
  • Object
show all
Includes:
Reform::Form::ActiveModel, Reform::Form::ActiveModel::FormBuilderMethods, Reform::Form::ActiveModel::Validations, Reform::Form::ModelReflections, Reform::Form::ORM
Defined in:
lib/valkyrie/change_set.rb

Overview

Standard change set object for Valkyrie. ChangeSets are a way to group together properties that should be applied to an underlying resource. They are often used for powering HTML Forms or storing virtual attributes for special synchronization with a resource.

Examples:

Define a change set

class BookChangeSet < Valkyrie::ChangeSet
  self.fields = [:title, :author]
  validates :title, presence: true
  property :title, multiple: false, required: true
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fields=(fields) ⇒ Object

Quick setter for fields that should be in a changeset. Defaults to multiple, not required, with an empty array default.

Parameters:

  • fields (Array<Symbol>)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/valkyrie/change_set.rb', line 59

def self.fields=(fields)
  singleton_class.class_eval do
    remove_possible_method(:fields)
    define_method(:fields) { fields }
  end

  fields.each do |field|
    property field, default: []
  end
  fields
end

.reflect_on_association(*_args) ⇒ Object

Override reflect_on_association so SimpleForm can work.



72
# File 'lib/valkyrie/change_set.rb', line 72

def self.reflect_on_association(*_args); end

Instance Method Details

#[](key) ⇒ Object

Returns value for a given property.

Parameters:

  • key (Symbol)


76
77
78
# File 'lib/valkyrie/change_set.rb', line 76

def [](key)
  send(key) if respond_to?(key)
end

#append_id=(append_id) ⇒ Object

Set ID of record this one should be appended to. We use append_id to add a member/child onto an existing list of members.

Parameters:



35
36
37
# File 'lib/valkyrie/change_set.rb', line 35

def append_id=(append_id)
  super(Valkyrie::ID.new(append_id))
end

#multiple?(field_name) ⇒ Boolean

Returns whether or not a given field has multiple values. Multiple values are useful for fields like creator, author, title, etc. where there may be more than one value for a field that is stored and returned in the UI

Parameters:

  • field_name (Symbol)

Returns:

  • (Boolean)


44
45
46
# File 'lib/valkyrie/change_set.rb', line 44

def multiple?(field_name)
  field(field_name)[:multiple] != false
end

#prepopulate!(_options = {}) ⇒ Object

Prepopulates all fields with defaults defined in the changeset. This is an override of Reform::Form’s method to allow for single-valued fields to prepopulate appropriately.



89
90
91
92
93
94
95
96
# File 'lib/valkyrie/change_set.rb', line 89

def prepopulate!(_options = {})
  self.class.definitions.select { |_field, definition| definition[:multiple] == false }.each_key do |field|
    value = Array.wrap(send(field.to_s)).first
    send("#{field}=", value)
  end
  super
  self
end

#required?(field_name) ⇒ Boolean

Returns whether or not a given field is required. Useful for distinguishing required fields in a form and for validation

Parameters:

  • field_name (Symbol)

Returns:

  • (Boolean)


52
53
54
# File 'lib/valkyrie/change_set.rb', line 52

def required?(field_name)
  field(field_name)[:required] == true
end

#resourceObject



98
99
100
# File 'lib/valkyrie/change_set.rb', line 98

def resource
  model
end

#valid?Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/valkyrie/change_set.rb', line 102

def valid?
  errors.clear
  super
end