Module: Rodauth::Rails

Defined in:
lib/rodauth/rails.rb,
lib/rodauth/rails/app.rb,
lib/rodauth/rails/auth.rb,
lib/rodauth/rails/test.rb,
lib/rodauth/rails/mailer.rb,
lib/rodauth/rails/railtie.rb,
lib/rodauth/rails/version.rb,
lib/rodauth/rails/middleware.rb,
lib/rodauth/rails/feature/base.rb,
lib/rodauth/rails/feature/csrf.rb,
lib/rodauth/rails/tasks/routes.rb,
lib/rodauth/rails/feature/email.rb,
lib/rodauth/rails/feature/render.rb,
lib/rodauth/rails/test/controller.rb,
lib/rodauth/rails/feature/callbacks.rb,
lib/rodauth/rails/controller_methods.rb,
lib/generators/rodauth/views_generator.rb,
lib/generators/rodauth/mailer_generator.rb,
lib/generators/rodauth/install_generator.rb,
lib/rodauth/rails/feature/instrumentation.rb,
lib/generators/rodauth/migration_generator.rb,
lib/rodauth/rails/feature/internal_request.rb

Defined Under Namespace

Modules: ControllerMethods, Feature, Generators, Tasks, Test Classes: App, Auth, Error, Mailer, Middleware, Railtie

Constant Summary collapse

VERSION =
"2.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.appObject



86
87
88
89
90
# File 'lib/rodauth/rails.rb', line 86

def app
  fail Rodauth::Rails::Error, "app was not configured" unless @app

  @app.constantize
end

.middleware=(value) ⇒ Object (writeonly)

Sets the attribute middleware

Parameters:

  • value

    the value to set the attribute middleware to.



84
85
86
# File 'lib/rodauth/rails.rb', line 84

def middleware=(value)
  @middleware = value
end

Class Method Details

.authenticate(name = nil, &condition) ⇒ Object

Routing constraint that requires authenticated account.



61
62
63
64
65
66
67
# File 'lib/rodauth/rails.rb', line 61

def authenticate(name = nil, &condition)
  lambda do |request|
    rodauth = request.env.fetch ["rodauth", *name].join(".")
    rodauth.
    condition.nil? || condition.call(rodauth)
  end
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



79
80
81
# File 'lib/rodauth/rails.rb', line 79

def configure
  yield self
end

.lib(**options, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rodauth/rails.rb', line 19

def lib(**options, &block)
  c = Class.new(Rodauth::Rails::App)
  c.configure(json: false, **options) do
    enable :internal_request
    instance_exec(&block)
  end
  c.freeze
  c.rodauth
end

.middleware?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rodauth/rails.rb', line 92

def middleware?
  @middleware
end

.model(name = nil, **options) ⇒ Object



56
57
58
# File 'lib/rodauth/rails.rb', line 56

def model(name = nil, **options)
  Rodauth::Model.new(app.rodauth!(name), **options)
end

.rodauth(name = nil, account: nil, **options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rodauth/rails.rb', line 29

def rodauth(name = nil, account: nil, **options)
  auth_class = app.rodauth!(name)

  unless auth_class.features.include?(:internal_request)
    fail Rodauth::Rails::Error, "Rodauth::Rails.rodauth requires internal_request feature to be enabled"
  end

  if 
    options[:account_id] = .id
  end

  instance = auth_class.internal_request_eval(options) do
    if defined?(ActiveRecord::Base) && .is_a?(ActiveRecord::Base)
      @account = .attributes_before_type_cast.symbolize_keys
    elsif defined?(Sequel::Model) && .is_a?(Sequel::Model)
      @account = .values
    end
    self
  end

  # clean up inspect output
  instance.remove_instance_variable(:@internal_request_block)
  instance.remove_instance_variable(:@internal_request_return_value)

  instance
end

Instance Method Details

#secret_key_baseObject



70
71
72
# File 'lib/rodauth/rails.rb', line 70

def secret_key_base
  ::Rails.application.secret_key_base
end