Module: Para::ModelHelper

Included in:
Admin::ResourcesController, Inputs::MultiSelectInput
Defined in:
app/helpers/para/model_helper.rb

Instance Method Summary collapse

Instance Method Details

#attribute_field_mappings_for(component, relation) ⇒ Object



3
4
5
6
# File 'app/helpers/para/model_helper.rb', line 3

def attribute_field_mappings_for(component, relation)
  model = relation_klass_for(component, relation)
  model_field_mappings(model).fields
end

#excerpt_value_for(value) ⇒ Object



48
49
50
51
52
53
54
55
# File 'app/helpers/para/model_helper.rb', line 48

def excerpt_value_for(value)
  return value unless value.is_a?(String)

  truncate(
    sanitize(value, tags: []),
    length: 100
  )
end

#field_for(model, field_name, type = nil) ⇒ Object



28
29
30
# File 'app/helpers/para/model_helper.rb', line 28

def field_for(model, field_name, type = nil)
  model_field_mappings(model).field_for(field_name, type)
end

#field_value_for(object, field_name, type = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/para/model_helper.rb', line 37

def field_value_for(object, field_name, type = nil)
  field = field_for(object.class, field_name, type)
  value = field.value_for(object)

  if field.excerptable_value?
    excerpt_value_for(value)
  else
    value
  end
end

#model_field_mappings(model, options = {}) ⇒ Object

Second argument can be the whitelist_attributes array or keyword arguments. This is to ensure backwards compatibility with old plugins.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/para/model_helper.rb', line 11

def model_field_mappings(model, options = {})
  if Array == options
    whitelist_attributes = options
  else
    whitelist_attributes = options.fetch(:whitelist_attributes, nil)
    mappings = options.fetch(:mappings, {})
  end

  Para::AttributeFieldMappings.new(
    model, whitelist_attributes: whitelist_attributes, mappings: mappings
  )
end

#relation_klass_for(component, relation) ⇒ Object



24
25
26
# File 'app/helpers/para/model_helper.rb', line 24

def relation_klass_for(component, relation)
  component.class.reflect_on_association(relation).klass
end

#value_for(object, field_name, type = nil) ⇒ Object



32
33
34
35
# File 'app/helpers/para/model_helper.rb', line 32

def value_for(object, field_name, type = nil)
  field = field_for(object.class, field_name, type)
  field.value_for(object)
end