Class: Valkyrie::ChangeSet
- Inherits:
-
Reform::Form
- Object
- Reform::Form
- Valkyrie::ChangeSet
- 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.
Class Method Summary collapse
-
.fields=(fields) ⇒ Object
Quick setter for fields that should be in a changeset.
-
.reflect_on_association(*_args) ⇒ Object
Override reflect_on_association so SimpleForm can work.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns value for a given property.
-
#append_id=(append_id) ⇒ Object
Set ID of record this one should be appended to.
-
#multiple?(field_name) ⇒ Boolean
Returns whether or not a given field has multiple values.
-
#prepopulate!(_options = {}) ⇒ Object
Prepopulates all fields with defaults defined in the changeset.
-
#required?(field_name) ⇒ Boolean
Returns whether or not a given field is required.
- #resource ⇒ Object
- #valid? ⇒ Boolean
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.
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.
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.
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
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!( = {}) 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
52 53 54 |
# File 'lib/valkyrie/change_set.rb', line 52 def required?(field_name) field(field_name)[:required] == true end |
#resource ⇒ Object
98 99 100 |
# File 'lib/valkyrie/change_set.rb', line 98 def resource model end |
#valid? ⇒ Boolean
102 103 104 105 |
# File 'lib/valkyrie/change_set.rb', line 102 def valid? errors.clear super end |