Sha256: 9ad237591ae07aafbd28843a320247b43bf4042018beae613d61555fb26b0da4

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

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

    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

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

    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

    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
para-0.5.3 app/helpers/para/model_helper.rb
para-0.5.1 app/helpers/para/model_helper.rb
para-0.5.0 app/helpers/para/model_helper.rb
para-0.4.0 app/helpers/para/model_helper.rb