deforest 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46afcedf5c2b2140f01af815c8962d3bcebc884c5f1b397850047c23909012ab
4
- data.tar.gz: 84ba4dde69c91fdf8d3670fc913324e28a505e15b1883d0b715ce41beb556901
3
+ metadata.gz: 696760761eb3f30b0c9005b4b415517b2425e818b5a314d23872821b3a596877
4
+ data.tar.gz: fc24e0c602020f5b8bafb8ec8bc409e7e08ae7a84832654ec4c2b6d8aaf21633
5
5
  SHA512:
6
- metadata.gz: 261d8247c160e40d55444d0c655c7777968d78f5ac23cd9938e158cd544fb0373deacb9213f18246b8c4568643f96d563dd70025c438bd4e99bc1c8a7488368d
7
- data.tar.gz: 723cefe6975802deda157c05fbadfc70e3c1525ebd102f33f97877db77fef64461bb932750e485846944f3f5937070e88fca1c369e0d53a608d9b0635d2238df
6
+ metadata.gz: 3037cd3c5f07cf97a16199ffb3c710f6190be9575db5f96037b8d406eeecadc8fa3d2f5e7edf7a83c2a8eb490697d836403157b0d6b5efd14b3370fc5f375f39
7
+ data.tar.gz: bff04b8410c8c02c9ddb60d44190d1fc28eac31083e9c19842b3a10197120f4f9f7e1dfc73faafcbb5fe682ad7ebf6806ee091c4c3fd9fd75c9db1fafc2d7ab4
@@ -1,5 +1,5 @@
1
1
  module Deforest
2
- class ApplicationController < ActionController::Base
2
+ class ApplicationController < ::ApplicationController
3
3
  protect_from_forgery with: :exception
4
4
  end
5
5
  end
@@ -60,6 +60,7 @@ module Deforest
60
60
 
61
61
  def check_if_admin_logged_in
62
62
  if send(Deforest.current_admin_method_name).blank?
63
+ puts "Make sure to set config.current_admin_method_name to the correct method in config/initializers/deforest.rb"
63
64
  raise ActionController::RoutingError.new('Not Found')
64
65
  end
65
66
  end
@@ -1,3 +1,3 @@
1
1
  module Deforest
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/deforest.rb CHANGED
@@ -4,9 +4,7 @@ require "active_support"
4
4
  require "active_record"
5
5
 
6
6
  module Deforest
7
- mattr_accessor :write_logs_to_db_every, :current_admin_method_name, :most_used_percentile_threshold, :least_used_percentile_threshold, :track_dirs, :render_source_on_browser
8
- @@last_saved_log_file_at = nil
9
- @@saving_log_file = false
7
+ mattr_accessor :write_logs_to_db_every, :current_admin_method_name, :most_used_percentile_threshold, :least_used_percentile_threshold, :track_dirs, :render_source_on_browser, :last_saved_log_file_at, :saving_log_file
10
8
 
11
9
  def self.get_app_classes(dir)
12
10
  Dir["#{Rails.root}#{dir}/**/*.rb"].each do |f|
@@ -31,48 +29,6 @@ module Deforest
31
29
  end
32
30
  end
33
31
  end
34
-
35
- def self.override_instance_methods_for(klass, dir)
36
- klass.instance_methods(false).each do |mname|
37
- klass.instance_eval do
38
- alias_method "old_#{mname}", mname
39
- define_method mname do |*args, &block|
40
- old_method = self.class.instance_method("old_#{mname}")
41
- file_name, line_no = old_method.source_location
42
- if file_name.include?(dir)
43
- Deforest.insert_into_logs(mname, file_name, line_no)
44
- end
45
- if @@last_saved_log_file_at < Deforest.write_logs_to_db_every.ago && !@@saving_log_file
46
- Deforest.parse_and_save_log_file()
47
- t = Time.zone.now
48
- @@last_saved_log_file_at = t
49
- File.open("deforest_db_sync.txt", "w") { |fl| fl.write(t.to_i) }
50
- end
51
- old_method.bind(self).call(*args, &block)
52
- end
53
- end
54
- end
55
- end
56
-
57
- def self.override_class_methods_for(klass, dir)
58
- klass.singleton_methods(false).each do |mname|
59
- klass.singleton_class.send(:alias_method, "old_#{mname}", mname)
60
- klass.define_singleton_method mname do |*args, &block|
61
- old_method = self.singleton_method("old_#{mname}")
62
- file_name, line_no = old_method.source_location
63
- if file_name.include?(dir)
64
- Deforest.insert_into_logs(mname, file_name, line_no)
65
- end
66
- if @@last_saved_log_file_at < Deforest.write_logs_to_db_every.ago && !@@saving_log_file
67
- Deforest.parse_and_save_log_file()
68
- t = Time.zone.now
69
- @@last_saved_log_file_at = t
70
- File.open("deforest_db_sync.txt", "w") { |fl| fl.write(t.to_i) }
71
- end
72
- old_method.unbind.bind(self).call(*args, &block)
73
- end
74
- end
75
- end
76
32
 
77
33
  def self.initialize!
78
34
  if block_given?
@@ -80,10 +36,49 @@ module Deforest
80
36
  end
81
37
  self.initialize_db_sync_file()
82
38
  Deforest.track_dirs.each do |dir|
83
- self.get_app_classes(dir) do |model|
84
- if model.present?
85
- self.override_instance_methods_for(model, dir)
86
- self.override_class_methods_for(model, dir) unless dir.include?("/app/controllers")
39
+ self.get_app_classes(dir) do |klass|
40
+ if klass.present?
41
+ klass.prepend(Module.new do
42
+ klass.instance_methods(false).each do |mname|
43
+ method_location = klass.instance_method(mname).source_location
44
+ if method_location.first.ends_with?("#{klass.to_s.underscore}.rb")
45
+ define_method mname do |*args, &block|
46
+ file_name, line_no = method_location
47
+ if file_name.include?(dir)
48
+ Deforest.insert_into_logs(mname, file_name, line_no)
49
+ end
50
+ if Deforest.last_saved_log_file_at < Deforest.write_logs_to_db_every.ago && !Deforest.saving_log_file
51
+ Deforest.parse_and_save_log_file()
52
+ t = Time.zone.now
53
+ Deforest.last_saved_log_file_at = t
54
+ File.open("deforest_db_sync.txt", "w") { |fl| fl.write(t.to_i) }
55
+ end
56
+ super(*args, &block)
57
+ end
58
+ end
59
+ end
60
+ end)
61
+
62
+ klass.singleton_class.prepend(Module.new do
63
+ klass.singleton_methods(false).each do |mname|
64
+ method_location = klass.singleton_method(mname).source_location
65
+ if method_location.first.ends_with?("#{klass.to_s.underscore}.rb")
66
+ define_method mname do |*args, &block|
67
+ file_name, line_no = method_location
68
+ if file_name.include?(dir)
69
+ Deforest.insert_into_logs(mname, file_name, line_no)
70
+ end
71
+ if Deforest.last_saved_log_file_at < Deforest.write_logs_to_db_every.ago && !Deforest.saving_log_file
72
+ Deforest.parse_and_save_log_file()
73
+ t = Time.zone.now
74
+ Deforest.last_saved_log_file_at = t
75
+ File.open("deforest_db_sync.txt", "w") { |fl| fl.write(t.to_i) }
76
+ end
77
+ super(*args, &block)
78
+ end
79
+ end
80
+ end
81
+ end)
87
82
  end
88
83
  end
89
84
  end
@@ -92,11 +87,11 @@ module Deforest
92
87
  def self.initialize_db_sync_file
93
88
  File.open("deforest.log", "w") unless File.exist?("deforest.log")
94
89
  if File.exist?("deforest_db_sync.txt")
95
- @@last_saved_log_file_at = Time.at(File.open("deforest_db_sync.txt").read.to_i)
90
+ Deforest.last_saved_log_file_at = Time.at(File.open("deforest_db_sync.txt").read.to_i)
96
91
  else
97
92
  File.open("deforest_db_sync.txt", "w") do |f|
98
93
  current_time = Time.zone.now
99
- @@last_saved_log_file_at = current_time
94
+ Deforest.last_saved_log_file_at = current_time
100
95
  f.write(current_time.to_i)
101
96
  end
102
97
  end
@@ -104,14 +99,14 @@ module Deforest
104
99
 
105
100
  def self.insert_into_logs(method_name, file_name, line_no)
106
101
  key = "#{file_name}|#{line_no}|#{method_name}\n"
107
- log_file_name = @@saving_log_file ? "deforest_tmp.log" : "deforest.log"
102
+ log_file_name = Deforest.saving_log_file ? "deforest_tmp.log" : "deforest.log"
108
103
  File.open(log_file_name, "a") do |f|
109
104
  f.write(key)
110
105
  end
111
106
  end
112
107
 
113
108
  def self.parse_and_save_log_file
114
- @@saving_log_file = true
109
+ Deforest.saving_log_file = true
115
110
  sql_stmt = "INSERT INTO deforest_logs (file_name, line_no, method_name, count, created_at, updated_at) VALUES "
116
111
  hash = {}
117
112
  File.foreach("deforest.log") do |line|
@@ -137,7 +132,7 @@ module Deforest
137
132
  File.delete("deforest.log")
138
133
  end
139
134
  end
140
- @@saving_log_file = false
135
+ Deforest.saving_log_file = false
141
136
  end
142
137
 
143
138
  def self.prepare_file_for_render(file)
@@ -155,7 +150,7 @@ module Deforest
155
150
  line +
156
151
  "</span>&nbsp;&nbsp;" +
157
152
  "<span class='method_call_count'>#{line_no_count[idx]}</span>" +
158
- "<span class='last_accessed'>last called at: #{last_log_for_current_line.created_at.strftime('%m/%d/%Y')}</span>"
153
+ "<span class='last_accessed'>last called at: #{last_log_for_current_line.created_at.localtime.strftime('%m/%d/%Y')}</span>"
159
154
  else
160
155
  "<span>#{line}</span>"
161
156
  end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generate initializer file and migration file for logs.
3
+
4
+ Example:
5
+ rails g deforest
6
+
7
+ This will create:
8
+ db/migrate/[TIMESTAMP]create_deforest_logs.rb
9
+ config/initializers/deforest.rb
@@ -0,0 +1,39 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/migration'
3
+
4
+ class DeforestGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def copy_migration_file
10
+ migration_template "migration.rb", "db/migrate/create_deforest_logs.rb", migration_version: migration_version
11
+ end
12
+
13
+ def copy_initializer_file
14
+ if rails5_and_up? && Rails.autoloaders.zeitwerk_enabled?
15
+ copy_file "zeitwerk_enabled_initializer.rb", "config/initializers/deforest.rb"
16
+ else
17
+ copy_file "classic_enabled_initializer.rb", "config/initializers/deforest.rb"
18
+ end
19
+ end
20
+
21
+ def rails5_and_up?
22
+ Rails::VERSION::MAJOR >= 5
23
+ end
24
+
25
+ def migration_version
26
+ if rails5_and_up?
27
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
28
+ end
29
+ end
30
+
31
+ def self.next_migration_number(dirname)
32
+ if ActiveRecord::Base.timestamped_migrations
33
+ current_time = Time.now.utc
34
+ current_time.strftime("%Y%m%d%H%M%S")
35
+ else
36
+ "%.3d" % (current_numeric_version(dirname) + 1)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ class CreateDeforestLogs < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :deforest_logs do |t|
4
+ t.string :file_name
5
+ t.integer :line_no
6
+ t.string :method_name
7
+ t.integer :count
8
+
9
+ t.timestamps null: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ Rails.application.config.to_prepare do
2
+ Deforest.initialize! do |config|
3
+ config.write_logs_to_db_every = 1.minute
4
+ config.current_admin_method_name = :current_admin
5
+ config.most_used_percentile_threshold = 80
6
+ config.least_used_percentile_threshold = 20
7
+ config.track_dirs = ["/app/models", "/app/controllers", "/app/helpers"]
8
+ config.render_source_on_browser = true
9
+ end
10
+ end
Binary file