diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..c53bc1c --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/LICENSE.txt b/LICENSE.txt index b96ca9c..366d094 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -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 diff --git a/README.md b/README.md index 8e1171f..e37d8d8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/console b/bin/console deleted file mode 100755 index 7d197b8..0000000 --- a/bin/console +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -require "bundler/setup" -require "dev_toolbar" - -# You can add fixtures and/or initialization code here to make experimenting -# with your gem easier. You can also use a different console, if you like. - -require "irb" -IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup deleted file mode 100755 index dce67d8..0000000 --- a/bin/setup +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -IFS=$'\n\t' -set -vx - -bundle install - -# Do any other automated setup that you need to do here diff --git a/dev_toolbar.gemspec b/dev_toolbar.gemspec index 6988c52..7996ff3 100644 --- a/dev_toolbar.gemspec +++ b/dev_toolbar.gemspec @@ -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 diff --git a/lib/dev_toolbar.rb b/lib/dev_toolbar.rb index 161bfd5..f861e29 100644 --- a/lib/dev_toolbar.rb +++ b/lib/dev_toolbar.rb @@ -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 diff --git a/lib/dev_toolbar/configuration.rb b/lib/dev_toolbar/configuration.rb new file mode 100644 index 0000000..26d3c7c --- /dev/null +++ b/lib/dev_toolbar/configuration.rb @@ -0,0 +1,9 @@ +module DevToolbar + class Configuration + attr_accessor :links + + def initialize + @links = [] + end + end +end diff --git a/lib/dev_toolbar/middleware.rb b/lib/dev_toolbar/middleware.rb new file mode 100644 index 0000000..7e96c5d --- /dev/null +++ b/lib/dev_toolbar/middleware.rb @@ -0,0 +1,51 @@ +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") + response_body = response.body + toolbar_html = <<-HTML +
+ #{toolbar_links} +
+ + HTML + + response_body.sub!('', "#{toolbar_html}") + 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| + "#{link[:name]}" + end.join(' ') + end + end +end diff --git a/lib/dev_toolbar/railtie.rb b/lib/dev_toolbar/railtie.rb new file mode 100644 index 0000000..d6f03ad --- /dev/null +++ b/lib/dev_toolbar/railtie.rb @@ -0,0 +1,7 @@ +module DevToolbar + class Railtie < Rails::Railtie + initializer "dev_toolbar.middleware" do |app| + app.middleware.use DevToolbar::Middleware if Rails.env.development? + end + end +end diff --git a/sig/dev_toolbar.rbs b/sig/dev_toolbar.rbs deleted file mode 100644 index 2f9723a..0000000 --- a/sig/dev_toolbar.rbs +++ /dev/null @@ -1,4 +0,0 @@ -module DevToolbar - VERSION: String - # See the writing guide of rbs: https://github.com/ruby/rbs#guides -end