Module: Formable

Extended by:
ActiveSupport::Concern
Defined in:
lib/app/models/concerns/formable.rb

Overview

For models that are updated via forms, we need an easy and consistent way to return a list of allowed parameters for a given model. While doing Model.attribute_names is working for most forms, it DOES NOT deal with relationships at all.

This mix is should be included in those models, and the model then calls Model.allowed_param_names to retrieve a complete (dynamic) list of

  • Attribute names
  • Many to many relationships (has_many_and_belongs_to)
  • One to many relationships (belongs to)

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Include the class methods into the model



19
20
21
22
23
# File 'lib/app/models/concerns/formable.rb', line 19

def self.included(base)
  base.class_eval do
    base.extend ClassMethods
  end
end

Instance Method Details

#remove_blank_secure_fields(params) ⇒ Object



107
108
109
110
# File 'lib/app/models/concerns/formable.rb', line 107

def remove_blank_secure_fields(params)
  secure_fields.each { |field| params.delete(field) if params[field].blank? }
  params
end

#secure_fields ⇒ Object

List of secure fields, to add fields in concrete class, simply override this method



103
104
105
# File 'lib/app/models/concerns/formable.rb', line 103

def secure_fields
  []
end

#update(params) ⇒ Object Also known as: update_attributes

Remove updates for secure fields that come across as blank to start with and get removed on update



85
86
87
# File 'lib/app/models/concerns/formable.rb', line 85

def update(params)
  super(remove_blank_secure_fields(params))
end

#update!(params) ⇒ Object Also known as: update_attributes!

Remove updates for secure fields that come across as blank to start with and get removed on update



94
95
96
# File 'lib/app/models/concerns/formable.rb', line 94

def update!(params)
  super(remove_blank_secure_fields(params))
end