Class: DiscoverUnusedPartials::PartialWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/discover-unused-partials.rb

Constant Summary collapse

EXT =
%w(.html.erb .text.erb .pdf.erb .erb .html.haml .text.haml .haml .rhtml .html.slim slim)
@@filename =
/[a-zA-Z\d_\/]+?/
@@extension =
/\.\w+/
@@partial =
/:partial\s*=>\s*|partial:\s*/
@@render =
/\brender\s*(?:\(\s*)?/

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PartialWorker

Returns a new instance of PartialWorker.



38
39
40
# File 'lib/discover-unused-partials.rb', line 38

def initialize options
  @options = options
end

Instance Method Details

#check_extension_path(file) ⇒ Object



121
122
123
# File 'lib/discover-unused-partials.rb', line 121

def check_extension_path(file)
  "#{file}#{EXT.find{ |e| File.exists? file + e }}"
end

#each_file(root, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/discover-unused-partials.rb', line 125

def each_file(root, &block)
  files = Dir.glob("#{root}/*")
  files.each do |file|
    if File.directory? file
      next if file =~ %r[^app/assets]
      each_file(file) {|file| yield file}
    else
      yield file
    end
  end
end

#existent_partials(root) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/discover-unused-partials.rb', line 42

def existent_partials root
  partials = []
  each_file(root) do |file|
    if file =~ /^.*\/_.*$/
      partials << file.strip
    end
  end

  partials
end

#process_partials(files) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/discover-unused-partials.rb', line 82

def process_partials(files)
  partials = @options['keep'] || []
  dynamic = {}
  files.each do |file|
    File.open(file) do |f|
      f.each do |line|
        line.strip!
        if line =~ %r[(?:#@@partial|#@@render)(['"])/?(#@@filename)#@@extension*\1]
          match = $2
          if match.index("/")

            path = match.split('/')[0...-1].join('/')
            file_name = "_#{match.split('/')[-1]}"

            full_path = "app/views/#{path}/#{file_name}"
          else
            if file =~ /app\/controllers\/(.*)_controller.rb/
              full_path = "app/views/#{$1}/_#{match}"
            else
              full_path = "#{file.split('/')[0...-1].join('/')}/_#{match}"
            end
          end
          partials << check_extension_path(full_path)
        elsif line =~ /#@@partial|#@@render["']/
          if @options["dynamic"] && @options["dynamic"][file]
            partials += @options["dynamic"][file]
          else
            dynamic[file] ||= []
            dynamic[file] << line
          end
        end
      end
    end
  end
  partials.uniq!
  [partials, dynamic]
end

#used_partials(root) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/discover-unused-partials.rb', line 53

def used_partials root
  raise "#{Dir.pwd} does not have '#{root}' directory" unless File.directory? root
  files = []
  each_file(root) do |file|
    files << file
  end
  tree = {}
  level = 1
  existent = existent_partials(root)
  top_dynamic = nil
  loop do
    used, dynamic = process_partials(files)
    break if level > 1 && used.size == tree[level-1][:used].size
    tree[level] = {
      used: used,
    }
    if level == 1
      top_dynamic = dynamic
      tree[level][:unused] = existent - used
    else
      tree[level][:unused] = tree[level-1][:used] - used
    end
    break unless (files - tree[level][:unused]).size < files.size
    files -= tree[level][:unused]
    level += 1
  end
  [tree, top_dynamic]
end