Module: Para::ModelHelper
- Included in:
- Admin::ResourcesController, Admin::SearchController
- Defined in:
- app/helpers/para/model_helper.rb
Instance Method Summary collapse
- #attribute_field_mappings_for(component, relation) ⇒ Object
- #excerpt_value_for(value) ⇒ Object
- #field_for(model, field_name, type = nil) ⇒ Object
- #field_value_for(object, field_name, type = nil) ⇒ Object
-
#model_field_mappings(model, options = {}) ⇒ Object
Second argument can be the whitelist_attributes array or keyword arguments.
- #relation_klass_for(component, relation) ⇒ Object
- #value_for(object, field_name, type = nil) ⇒ Object
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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/helpers/para/model_helper.rb', line 50 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_for(model, field_name, type = nil) ⇒ Object
30 31 32 |
# File 'app/helpers/para/model_helper.rb', line 30 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
39 40 41 42 43 44 45 46 47 48 |
# File 'app/helpers/para/model_helper.rb', line 39 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 23 24 |
# File 'app/helpers/para/model_helper.rb', line 11 def model_field_mappings(model, = {}) if Array == whitelist_attributes = else whitelist_attributes = .fetch(:whitelist_attributes, nil) mappings = .fetch(:mappings, {}) end store_key = ['model', 'mappings', model.name.underscore].join(':') Para::AttributeFieldMappings.new( model, whitelist_attributes: whitelist_attributes, mappings: mappings ) end |
#relation_klass_for(component, relation) ⇒ Object
26 27 28 |
# File 'app/helpers/para/model_helper.rb', line 26 def relation_klass_for(component, relation) component.class.reflect_on_association(relation).klass end |
#value_for(object, field_name, type = nil) ⇒ Object
34 35 36 37 |
# File 'app/helpers/para/model_helper.rb', line 34 def value_for(object, field_name, type = nil) field = field_for(object.class, field_name, type) field.value_for(object) end |