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

Support API-only apps and other apps not using Sprockets or Propshaft #61

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Setup Ruby and install gems
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.3
ruby-version: 3.3.4
bundler-cache: true
- name: Run rubocop
run: |
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
- name: Setup Ruby and install gems
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.3
ruby-version: 3.3.4
bundler-cache: true
- name: Setup test database
env:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.3
3.3.4
22 changes: 0 additions & 22 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,3 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

# Specify your gem's dependencies in audits1984.gemspec.
gemspec

gem 'sqlite3'
gem 'pg'
gem 'mysql2'

group :development do
gem 'rubocop-rails-omakase', require: false
end

group :test do
gem 'minitest'

gem 'rails'
gem 'sprockets-rails'
gem 'puma'

gem 'capybara'
gem 'cuprite'
end

# To use a debugger
# gem 'byebug', group: [:development, :test]
29 changes: 19 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
audits1984 (0.1.7)
console1984
importmap-rails (>= 1.2.1)
rinku
rouge
turbo-rails
Expand Down Expand Up @@ -99,6 +100,9 @@ GEM
capybara (~> 3.0)
ferrum (~> 0.14.0)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
erubi (1.12.0)
ferrum (0.14)
addressable (~> 2.5)
Expand All @@ -109,6 +113,10 @@ GEM
activesupport (>= 5.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.3)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
io-console (0.7.2)
irb (1.14.0)
rdoc (>= 4.0.0)
Expand All @@ -133,6 +141,7 @@ GEM
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-smtp (0.4.0)
Expand All @@ -145,6 +154,11 @@ GEM
parser (3.2.2.1)
ast (~> 2.4.1)
pg (1.5.3)
propshaft (1.1.0)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
rack
railties (>= 7.0.0)
psych (5.1.2)
stringio
public_suffix (5.0.1)
Expand Down Expand Up @@ -220,13 +234,6 @@ GEM
rubocop-performance
rubocop-rails
ruby-progressbar (1.13.0)
sprockets (4.2.0)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.6.3)
mini_portile2 (~> 2.8.0)
stringio (3.1.1)
Expand Down Expand Up @@ -257,14 +264,16 @@ DEPENDENCIES
audits1984!
capybara
cuprite
debug
minitest
mysql2
pg
propshaft
puma
rails
rubocop (~> 1.52.0)
rubocop-performance
rubocop-rails-omakase
sprockets-rails
sqlite3

BUNDLED WITH
2.2.33
2.5.23
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ Mount the engine in your `routes.rb`:
mount Audits1984::Engine => "/console"
```

### API-only apps or apps using `vite_rails` and other asset pipelines outside Rails

If you want to use this gem with an [API-only Rails app](https://guides.rubyonrails.org/api_app.html) or an app that's using `vite_ruby`/`vite_rails`, or some other custom asset pipeline different from Sprockets and Propshaft, you need just one more thing: configure an asset pipeline so you can serve the JavaScript and CSS included in this gem. We recommend to use [`Propshaft`](https://github.com/rails/propshaft). You simply need to add this line to your application's Gemfile:

```ruby
gem "propshaft"
```

Then execute
```bash
$ bundle install
```

And you should be ready to go.

### Authenticate auditors

By default, the library controllers will inherit from the host application's `ApplicationController`. To authenticate auditors, you need to implement a method `#find_current_auditor` in your `ApplicationController`. This method must return a record representing the auditing user. It can be any model but it has to respond to `#name`.
Expand Down
3 changes: 0 additions & 3 deletions app/assets/javascripts/audits1984/application.js

This file was deleted.

7 changes: 7 additions & 0 deletions app/controllers/audits1984/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module Audits1984
class ApplicationController < Audits1984.base_controller_class.constantize
ActionController::Base::MODULES.each do |mod|
include mod unless self < mod
end

before_action :authenticate_auditor

layout "audits1984/application"

helper Audits1984::ApplicationHelper unless self < Audits1984::ApplicationHelper
helper Importmap::ImportmapTagsHelper unless self < Importmap::ImportmapTagsHelper

private
def authenticate_auditor
unless respond_to?(:find_current_auditor, true)
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/audits1984/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
8 changes: 3 additions & 5 deletions app/views/audits1984/sessions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<%= render "audits1984/sessions/filter", filtered_sessions: @filtered_sessions %>
<%= render "audits1984/sessions/summary", sessions: @sessions %>

<turbo-frame id="sessions">
<div class="sessions">
<%= render partial: "audits1984/sessions/session", collection: @sessions, cached: true %>
</div>
</turbo-frame>
<div class="sessions">
<%= render partial: "audits1984/sessions/session", collection: @sessions, cached: true %>
</div>
4 changes: 2 additions & 2 deletions app/views/layouts/audits1984/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<meta charset="utf-8">
<title><%= @title || "Audits1984" %></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%= stylesheet_link_tag "audits1984/bulma.min" %>

<%= csrf_meta_tags %>

<%= javascript_include_tag "audits1984/application", nonce: true, data: { turbo_track: :reload } %>
<%= stylesheet_link_tag "audits1984/bulma.min" %>
<%= javascript_importmap_tags "application", importmap: Audits1984.importmap %>
<%= stylesheet_link_tag "audits1984/application", media: :all, data: { turbo_track: :reload } %>
</head>

Expand Down
14 changes: 14 additions & 0 deletions audits1984.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ Gem::Specification.new do |spec|
spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

spec.add_dependency "rouge"
spec.add_dependency "importmap-rails", ">= 1.2.1"
spec.add_dependency "turbo-rails"
spec.add_dependency "rinku"
spec.add_dependency "console1984"

spec.add_development_dependency "sqlite3"
spec.add_development_dependency "pg"
spec.add_development_dependency "mysql2"
spec.add_development_dependency "debug"
spec.add_development_dependency "rubocop", "~> 1.52.0"
spec.add_development_dependency "rubocop-performance"
spec.add_development_dependency "rubocop-rails-omakase"
spec.add_development_dependency "propshaft"
spec.add_development_dependency "puma"
spec.add_development_dependency "minitest"
spec.add_development_dependency "capybara"
spec.add_development_dependency "cuprite"
end
10 changes: 8 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
set -eu
cd "$(dirname "${BASH_SOURCE[0]}")"

docker-compose up -d --remove-orphans
docker-compose ps
if docker compose version &> /dev/null; then
DOCKER_COMPOSE_CMD="docker compose"
else
DOCKER_COMPOSE_CMD="docker-compose"
fi

$DOCKER_COMPOSE_CMD up -d --remove-orphans
$DOCKER_COMPOSE_CMD ps

bundle

Expand Down
2 changes: 2 additions & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pin "application", to: "audits1984/application.js", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ volumes:

services:
mysql:
image: percona:5.7.22
image: mysql:8.0.31
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
volumes:
- db:/var/lib/mysql
ports: [ "127.0.0.1:33307:3306" ]
ports: [ "127.0.0.1:33060:3306" ]
postgres:
image: postgres:13.4
environment:
Expand Down
1 change: 1 addition & 0 deletions lib/audits1984.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ module Audits1984
mattr_accessor :auditor_class, default: "::User"
mattr_accessor :auditor_name_attribute, default: :name
mattr_accessor :base_controller_class, default: "::ApplicationController"
mattr_accessor :importmap, default: Importmap::Map.new
end
24 changes: 22 additions & 2 deletions lib/audits1984/engine.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
require "console1984"
require 'rinku'
require "importmap-rails"
require "turbo-rails"
require "rinku"

module Audits1984
class Engine < ::Rails::Engine
isolate_namespace Audits1984

initializer "audits1984.middleware" do |app|
if app.config.api_only
app.middleware.use ActionDispatch::Flash
app.middleware.use ::Rack::MethodOverride
end
end

config.audits1984 = ActiveSupport::OrderedOptions.new

initializer "audits1984.config" do
Expand All @@ -19,8 +28,19 @@ class Engine < ::Rails::Engine
end
end

initializer "audits1984.assets.precompile" do |app|
initializer "audits1984.assets" do |app|
app.config.assets.paths << root.join("app/assets/stylesheets")
app.config.assets.paths << root.join("app/javascript")
app.config.assets.precompile << "audits1984_manifest.js"
end

initializer "audits1984.importmap", after: "importmap" do |app|
Audits1984.importmap.draw(root.join("config/importmap.rb"))
Audits1984.importmap.cache_sweeper(watches: root.join("app/javascript"))

ActiveSupport.on_load(:action_controller_base) do
before_action { Audits1984.importmap.cache_sweeper.execute_if_updated }
end
end
end
end
3 changes: 0 additions & 3 deletions test/dummy/app/assets/config/manifest.js

This file was deleted.

15 changes: 0 additions & 15 deletions test/dummy/app/javascript/packs/application.js

This file was deleted.

1 change: 1 addition & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative "boot"

require "rails/all"
require "propshaft"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down
Loading