Module: HydraEditor::Form::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#build_permitted_paramsObject



74
75
76
77
78
79
80
81
82
83
84
# File 'app/forms/hydra_editor/form.rb', line 74

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' }


58
59
60
61
62
63
64
# File 'app/forms/hydra_editor/form.rb', line 58

def model_attributes(form_params)
  clean_params = sanitize_params(form_params)
  terms.each do |key|
    clean_params[key].delete('') if clean_params[key]
  end
  clean_params
end

#multiple?(field) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/forms/hydra_editor/form.rb', line 48

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

#permitted_paramsObject



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

def permitted_params
  @permitted ||= build_permitted_params
end

#sanitize_params(form_params) ⇒ Object



66
67
68
# File 'app/forms/hydra_editor/form.rb', line 66

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

#validators_on(*attributes) ⇒ Object



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

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