Skip to content

Commit

Permalink
Add dedicated configuration for skipping Tilt
Browse files Browse the repository at this point in the history
Having to pass `render: false` when registering Rodauth configurations can be a
bit awkward when the developer isn't otherwise exposed with Rodauth being a Roda
plugin. Moreover, it can be cumbersome, because it needs to be passed for every
Rodauth configuration.

Having dedicated configuration makes it much more straighforward to skip Tilt,
and this way it doesn't get mixed with things that actually affect
authentication behavior or calling Rodauth methods.
  • Loading branch information
janko committed Dec 20, 2024
1 parent c118003 commit afae9d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,17 @@ end
Rails.configuration.middleware.insert_before AnotherMiddleware, Rodauth::Rails::Middleware
```

### Skipping Tilt

Rodauth uses the [Tilt] gem to render built-in view & email templates. If you don't want to have Tilt as a dependency, you can disable it, provided that you've imported all view & email templates into your app:

```rb
# config/initializers/rodauth.rb
Rodauth::Rails.configure do |config|
config.tilt = false # skip loading Tilt gem
end
```

## How it works

### Rack middleware
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rodauth::Rails.configure do |config|
config.app = "RodauthApp"
# config.middleware = false # disable auto-insertion of Rodauth middleware
# config.tilt = false # skip loading Tilt gem for rendering built-in templates
end
12 changes: 9 additions & 3 deletions lib/rodauth/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class Error < StandardError
autoload :Auth, "rodauth/rails/auth"
autoload :Mailer, "rodauth/rails/mailer"

@app = nil
@middleware = true

class << self
def lib(**options, &block)
c = Class.new(Rodauth::Rails::App)
Expand Down Expand Up @@ -80,8 +77,13 @@ def configure
yield self
end

@app = nil
@middleware = true
@tilt = true

attr_writer :app
attr_writer :middleware
attr_writer :tilt

def app
fail Rodauth::Rails::Error, "app was not configured" unless @app
Expand All @@ -92,6 +94,10 @@ def app
def middleware?
@middleware
end

def tilt?
@tilt
end
end
end
end
6 changes: 3 additions & 3 deletions lib/rodauth/rails/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class App < Roda
plugin :hooks
plugin :pass

def self.configure(*args, **options, &block)
def self.configure(*args, render: Rodauth::Rails.tilt?, **options, &block)
auth_class = args.shift if args[0].is_a?(Class)
auth_class ||= Class.new(Rodauth::Rails::Auth)
name = args.shift if args[0].is_a?(Symbol)

fail ArgumentError, "need to pass optional Rodauth::Auth subclass and optional configuration name" if args.any?

# we'll render Rodauth's built-in view templates within Rails layouts
plugin :render, layout: false unless options[:render] == false
plugin :render, layout: false unless render == false

plugin :rodauth, auth_class: auth_class, name: name, csrf: false, flash: false, json: true, **options, &block
plugin :rodauth, auth_class: auth_class, name: name, csrf: false, flash: false, json: true, render: render, **options, &block

# we need to do it after request methods from rodauth have been included
self::RodaRequest.include RequestMethods
Expand Down

0 comments on commit afae9d0

Please sign in to comment.