Module: HydraEditor::Form::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#build_permitted_paramsObject



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

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
65
66
67
68
69
70
# File 'app/forms/hydra_editor/form.rb', line 58

def model_attributes(form_params)
  sanitize_params(form_params).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)


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

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

#permitted_paramsObject



76
77
78
# File 'app/forms/hydra_editor/form.rb', line 76

def permitted_params
  @permitted ||= build_permitted_params
end

#sanitize_params(form_params) ⇒ Object



72
73
74
# File 'app/forms/hydra_editor/form.rb', line 72

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