Module: KickinRistretto

Defined in:
lib/kickin_ristretto.rb,
lib/kickin_ristretto/version.rb

Overview

require ‘prawn-table’

Constant Summary collapse

ROOT_PATH =
File.expand_path '../../', __FILE__
VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#clean_non_referenced_images(image_directory = 'app/assets/images') ⇒ Object

Search through all images in the project and check to see if they are referenced somewhere If they are not referenced, prompt to delete them



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kickin_ristretto.rb', line 24

def clean_non_referenced_images(image_directory = 'app/assets/images')
  images = search_directory(image_directory)
  legacy_images = collect_none_referenced_images(images)
  puts "\n Is this what you want to happen? [Y/N]"
  answer = STDIN.gets.chomp
  if answer == 'Y'
    delete_files(legacy_images)
  elsif answer == 'N'
    puts 'Exiting'
    return false # Abort the rake task
  end
end

#collect_none_referenced_images(images) ⇒ Object

Returns a list of images with paths that are not referenced in the rails project



72
73
74
75
76
77
78
79
80
# File 'lib/kickin_ristretto.rb', line 72

def collect_none_referenced_images(images)
  legacy_images = []
  images.each do |image|
    filename = File.basename(image)
    result = system("grep -r '#{filename}' ./app")
    legacy_images << image if result == false
  end
  legacy_images
end

#delete_files(image_files) ⇒ Object

Deletes each of the files



83
84
85
86
# File 'lib/kickin_ristretto.rb', line 83

def delete_files(image_files)
  #image_files.each { |image_file| puts "Deleting #{image_file}" }
  image_files.each {|image_file| File.delete(image_file) if File.exist?(image_file)}
end

#get_all_images_by_type(files) ⇒ Object



88
89
90
# File 'lib/kickin_ristretto.rb', line 88

def get_all_images_by_type(files)
  files.group_by{ |f| File.extname(f) }
end

#image_audit_report(image_directory = 'app/assets/images') ⇒ Object

Produce a PDF report listing all the images in the website



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kickin_ristretto.rb', line 39

def image_audit_report(image_directory = 'app/assets/images')
  images = search_directory(image_directory)
  #filtered_images = get_all_images_by_type(images)['.gif', '.png', '.jpg', '.jpeg', '.JPG', '.JPEG']
  filtered_images = get_all_images_by_type(images).values_at('.png', '.jpg', '.jpeg', '.JPG', '.JPEG').flatten
  filtered_images = filtered_images.reject { |v| v.nil? }

  Prawn::Document.generate("image_audit_report.pdf") do
    text "Image Audit Report"

    data = [["File Path", "Image"]]
    filtered_images.each do |image|
      # puts image
      # text image
      # image image
      data += [[image, {image: image, :fit => [400, 400]}]]
    end


    #data += [["..."]] * 30
    table(data, :header => true)

    #image "#{Prawn::DATADIR}/images/pigs.jpg"
  end
end

#search_directory(image_directory = 'app/assets/images') ⇒ Object

Search the Directories for images Returns a list of files with fullpath



66
67
68
69
# File 'lib/kickin_ristretto.rb', line 66

def search_directory(image_directory = 'app/assets/images')
  image_directory_regex = "#{image_directory}/**/*"
  Dir[image_directory_regex].select{ |e| File.file? e }
end