Created
November 23, 2020 19:55
-
-
Save bjeanes/be8e0cedbc63206ee135c87b5b0a2c98 to your computer and use it in GitHub Desktop.
Make Rodauth routes, tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rodauth' | |
module Rodauth | |
# Clean up how we configure all of our tables, columns, etc., to use values which are more consistent with Rails than | |
# the defaults as well as use Rails mailers | |
Feature.define(:rails_conventions, :RailsConventions) do | |
depends :rails # rodauth-rails feature | |
def post_configure | |
# Rodauth uses dash-separated paths by default, so we'll make these underscores by default | |
methods.grep(/_route$/).grep_v(/^before_|^after_/).each do |route_conf| | |
if method(route_conf).owner != self.class | |
self.class.define_method(route_conf) { super().tr('-', '_') } | |
end | |
end | |
# (this one isn't actually a Rails convention but its of-a-kind with the other code here so perhaps this feature needs to be renamed) | |
# Adjust tables in-use to be under the `auth` schema: | |
methods.grep(/_table$/).each do |table_conf| | |
if method(table_conf).owner != self.class | |
self.class.define_method(table_conf) { Sequel[:auth][super()] } | |
end | |
end | |
# Make column names conform to Rails conventions, where possible: | |
methods.grep(/_column$/).each do |col_conf| | |
next if method(col_conf).owner == self.class | |
new_name = | |
case col_conf | |
when /deadline/ | |
:expires_at | |
when /email_last_sent/ | |
:email_last_sent_at | |
when /last_use/ | |
:last_used_at | |
else | |
next # don't change this column name | |
end | |
self.class.define_method(col_conf) { new_name } | |
end | |
# The defaults don't end in .js but javascript_include_tag adds '.js' suffix if ommitted | |
self.class.define_method(:webauthn_auth_js_route) { 'webauthn_auth.js' } | |
self.class.define_method(:webauthn_setup_js_route) { 'webauthn_setup.js' } | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment