Module: Para::ModelHelper

Included in:
Admin::ResourcesController, ApplicationHelper
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



26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/para/model_helper.rb', line 26

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

  value = sanitize(value, tags: [])
  
  if (truncated = value[0..100]) != value
    "#{ truncated }..."
  else
    value
  end
end

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



20
21
22
23
24
# File 'app/helpers/para/model_helper.rb', line 20

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

#model_field_mappings(model, attributes = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'app/helpers/para/model_helper.rb', line 8

def model_field_mappings(model, attributes = nil)
  store_key = ['model', 'mappings', model.name.underscore].join(':')

  Para.store[store_key] ||= Para::AttributeFieldMappings.new(
    model, whitelist_attributes: attributes
  )
end

#relation_klass_for(component, relation) ⇒ Object



16
17
18
# File 'app/helpers/para/model_helper.rb', line 16

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