This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Low level Helpers
Stan Lo edited this page May 24, 2020
·
3 revisions
tap_init!(class)
- tracks a class’ instance initialization
calls = []
tap_init!(Student) do |payload|
calls << [payload[:method_name], payload[:arguments]]
end
Student.new("Stan", 18)
Student.new("Jane", 23)
puts(calls.to_s) #=> [[:initialize, {:name=>"Stan", :age=>18}], [:initialize, {:name=>"Jane", :age=>23}]]
tap_on!(object)
- tracks any calls received by the object.
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def show
tap_on!(@post).and_print(:method_name_and_location)
end
end
And you can see these in log:
name FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
user_id FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
to_param FROM /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
Also check the track_as_records
option if you want to track ActiveRecord
records.
tap_passed!(target)
tracks method calls that takes the target as its argument. This is particularly useful when debugging libraries. It saves your time from jumping between files and check which path the object will go.
class PostsController < ApplicationController
# GET /posts/new
def new
@post = Post.new
tap_passed!(@post) do |payload|
puts(payload.passed_at(with_method_head: true))
end
end
end
Passed as 'record' in method ':polymorphic_mapping'
> def polymorphic_mapping(record)
at /Users/st0012/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/polymorphic_routes.rb:131
Passed as 'klass' in method ':get_method_for_class'
> def get_method_for_class(klass)
at /Users/st0012/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/polymorphic_routes.rb:269
Passed as 'record' in method ':handle_model'
> def handle_model(record)
at /Users/st0012/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/polymorphic_routes.rb:227
Passed as 'record_or_hash_or_array' in method ':polymorphic_method'
> def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
at /Users/st0012/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/polymorphic_routes.rb:139
tap_assoc!(activerecord_object)
tracks association calls on a record, like post.comments
tap_assoc!(order).and_print(:method_name_and_location)
payments FROM /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
line_items FROM /MY_PROJECT/app/models/line_item_container_helpers.rb:44
effective_line_items FROM /MY_PROJECT/app/models/line_item_container_helpers.rb:110
amending_orders FROM /MY_PROJECT/app/models/order.rb:385
amends_order FROM /MY_PROJECT/app/models/order.rb:432
tap_sql!(anything_that_generates_sql_queries)
tracks sql queries generated from the target
class PostsController < ApplicationController
def index
# simulate current_user
@current_user = User.last
# reusable ActiveRecord::Relation
@posts = Post.all
tap_sql!(@posts) do |payload|
puts("Method: #{payload[:method_name]} generated sql: #{payload[:sql]} from #{payload[:filepath]}:#{payload[:line_number]}")
end
end
end
<h1>Posts (<%= @posts.count %>)</h1>
......
<% @posts.each do |post| %>
......
<% end %>
......
<p>Posts created by you: <%= @posts.where(user: @current_user).count %></p>
Method: count generated sql: SELECT COUNT(*) FROM "posts" from /PROJECT_PATH/rails-6-sample/app/views/posts/index.html.erb:3
Method: each generated sql: SELECT "posts".* FROM "posts" from /PROJECT_PATH/rails-6-sample/app/views/posts/index.html.erb:16
Method: count generated sql: SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? from /PROJECT_PATH/rails-6-sample/app/views/posts/index.html.erb:31