Sha256: aeda35d92a0a61f0227c99018ae5e36bfc94c70426fba2be23acf8bdfaecc6d1
Contents?: true
Size: 1.71 KB
Versions: 2
Compression:
Stored size: 1.71 KB
Contents
module HumanAttributes module ActiveRecordExtension extend ActiveSupport::Concern class_methods do 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 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 def humanizers return [] unless @humanizers @humanizers.uniq! @humanizers.reject! { |method| !method_defined?(method) } @humanizers end private def humanize_from_type(col) if col.name == "id" humanize(:id, custom: { formatter: ->(_o, value) { "#{model_name.human}: ##{value}" } }) elsif [:date, :datetime].include?(col.type) humanize_date(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 def humanize_date(attr_name) humanize(attr_name, date: true) humanize(attr_name, date: { format: :short, suffix: "to_short_date" }) end end end end ActiveRecord::Base.send(:include, HumanAttributes::ActiveRecordExtension)
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
human_attributes-0.1.1 | lib/human_attributes/active_record_extension.rb |
human_attributes-0.1.0 | lib/human_attributes/active_record_extension.rb |