Sha256: 14d74856a3a7f6c5d5cde2a76c89c7ad2f93c15f85b2e784dc0c7f755cfd4545

Contents?: true

Size: 822 Bytes

Versions: 6

Compression:

Stored size: 822 Bytes

Contents

require "factory_bot"

namespace :human_attrs do
  desc "Show generated human attributes for ActiveRecord models"
  task :show, [:model] => [:environment] do |_t, args|
    FactoryBot.reload
    model_name = args[:model].tableize.singularize
    model = args[:model].classify.constantize

    instance = begin
      FactoryBot.build(model_name)
    rescue
      nil
    end

    if instance
      instance.id = 1 if instance.respond_to?(:id)
      instance.created_at = DateTime.current if instance.respond_to?(:created_at)
      instance.updated_at = DateTime.current if instance.respond_to?(:updated_at)
    end

    result = model.humanizers.map do |m|
      value = begin
        instance.send(m)
      rescue
        nil
      end

      !!value ? "#{m} => #{value}" : m
    end

    puts result.join("\n")
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
human_attributes-1.1.0 lib/tasks/human_attributes_tasks.rake
human_attributes-1.0.0 lib/tasks/human_attributes_tasks.rake
human_attributes-0.7.1 lib/tasks/human_attributes_tasks.rake
human_attributes-0.7.0 lib/tasks/human_attributes_tasks.rake
human_attributes-0.6.0 lib/tasks/human_attributes_tasks.rake
human_attributes-0.5.0 lib/tasks/human_attributes_tasks.rake