Skip to content

Instantly share code, notes, and snippets.

@ecoologic
Last active February 27, 2018 05:54
Show Gist options
  • Save ecoologic/66939216c9b2443ae643d6d114b53fcc to your computer and use it in GitHub Desktop.
Save ecoologic/66939216c9b2443ae643d6d114b53fcc to your computer and use it in GitHub Desktop.
Ecoologic's debugging and console tricks (for Brisbane Ruby Meetup talk)
# CGI.parse(params) # "checklist_list_ids%5B%5D=10806"
# Gem::Specification.map(&:name).sort
# app.users_path # => "/users"
::U = User # Etc
def self.helpme
puts <<-TEXT
v # view helpers (eg: content_tag)
exceptions # List of all exceptions
base_exceptions # List of exceptions without namespace
durl(url) # Open URL in development
...
TEXT
end
# v.content_tag etc
def self.v
# Rails.application.routes.url_helper # Rails 3
ActionController::Base.helpers
end
def self.exceptions
ObjectSpace.each_object(Class).select { |k| k < StandardError }.map(&:to_s).sort
end
def self.base_exceptions
exceptions.select { |e| !e.to_s.include?('::') }
end
# development_url
# https://my-production-app.com/users/1 => http://localhost:3000/users/1
def self.durl(url)
p x = url.sub('https://my-production-app.com', 'http://localhost:3000')
.sub('http://my-staging-app.com', 'http://localhost:3000')
`open #{x}`; x
end
# resource_from_url
# localhost:3000/users/42 => User.find(42)
def self.rfurl(url)
*, what, id = *%r{(.+)/([^/]+)/(\d+)}.match(url).to_a
what.singularize.camelcase.constantize.find(id)
end
def self.me
User.first
end
def self.set_pwd(user)
@encrypted_password ||= {}
@encrypted_password[user.id] = user.attributes['encrypted_password']
puts user.email
puts user.password = 'ErikTrapin11' # or email
user.save!
`open "http://localhost:3000/users/sign_in"`
end
def self.reset_pwd(user)
user.password = @encrypted_password[user.id]
user.save!
end
# distinct(Cart, :status) # => %w[pending checkout paid delivered]
def self.distinct(resource_class, field)
resource_class.distinct(field)
.map(&field.to_sym)
end
# map_ids - A table with ID and the eval'd code you pass
# mids(User.all, :email, :created_at)
def self.mids(items, *fields)
items.map do |item|
columns = fields.map { |f| eval "item.#{f}" }.presence || [item.to_s]
[item.id, *columns]
end
end
# map_children - A table with the data of the children
# mchs(User.all, 'tasks', 'name', 'description')
# => | User ID | Task Name | Task Description |
def self.mchs(items, has_many, *props)
puts "Item ID | #{props.join(' | ')}"
props = props.presence || %i[id name]
items.flat_map do |item|
item.send(has_many).map do |a|
p [item.id] + props.map { |f| a.send(f) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment