class Para::AttributeFieldMappings
Constants
- UNEDITABLE_ATTRIBUTES
Attributes
Public Class Methods
Source
# File lib/para/attribute_field_mappings.rb, line 12 def initialize(model, whitelist_attributes: nil, mappings: nil) @model = model @whitelist_attributes = whitelist_attributes @mappings = mappings || {} process_fields! end
The mappings hash is a form provided hash of attributes and input types mapping that are automatically generated in the form builder and that allows our model field parsers to spot unregistered fields and process them when needed.
Public Instance Methods
Source
# File lib/para/attribute_field_mappings.rb, line 24 def field_for(field_name, type = nil) existing_field = fields_hash[field_name] if !existing_field || (type && !existing_field.type?(type)) fields_hash[field_name] = if model.new.respond_to?(field_name) build_field_for(field_name, type) else raise NoMethodError.new( "No attribute or method correspond to ##{ field_name } " + "in the model #{ model.name }. No field could be created." ) end else existing_field end end
Source
# File lib/para/attribute_field_mappings.rb, line 20 def fields fields_hash.values end
Private Instance Methods
Source
# File lib/para/attribute_field_mappings.rb, line 64 def build_field_for(attribute_name, type) field_class = field_class_for(type) field_class.new(model, name: attribute_name, type: type) end
Source
# File lib/para/attribute_field_mappings.rb, line 69 def field_class_for(type) attribute_class = type && AttributeField::Base.field_types[type.to_sym] attribute_class || AttributeField::Base end
Source
# File lib/para/attribute_field_mappings.rb, line 47 def process_fields! @fields_hash = model.columns.each_with_object({}) do |column, fields| next unless whitelisted?(column.name) # Reject uneditable attributes unless UNEDITABLE_ATTRIBUTES.include?(column.name) field_class = field_class_for(column.type) fields[column.name] = field_class.new( model, name: column.name, type: column.type ) end end.with_indifferent_access Para::ModelFieldParsers.parse!(model, fields_hash, mappings) end
Source
# File lib/para/attribute_field_mappings.rb, line 43 def whitelisted?(attribute_name) !whitelist_attributes || whitelist_attributes.include?(attribute_name) end