Skip to content

Commit

Permalink
Support API-only apps
Browse files Browse the repository at this point in the history
Add the modules and middleware needed.
  • Loading branch information
rosa committed Nov 13, 2024
1 parent d10c247 commit bc0aee0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
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
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: 1 addition & 1 deletion audits1984.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

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

Expand Down
7 changes: 7 additions & 0 deletions lib/audits1984/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ 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 Down

0 comments on commit bc0aee0

Please sign in to comment.