Sha256: 8806e62ed14598df9203c4a611a5753899c4a71c202ad016e3968efe669d43ee

Contents?: true

Size: 1.6 KB

Versions: 42

Compression:

Stored size: 1.6 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

    # Second argument can be the whitelist_attributes array or keyword
    # arguments. This is to ensure backwards compatibility with old plugins.
    #
    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

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

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

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

    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

    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

42 entries across 42 versions & 1 rubygems

Version Path
para-0.11.4 app/helpers/para/model_helper.rb
para-0.11.3 app/helpers/para/model_helper.rb
para-0.11.2 app/helpers/para/model_helper.rb
para-0.11.1 app/helpers/para/model_helper.rb
para-0.11.0 app/helpers/para/model_helper.rb
para-0.10.0 app/helpers/para/model_helper.rb
para-0.9.4 app/helpers/para/model_helper.rb
para-0.9.3.3 app/helpers/para/model_helper.rb
para-0.9.3.2 app/helpers/para/model_helper.rb
para-0.9.3.1 app/helpers/para/model_helper.rb
para-0.9.2 app/helpers/para/model_helper.rb
para-0.9.0 app/helpers/para/model_helper.rb
para-0.8.15 app/helpers/para/model_helper.rb
para-0.8.14 app/helpers/para/model_helper.rb
para-0.8.13 app/helpers/para/model_helper.rb
para-0.8.12 app/helpers/para/model_helper.rb
para-0.8.11 app/helpers/para/model_helper.rb
para-0.8.10 app/helpers/para/model_helper.rb
para-0.8.9 app/helpers/para/model_helper.rb
para-0.8.8 app/helpers/para/model_helper.rb