Skip to content

Commit

Permalink
Allow callers from some files to be ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeweaver committed May 15, 2017
1 parent b912cb3 commit 1527f80
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ Select your editor on the extension options page: chrome://extensions. Following
* Atom
* RubyMine

## Meta Request Configuration

If the ActiveRecord caller locations are including files you don't want, you can exclude those files by adding them to the configuration in config/meta_request.yml. Files in the backtrace that match a regex in the ingore paths list will be skipped.

```yaml
:ignore_paths:
- .*/directory/exclude_this_file
```
## Compatibility Warnings
If you're using [LiveReload](http://livereload.com/) or
Expand Down
1 change: 1 addition & 0 deletions meta_request/lib/meta_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module MetaRequest
autoload :Middlewares, "meta_request/middlewares"
autoload :LogInterceptor, "meta_request/log_interceptor"
autoload :AppNotifications, "meta_request/app_notifications"
autoload :Config, "meta_request/config"

def self.logger
@@logger ||= Logger.new(File.join(Rails.root, 'log', 'meta_request.log'))
Expand Down
10 changes: 7 additions & 3 deletions meta_request/lib/meta_request/app_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AppNotifications
payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
end

dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
dev_caller = caller.detect { |c| is_dev_caller(c) }
if dev_caller
c = Callsite.parse(dev_caller)
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
Expand All @@ -39,7 +39,7 @@ def self.subscribe
subscribe("meta_request.log").
subscribe("sql.active_record") do |*args|
name, start, ending, transaction_id, payload = args
dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
dev_caller = caller.detect { |c| is_dev_caller(c) }
if dev_caller
c = Callsite.parse(dev_caller)
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
Expand Down Expand Up @@ -71,6 +71,10 @@ def subscribe(event_name)
self
end

end
private

def self.is_dev_caller(file_path)
file_path.include?(MetaRequest.rails_root) && !Config.ignored_path?(file_path)
end
end
end
34 changes: 34 additions & 0 deletions meta_request/lib/meta_request/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'yaml'

module MetaRequest
module Config
def self.ignored_path?(file_path)
ingore_paths.any? { |ignore_path| ignore_path.match(file_path) }
end

def self.config_file
@@config_file ||= load_and_parse_config_file
end

private

def self.ingore_paths
config_file[:ignore_paths]
end

def self.load_and_parse_config_file
@@config_file = load_config_file
@@config_file[:ignore_paths] ||= []
@@config_file[:ignore_paths].map! do |ignore_path|
Regexp.new(ignore_path)
end
@@config_file.freeze
end

def self.load_config_file
YAML.load_file("#{MetaRequest.rails_root}/config/meta_request.yml") || {}
rescue Errno::ENOENT
{}
end
end
end
2 changes: 1 addition & 1 deletion meta_request/lib/meta_request/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MetaRequest
VERSION = '0.4.2'
VERSION = '0.4.3'
end
63 changes: 63 additions & 0 deletions meta_request/test/unit/meta_request/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'test_helper'
require 'tempfile'
require 'fileutils'

describe MetaRequest::Config do
CONFIG_DIRECTORY = "#{MetaRequest.rails_root}/config"
CONFIG_FILE_PATH = "#{CONFIG_DIRECTORY}/meta_request.yml"

def write_config_file(config)
file = File.new(CONFIG_FILE_PATH, "w")
file.write(config.to_yaml)
file.close
end

before(:each) do
FileUtils.mkdir_p(CONFIG_DIRECTORY)
end

after(:each) do
FileUtils.remove_dir(CONFIG_DIRECTORY, force: true)
MetaRequest::Config.class_variable_set(:@@config_file, nil)
end

describe 'config_file' do
it 'provides default config when config file is not present' do
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
end

it 'provides default config when config file is empty' do
write_config_file(nil)
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
end

it 'converts ignore paths array to regexs' do
config = {
ignore_paths: ['.*files', 'notaregex']
}
expected_ignore_paths = config[:ignore_paths].map { |p| Regexp.new(p) }
write_config_file(config)
assert_equal expected_ignore_paths, MetaRequest::Config.config_file[:ignore_paths]
end
end

describe 'ignored_path?' do
it 'does not ignore files if the ignore_paths option is empty' do
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/not/ignored.txt')
end

it 'ignores files that match the ignore_paths' do
config = {
ignore_paths: ['.*ignored.*', 'notaregex']
}
expected_ignore_paths = config[:ignore_paths].map { |p| Regexp.new(p) }
write_config_file(config)
assert_equal true, MetaRequest::Config.ignored_path?('this/file/is/ignored.txt')
assert_equal true, MetaRequest::Config.ignored_path?('notaregex')
assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/included.txt')
end

end
end

0 comments on commit 1527f80

Please sign in to comment.