Module: HydraEditor::Form::ClassMethods

Defined in:
app/forms/hydra_editor/form.rb

Instance Method Summary collapse

Instance Method Details

#build_permitted_paramsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/forms/hydra_editor/form.rb', line 78

def build_permitted_params
  permitted = []
  terms.each do |term|
    if multiple?(term)
      permitted << { term => [] }
    else
      permitted << term
    end
  end
  permitted
end

#model_attributes(form_params) ⇒ Object

Return a hash of all the parameters from the form as a hash. This is typically used by the controller as the main read interface to the form. This hash can then be used to create or update an object in the data store. example:

ImageForm.model_attributes(params[:image])
# => { title: 'My new image' }


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/forms/hydra_editor/form.rb', line 56

def model_attributes(form_params)
  sanitize_params(form_params).to_h.tap do |clean_params|
    terms.each do |key|
      if clean_params[key]
        if multiple?(key)
          clean_params[key].delete('')
        elsif clean_params[key] == ''
          clean_params[key] = nil
        end
      end
    end
  end
end

#multiple?(field) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/forms/hydra_editor/form.rb', line 46

def multiple?(field)
  .multiple?(model_class, field)
end

#permitted_paramsObject



74
75
76
# File 'app/forms/hydra_editor/form.rb', line 74

def permitted_params
  @permitted ||= build_permitted_params
end

#sanitize_params(form_params) ⇒ Object



70
71
72
# File 'app/forms/hydra_editor/form.rb', line 70

def sanitize_params(form_params)
  form_params.permit(*permitted_params)
end

#validators_on(*attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'app/forms/hydra_editor/form.rb', line 36

def validators_on(*attributes)
  attributes.flat_map do |attribute|
    if required_fields.include?(attribute)
      [Validator.new(attributes: [attribute])]
    else
      []
    end
  end
end