Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full rewrite #1

Merged
merged 19 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
PATH
remote: .
specs:
dev_toolbar (0.1.0)
rails (~> 7.0)

GEM
remote: https://rubygems.org/
specs:
actioncable (7.1.3.3)
actionpack (= 7.1.3.3)
activesupport (= 7.1.3.3)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.1.3.3)
actionpack (= 7.1.3.3)
activejob (= 7.1.3.3)
activerecord (= 7.1.3.3)
activestorage (= 7.1.3.3)
activesupport (= 7.1.3.3)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.1.3.3)
actionpack (= 7.1.3.3)
actionview (= 7.1.3.3)
activejob (= 7.1.3.3)
activesupport (= 7.1.3.3)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.2)
actionpack (7.1.3.3)
actionview (= 7.1.3.3)
activesupport (= 7.1.3.3)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3.3)
actionpack (= 7.1.3.3)
activerecord (= 7.1.3.3)
activestorage (= 7.1.3.3)
activesupport (= 7.1.3.3)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.1.3.3)
activesupport (= 7.1.3.3)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.1.3.3)
activesupport (= 7.1.3.3)
globalid (>= 0.3.6)
activemodel (7.1.3.3)
activesupport (= 7.1.3.3)
activerecord (7.1.3.3)
activemodel (= 7.1.3.3)
activesupport (= 7.1.3.3)
timeout (>= 0.4.0)
activestorage (7.1.3.3)
actionpack (= 7.1.3.3)
activejob (= 7.1.3.3)
activerecord (= 7.1.3.3)
activesupport (= 7.1.3.3)
marcel (~> 1.0)
activesupport (7.1.3.3)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.2.4)
concurrent-ruby (1.3.1)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
drb (2.2.1)
erubi (1.12.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.4)
mini_mime (1.1.5)
minitest (5.23.1)
mutex_m (0.2.0)
net-imap (0.4.11)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.5-x86_64-darwin)
racc (~> 1.4)
psych (5.1.2)
stringio
racc (1.8.0)
rack (3.0.11)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails (7.1.3.3)
actioncable (= 7.1.3.3)
actionmailbox (= 7.1.3.3)
actionmailer (= 7.1.3.3)
actionpack (= 7.1.3.3)
actiontext (= 7.1.3.3)
actionview (= 7.1.3.3)
activejob (= 7.1.3.3)
activemodel (= 7.1.3.3)
activerecord (= 7.1.3.3)
activestorage (= 7.1.3.3)
activesupport (= 7.1.3.3)
bundler (>= 1.15.0)
railties (= 7.1.3.3)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3.3)
actionpack (= 7.1.3.3)
activesupport (= 7.1.3.3)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
reline (0.5.8)
io-console (~> 0.5)
stringio (3.1.0)
thor (1.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
webrick (1.8.1)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.15)

PLATFORMS
x86_64-darwin-22

DEPENDENCIES
bundler (~> 2.0)
dev_toolbar!
rake (~> 13.0)

BUNDLED WITH
2.4.12
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2024 Ben Purinton
Copyright (c) 2024 FIRSTDRAFT, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
# DevToolbar

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dev_toolbar`. To experiment with that code, run `bin/console` for an interactive prompt.
First Draft DevToolbar for `appdev-projects` beginner ergonomics in Rails applications.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.

Install the gem and add to the application's Gemfile by executing:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG

If bundler is not being used to manage dependencies, install the gem by executing:
Add this line to your application's Gemfile:

$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
```ruby
group :development do
gem "dev_toolbar", git: "https://github.com/firstdraft/dev_toolbar.git", branch: "bp-make-it-work"
end
```

## Usage
Then execute:

TODO: Write usage instructions here
```bash
bundle install
```

## Development
Then, add a configuration file with the names and routes you want links for:

After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
```rb
# config/initializers/dev_toolbar.rb
DevToolbar.configure do |config|
config.links = [
{ name: "Routes", path: "/rails/info/routes" },
{ name: "Database", path: "/rails/db" }, # rails_db gem must be installed
{ name: "Data Model", path: "/erd.png" }, # erd.png must be in public/ folder
# etc.
]
end
```

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
These routes will now appear on every page in your app while in development.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dev_toolbar.
Bug reports and pull requests are welcome on GitHub at https://github.com/firstdraft/dev_toolbar.

## License

Expand Down
11 changes: 0 additions & 11 deletions bin/console

This file was deleted.

8 changes: 0 additions & 8 deletions bin/setup

This file was deleted.

29 changes: 10 additions & 19 deletions dev_toolbar.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@ Gem::Specification.new do |spec|
spec.name = "dev_toolbar"
spec.version = DevToolbar::VERSION
spec.authors = ["Ben Purinton"]
spec.email = ["ben.purinton@gmail.com"]
spec.email = ["ben@firstdraft.com"]

spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = "A development toolbar for Rails applications."
spec.description = "A sticky toolbar for Rails applications in development mode"
spec.homepage = "https://github.com/firstdraft"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"

spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
end
end
spec.metadata["source_code_uri"] = "https://github.com/firstdraft/dev_toolbar"

spec.files = Dir["lib/**/*", "app/**/*"]
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_dependency "rails", "~> 7.0"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 13.0"
end
13 changes: 12 additions & 1 deletion lib/dev_toolbar.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# frozen_string_literal: true

require_relative "dev_toolbar/version"
require_relative "dev_toolbar/railtie"
require_relative "dev_toolbar/middleware"
require_relative "dev_toolbar/configuration"

module DevToolbar
class Error < StandardError; end
# Your code goes here...

class << self
attr_accessor :configuration

def configure
self.configuration ||= Configuration.new
yield(configuration)
end
end
end
9 changes: 9 additions & 0 deletions lib/dev_toolbar/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module DevToolbar
class Configuration
attr_accessor :links

def initialize
@links = []
end
end
end
41 changes: 41 additions & 0 deletions lib/dev_toolbar/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module DevToolbar
class Middleware
def initialize(app)
@app = app
end

def call(env)
status, headers, response = @app.call(env)

if Rails.env.development? && headers["Content-Type"]&.include?("text/html")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying the response body directly can lead to issues with streaming responses or responses that are already gzipped. Consider handling different response types more robustly, possibly by using a more sophisticated approach to modify the response only if it's a complete HTML document and not encoded in a way that would break by simple string substitution.

response_body = response.body
toolbar_html = <<-HTML
<div id="dev-toolbar" style="position: fixed; right: 0; top: 0; background: #333; color: #fff; padding: 0.5rem; z-index: 1000;">
#{toolbar_links}
</div>
<style>
#dev-toolbar a {
color: #fff;
margin-right: 0.5rem;
}
</style>
HTML

response_body.sub!('</body>', "#{toolbar_html}</body>")
headers["Content-Length"] = response_body.bytesize.to_s

response = [response_body]
end

[status, headers, response]
end

private

def toolbar_links
DevToolbar.configuration.links.map do |link|
"<a href='#{link[:path]}' target='_blank'>#{link[:name]}</a>"
end.join(' ')
end
end
end
Loading