module HumanAttributes::Extension

Public Instance Methods

humanize(*attrs, options) click to toggle source
# File lib/human_attributes/extension.rb, line 6
def humanize(*attrs, options)
  formatters = HumanAttributes::FormattersBuilder.new(attrs, options).build
  @builder ||= HumanAttributes::MethodBuilder.new(self)
  @humanizers ||= []
  formatters.each { |formatter| @humanizers << @builder.build(formatter) }
end
humanize_attributes(options = {}) click to toggle source
# File lib/human_attributes/extension.rb, line 13
def humanize_attributes(options = {})
  included_attrs = options.fetch(:only, nil)
  excluded_attrs = options.fetch(:except, nil)
  columns.each do |col|
    next if col.name.ends_with?("_id")
    next if included_attrs && !included_attrs.include?(col.name.to_sym)
    next if excluded_attrs && excluded_attrs.include?(col.name.to_sym)

    humanize_from_type(col)
  end
end
humanize_date(attr_name) click to toggle source
# File lib/human_attributes/extension.rb, line 50
def humanize_date(attr_name)
  humanize(attr_name, date: true)
  humanize(attr_name, date: { format: :short, suffix: "to_short_date" })
  humanize(attr_name, date: { format: :short, suffix: "to_long_date" })
end
humanize_datetime(attr_name) click to toggle source
# File lib/human_attributes/extension.rb, line 56
def humanize_datetime(attr_name)
  humanize(attr_name, datetime: true)
  humanize(attr_name, datetime: { format: :short, suffix: "to_short_datetime" })
  humanize(attr_name, datetime: { format: :short, suffix: "to_long_datetime" })
end
humanize_from_type(col) click to toggle source
# File lib/human_attributes/extension.rb, line 35
def humanize_from_type(col)
  if col.name == "id"
    humanize(:id, custom: { formatter: ->(_o, value) { "#{model_name.human}: ##{value}" } })
  elsif col.type == :date
    humanize_date(col.name)
  elsif col.type == :datetime
    humanize_date(col.name)
    humanize_datetime(col.name)
  elsif [:decimal, :float, :integer].include?(col.type)
    humanize(col.name, delimiter: true)
  elsif col.type == :boolean
    humanize(col.name, boolean: true)
  end
end
humanizers() click to toggle source
# File lib/human_attributes/extension.rb, line 25
def humanizers
  return [] unless @humanizers

  @humanizers.uniq!
  @humanizers.select! { |method| method_defined?(method) }
  @humanizers
end