filter_decrufter 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd9c4f9c59da12771bbf12d09fadb5ce6fe282a3
4
+ data.tar.gz: 115f66febd38a0cefc999919eccb2ae03a2f09af
5
+ SHA512:
6
+ metadata.gz: 25fa82579c3e2c0daf3b620b1fd1800632cc20a1108dac15bc359e7d38a0082b40dd49aa3ca8a8e068bebdbdb0de274e59005d0d1fb2620888378a8059bdf4ca
7
+ data.tar.gz: a469df57be33490c3dcc4ea499085a3f61b215815a6859b8954da39d38f73e7c04382360599594ee64188de23636a4bee50f55ff0105d124f2f2aab2a407a4bd
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ## 0.0.3 (2015-02-18)
2
+
3
+ * [FIXED] No longer reports all violations as being for before_filters
4
+ * [NEW] Rake tasks are required automatically now
5
+
6
+ ## 0.0.2 (2015-02-17)
7
+
8
+ * [NEW] Works fine with Ruby 1.9.3
9
+
10
+ ## 0.0.1 (2015-02-16)
11
+
12
+ * [NEW] Initial release
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # FilterDecrufter
2
+
3
+ FilterDecrufter is a little utility for cleaning up before_filters.
4
+
5
+ Suppose you have a Rails controller with a before_filter:
6
+
7
+ before_filter :load_widget, :only => [:show, :frobnicate]
8
+
9
+ If you've deleted the frobnicate action, Rails won't complain. But FilterDecrufter will!
10
+
11
+ # Usage
12
+
13
+ Add it to your Gemfile in the development group:
14
+
15
+ # In your Gemfile
16
+ gem 'filter_decrufter'
17
+
18
+ Run the task!
19
+
20
+ $ bundle exec rake filter_decrufter:check
21
+ Api::V1::WidgetsController before_filter 'find_widget' has an :only constraint with a non-existent action name 'show'
22
+ EmployeesController after_filter 'set_name' has an :only constraint with a non-existent action name 'frobnicate'
23
+
24
+ Tested with Rails 3.2.
25
+
26
+ # Credits
27
+
28
+ * Tom Copeland - author
29
+ * John Moon - design discussions
@@ -0,0 +1,2 @@
1
+ require 'filter_decrufter/checker'
2
+ require 'filter_decrufter/tasks'
@@ -0,0 +1,103 @@
1
+ module FilterDecrufter
2
+
3
+ class Hit
4
+
5
+ attr_accessor :controller_class, :before_filter_name, :options, :filter_type
6
+
7
+ def initialize(controller_class, before_filter_name, options, filter_type)
8
+ @controller_class = controller_class
9
+ @before_filter_name = before_filter_name
10
+ @options = options
11
+ @filter_type = filter_type
12
+ end
13
+
14
+ def actions_include?(action_name)
15
+ action_methods.include?(action_name)
16
+ end
17
+
18
+ def populated_only_except_options
19
+ options.select {|opt_name,opt_value| [:only,:except].include?(opt_name) && !opt_value.empty? }
20
+ end
21
+
22
+ private
23
+
24
+ def action_methods
25
+ controller_class.action_methods.map &:to_sym
26
+ end
27
+
28
+ end
29
+
30
+ class Report
31
+
32
+ attr_accessor :hits
33
+
34
+ def self.instance
35
+ @instance ||= Report.new
36
+ end
37
+
38
+ def initialize
39
+ @hits = []
40
+ end
41
+
42
+ def add(hit)
43
+ raise "Oops filter_decrufter can't handle this version of Rails" unless hit.controller_class.respond_to?(:action_methods)
44
+ hits << hit
45
+ end
46
+
47
+ def find_problems
48
+ hits.each do |hit|
49
+ hit.populated_only_except_options.each do |k,v|
50
+ [:only, :except].each do |constraint_name|
51
+ check_constraint(constraint_name, hit) if hit.populated_only_except_options[constraint_name].present?
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def check_constraint(name, hit)
58
+ [hit.populated_only_except_options[name]].flatten.each do |action_syms|
59
+ [action_syms].flatten.each do |action_name|
60
+ if !hit.actions_include?(action_name)
61
+ puts "#{hit.controller_class} #{hit.filter_type} '#{hit.before_filter_name}' has an :#{name} constraint with a non-existent action name '#{action_name}'"
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ class Checker
70
+
71
+ def initialize
72
+ raise "This is a Rails utility" unless defined?(Rails)
73
+ raise "Oops filter_decrufter can't handle this version of Rails" unless Rails.respond_to?(:application)
74
+ end
75
+
76
+ def check
77
+ [:before_filter, :around_filter, :after_filter].each {|s| patch_method(s) }
78
+ load_all_controllers
79
+ show_report
80
+ nil
81
+ end
82
+
83
+ private
84
+
85
+ def show_report
86
+ Report.instance.find_problems
87
+ end
88
+
89
+ def patch_method(filter_sym)
90
+ ApplicationController.define_singleton_method(filter_sym) do |*args, &blk|
91
+ if args.many? && (args[1][:only].present? || args[1][:except].present?)
92
+ Report.instance.add(FilterDecrufter::Hit.new(self, args[0], args[1], filter_sym))
93
+ end
94
+ super(*args, &blk)
95
+ end
96
+ end
97
+
98
+ def load_all_controllers
99
+ Rails.application.eager_load!
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,8 @@
1
+ namespace :filter_decrufter do
2
+
3
+ desc "Find the cruft"
4
+ task :check => :environment do
5
+ FilterDecrufter::Checker.new.check
6
+ end
7
+
8
+ end
@@ -0,0 +1,3 @@
1
+ module FilterDecrufter
2
+ VERSION = '0.0.3'
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filter_decrufter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Tom Copeland
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: Finds old action symbols in Rails before / after / around filters
42
+ email: tom@thomasleecopeland.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - CHANGELOG.md
48
+ - README.md
49
+ - lib/filter_decrufter.rb
50
+ - lib/filter_decrufter/checker.rb
51
+ - lib/filter_decrufter/tasks.rb
52
+ - lib/filter_decrufter/version.rb
53
+ homepage: https://github.com/tcopeland/filter_decrufter
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.9.3
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project: none
73
+ rubygems_version: 2.2.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Finds cruft in Rails filters
77
+ test_files: []