unused_view 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,6 +23,8 @@ $ bundle exec rake 'unused_views[users]'
23
23
  /Users/toqoz/Projects/ppp/app/views/articles/new.html.erb
24
24
  /Users/toqoz/Projects/ppp/app/views/users/create.html.erb
25
25
  /Users/toqoz/Projects/ppp/app/views/users/edit.html.erb
26
+ /Users/toqoz/Projects/ppp/app/views/users/_form.html.erb
27
+ /Users/toqoz/Projects/ppp/app/views/shared/_header.html.erb
26
28
 
27
29
  ```
28
30
 
@@ -35,6 +37,8 @@ irb(main):001:0> show_unused_views
35
37
  /Users/toqoz/Projects/ppp/app/views/articles/new.html.erb
36
38
  /Users/toqoz/Projects/ppp/app/views/users/create.html.erb
37
39
  /Users/toqoz/Projects/ppp/app/views/users/edit.html.erb
40
+ /Users/toqoz/Projects/ppp/app/views/users/_form.html.erb
41
+ /Users/toqoz/Projects/ppp/app/views/shared/_header.html.erb
38
42
 
39
43
  # if you use pry-rails
40
44
  [1] pry(main)> show-unused-views
@@ -42,8 +46,17 @@ irb(main):001:0> show_unused_views
42
46
  /Users/toqoz/Projects/ppp/app/views/articles/new.html.erb
43
47
  /Users/toqoz/Projects/ppp/app/views/users/create.html.erb
44
48
  /Users/toqoz/Projects/ppp/app/views/users/edit.html.erb
49
+ /Users/toqoz/Projects/ppp/app/views/users/_form.html.erb
50
+ /Users/toqoz/Projects/ppp/app/views/shared/_header.html.erb
45
51
  ```
46
52
 
47
53
  then see list of unused views.
48
54
 
49
- Now this is not support partial view.
55
+
56
+ ## Testing
57
+
58
+ ```sh
59
+ $ cd path-to-unused-view
60
+ $ bundle install
61
+ $ rspec
62
+ ```
@@ -1,43 +1,15 @@
1
1
  require 'unused_view/railtie'
2
+ require 'unused_view/view'
3
+ require 'unused_view/layout'
4
+ require 'unused_view/partial_view'
5
+ require 'unused_view/target_files'
2
6
 
3
7
  module UnusedView
4
8
  extend self
5
9
 
6
10
  def find_all(base_path)
7
- Rails.application.instance_eval do
8
- used_views = ApplicationController.descendants.reduce([]) do |views, controller_class|
9
- controller = controller_class.new
10
- views + UnusedView.find_views(controller).compact + [ UnusedView.find_layout(controller) ].compact
11
- end.map(&:identifier).uniq
12
-
13
- ApplicationController.view_paths.reduce([]) do |unused_views, view_path|
14
- unused_views + Dir.glob(File.join(view_path, '**/[a-zA-Z]*')).select do |f|
15
- File.file?(f) && used_views.exclude?(f) && f[%r{^#{base_path}}]
16
- end
17
- end
18
- end
19
- end
20
-
21
- def find_views(controller)
22
- controller.action_methods.map do |action|
23
- controller.lookup_context.find(action, controller._prefixes) rescue nil
24
- end
25
- end
26
-
27
- def find_layout(controller)
28
- name = layout_name(controller)
29
- if name
30
- controller.lookup_context.find(name, 'layouts') rescue nil
31
- end
32
- end
33
-
34
- def layout_name(controller)
35
- name = controller.send(:_layout) rescue nil
36
- case name
37
- when String
38
- name
39
- when ActionView::Template
40
- File.basename(name.identifier).split('.').first
41
- end
11
+ controllers = ApplicationController.descendants.map(&:new)
12
+ views = View.new(controllers).find_all + Layout.new(controllers).find_all
13
+ TargetFiles.new(base_path).all - views - PartialView.new(views).find_all
42
14
  end
43
15
  end
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module UnusedView
4
+ class Layout
5
+ def initialize(controllers)
6
+ @controllers = controllers
7
+ end
8
+
9
+ def find_all
10
+ @controllers.reduce([]) do |layouts, controller|
11
+ if _name = name(controller)
12
+ begin
13
+ layouts << controller.lookup_context.find(_name, 'layouts')
14
+ rescue
15
+ layouts
16
+ end
17
+ else
18
+ layouts
19
+ end
20
+ end.uniq.compact.map(&:identifier)
21
+ end
22
+
23
+ def name(controller)
24
+ _name = controller.send(:_layout) rescue nil
25
+ case _name
26
+ when String
27
+ _name
28
+ when ActionView::Template
29
+ File.basename(_name.identifier).split('.').first
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module UnusedView
4
+ class PartialView
5
+ PATTERN = {
6
+ render: %r{\brender\s*(?:\(\s*)?},
7
+ partial: %r{:partial\s*=>\s*|partial:\s*},
8
+ filename: %r{[\w/]+?},
9
+ extention: %r{\.\w+}
10
+ }
11
+
12
+ def initialize(views)
13
+ @views = views
14
+ end
15
+
16
+ def find_all
17
+ @views.reduce([]) do |sum, view|
18
+ used_partials_by_view = File.open(view) { |f| f.read.split("\n") }.map do |line|
19
+ if line =~ %r[(?:#{PATTERN[:partial]}|#{PATTERN[:render]})(['"])/?(#{PATTERN[:filename]})#{PATTERN[:extention]}*\1]
20
+ $2 # virtual path. e.g. articles/form
21
+ end
22
+ end.compact.map do |vpath|
23
+ File.class_eval do
24
+ if vpath.index('/')
25
+ path = ApplicationController.view_paths.map do |view_path|
26
+ File.join(view_path.to_s, vpath)
27
+ end.select { |f| File.exists?(File.dirname(f)) }.first || vpath
28
+ else
29
+ path = view
30
+ end
31
+ filename = "_#{basename(vpath)}.#{basename(view).split('.')[1..-1].join('.')}"
32
+ expand_path(join(dirname(path), filename)).to_s
33
+ end
34
+ end
35
+ sum + used_partials_by_view
36
+ end.uniq
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module UnusedView
4
+ class TargetFiles
5
+ def initialize(path)
6
+ @path = path
7
+ end
8
+
9
+ def all
10
+ views + partial_views
11
+ end
12
+
13
+ def views
14
+ ApplicationController.view_paths.reduce([]) do |sum, view_path|
15
+ sum + Dir[File.join(view_path, '**/[a-zA-Z]*')].select { |f| File.file?(f) && f[%r{^#{@path}}] }
16
+ end
17
+ end
18
+
19
+ def partial_views
20
+ ApplicationController.view_paths.reduce([]) do |sum, view_path|
21
+ sum + Dir[File.join(view_path, '**/_*')].select { |f| File.file?(f) && f[%r{^#{@path}}] }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module UnusedView
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module UnusedView
4
+ class View
5
+ def initialize(controllers)
6
+ @controllers = controllers
7
+ end
8
+
9
+ def find_all
10
+ @controllers.reduce([]) do |views, controller|
11
+ views + controller.action_methods.map do |action|
12
+ controller.lookup_context.find(action, controller._prefixes) rescue nil
13
+ end
14
+ end.uniq.compact.map(&:identifier)
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: unused_view
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Takatoshi Matsumoto
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-25 00:00:00.000000000 Z
12
+ date: 2012-12-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  type: :runtime
@@ -84,9 +84,13 @@ extra_rdoc_files: []
84
84
  files:
85
85
  - lib/unused_view/commands/show_unused_views.rb
86
86
  - lib/unused_view/commands.rb
87
+ - lib/unused_view/layout.rb
88
+ - lib/unused_view/partial_view.rb
87
89
  - lib/unused_view/railtie.rb
90
+ - lib/unused_view/target_files.rb
88
91
  - lib/unused_view/tasks.rake
89
92
  - lib/unused_view/version.rb
93
+ - lib/unused_view/view.rb
90
94
  - lib/unused_view.rb
91
95
  - MIT-LICENSE
92
96
  - Rakefile
@@ -104,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
108
  - !ruby/object:Gem::Version
105
109
  segments:
106
110
  - 0
107
- hash: 2644683764795231228
111
+ hash: 2956511099555490744
108
112
  version: '0'
109
113
  required_rubygems_version: !ruby/object:Gem::Requirement
110
114
  none: false
@@ -113,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
117
  - !ruby/object:Gem::Version
114
118
  segments:
115
119
  - 0
116
- hash: 2644683764795231228
120
+ hash: 2956511099555490744
117
121
  version: '0'
118
122
  requirements: []
119
123
  rubyforge_project: