forked from franckverrot/no_querying_views
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_querying_views.rb
28 lines (27 loc) · 1.08 KB
/
no_querying_views.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
if Rails.env.development?
module ::ActiveRecord
module ConnectionAdapters
%w(PostgreSQLAdapter MysqlAdapter SQLite3Adapter).each do |adapter_name|
begin
adapter = const_get adapter_name
adapter.class_eval do
puts "[WARNING] NoQueryingViews is preventing your views from triggering PG, MySQL or SQLite3 database connections. (To permit this, remove the initializer.)"
alias :orig_execute :execute
def execute(*args)
first_view = caller.grep(/app\/views/).first
first_helper = caller.grep(/app\/helpers/).first
# if we're coming from a view, let's analyze the situation
if !first_view.nil? and (first_helper.nil? or (caller.index(first_view) < caller.index(first_helper)))
raise "No query from view prohibited, eager-load from a controller instead."
else
orig_execute *args
end
end
end
rescue
# the adapter has not been instantiated
end
end
end
end
end