attribute_normalizer icon indicating copy to clipboard operation
attribute_normalizer copied to clipboard

Get attribute name within normalizer

Open tanraya opened this issue 9 years ago • 1 comments

I would like to create a normalizer with auto-truncation feature, so I need to know the current limit of a table field. To determine the length of the field I need to know the name of the attribute.

Here's what I want:

  config.normalizers[:auto_truncate] = lambda do |value, options|
    column = options[:model].column_for_attribute(options[:attribute_name])

    if value.is_a?(String) && column.limit
      value.mb_chars.limit(column.limit)
    else
      value
    end
  end

I looked into the source and found nothing relative to the topic. What if I add :attribute_name and :model keys into options hash and send PR?

tanraya avatar Nov 21 '15 14:11 tanraya

Yes. I can see value in knowing the attribute and even having access to instance.

mdeering avatar Nov 11 '16 18:11 mdeering