Module: MerbAuth

Defined in:
lib/merb-auth.rb,
lib/merb-auth/initializer.rb,
app/controllers/application.rb,
lib/merb-auth/adapters/common.rb,
lib/merb-auth/adapters/sequel/map.rb,
lib/merb-auth/adapters/sequel/model.rb,
lib/merb-auth/controller/controller.rb,
lib/merb-auth/controller/users_base.rb,
lib/merb-auth/adapters/datamapper/map.rb,
lib/merb-auth/controller/sessions_base.rb,
lib/merb-auth/adapters/activerecord/map.rb,
lib/merb-auth/adapters/datamapper/model.rb,
lib/merb-auth/adapters/activerecord/model.rb

Overview

All Slice code is expected to be namespaced inside a module

Defined Under Namespace

Modules: Adapter, Controller Classes: Application, Sessions, UserMailer, Users

Class Method Summary collapse

Class Method Details

.activateObject

Activation hook - runs after AfterAppLoads BootLoader



94
95
96
97
98
99
100
# File 'lib/merb-auth.rb', line 94

def self.activate
  # Make the aliases for current stuff
   Merb::Controller.module_eval do
    alias_method :"current_#{MA[:single_resource]}", :current_ma_user
    alias_method :"current_#{MA[:single_resource]}=", :current_ma_user=
  end
end

.adaptersHash

Returns A hash of the adapters.

Returns:

  • (Hash)

    A hash of the adapters.



17
18
19
# File 'lib/merb-auth/initializer.rb', line 17

def self.adapters
  @_adapters ||= Hash.new{|h,k| h[k] = {}}
end

.add_routes(&blk) ⇒ Object



53
54
55
# File 'lib/merb-auth.rb', line 53

def self.add_routes(&blk)
  custom_routes << blk
end

.clear_adapter_list!Object

Clears the currently registered adapter list.



4
5
6
# File 'lib/merb-auth/initializer.rb', line 4

def self.clear_adapter_list!
  @_adapters = nil
end

.custom_routesObject



57
58
59
# File 'lib/merb-auth.rb', line 57

def self.custom_routes
  @custom_routes ||= []
end

.deactivateObject

Deactivation hook - triggered by Merb::Slices#deactivate



103
104
# File 'lib/merb-auth.rb', line 103

def self.deactivate
end

.initObject

Initialization hook - runs before AfterAppLoads BootLoader



89
90
91
# File 'lib/merb-auth.rb', line 89

def self.init  
  
end

.load_adapter!(adapter = nil) ⇒ Object

Loads the adapter provided, or if not provided, the adapter set in the slices config

Parameters:

  • adapter (Symbol | String) (defaults to: nil)

    The name of the adapter to load. This must be registered

Raises:

  • (RuntimeError)

    Raises an error if the adapter is not registered.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/merb-auth/initializer.rb', line 25

def self.load_adapter!(adapter = nil)
  adapter ||= self.config[:adapter] || Merb.orm_generator_scope
  raise "MerbAuth: No Adapter Specified" if adapter.nil? || adapter.blank?
  
  # Check that the adapter is registered
  raise "MerbAuth: Adapter Not Registered - #{adapter}" unless adapters.keys.include?(adapter.to_sym)
  
  if Merb.env?(:test)
    load adapters[adapter.to_sym][:path] / "init.rb"
  else
    require adapters[adapter.to_sym][:path] / "init"
  end
end

.load_plugins!Object



39
40
41
42
43
44
45
# File 'lib/merb-auth/initializer.rb', line 39

def self.load_plugins!
  self.plugins.each do |label, file|
    Merb.logger.info "Loading MerbAuth Plugin - #{label}"
    load file
  end
  MA.setup_custom_routes!
end

.loadedObject

Stub classes loaded hook - runs before LoadClasses BootLoader right after a slice’s classes have been loaded internally



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/merb-auth.rb', line 75

def self.loaded
  # Setup the login field to use
  MA[:login_field] = (MA[:login_field] || :email).to_sym
  
  MA.load_adapter!
  
  Merb::Controller.send(:include, MA::Controller::Helpers)
  # sends the methoods to the controllers as an include so that other mixins can
  # overwrite them
  MA::Users.send(     :include, MA::Controller::UsersBase)
  MA::Sessions.send(  :include, MA::Controller::SessionsBase)
end

.pluginsObject



49
50
51
# File 'lib/merb-auth.rb', line 49

def self.plugins
  @@plugins ||= {}
end

.register_adapter(name, path, opts = {}) ⇒ Object

Registers an adapter.

Parameters:

  • name (Symbol)

    is the name of the adapter. Supported adapters are :datamapper and :activerecord

  • path (String)

    is the path to the adapter. The adapter path directory should include an init.rb file

  • opts (Hash) (defaults to: {})

    an options hash



12
13
14
# File 'lib/merb-auth/initializer.rb', line 12

def self.register_adapter(name, path, opts = {})
  adapters[name.to_sym] = opts.merge!(:path => path)
end

.setup_custom_routes!Object



61
62
63
64
65
66
# File 'lib/merb-auth.rb', line 61

def self.setup_custom_routes!
  Merb.logger.info "Adding custom routes"
  custom_routes.each do |r|
    r.call(MA[:router_scope])
  end
end

.setup_router(scope) ⇒ Object

Setup routes inside the host application

Parameters:

  • scope (Merb::Router::Behaviour)

    Routes will be added within this scope (namespace). In fact, any router behaviour is a valid namespace, so you can attach routes at any level of your router setup.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/merb-auth.rb', line 112

def self.setup_router(scope)
  MA[:router_scope] = scope # Hangs onto the scope for the plugins which are loaded after the routes are setup
  
  MA.setup_custom_routes!
  
  plural_model_path = MA[:route_path_model] || MA[:plural_resource] 
  plural_model_path ||= "User".snake_case.singularize.pluralize
  plural_model_path = plural_model_path.to_s.match(%r{^/?(.*?)/?$})[1]
  single_model_name = plural_model_path.singularize
  
  plural_session_path = MA[:route_path_session] || "sessions"
  plural_session_path = plural_session_path.to_s.match(%r{^/?(.*?)/?$})[1]
  single_session_name = plural_session_path.singularize
  
  activation_name = (MA[:single_resource].to_s << "_activation").to_sym
  
  MA[:routes] = {:user => {}}
  MA[:routes][:user][:new]       ||= :"new_#{single_model_name}"
  MA[:routes][:user][:show]      ||= :"#{single_model_name}"
  MA[:routes][:user][:edit]      ||= :"edit_#{single_model_name}"
  MA[:routes][:user][:delete]    ||= :"delete_#{single_model_name}"
  MA[:routes][:user][:index]     ||= :"#{plural_model_path}"
  MA[:routes][:user][:activate]  ||= :"#{single_model_name}_activation"
      
  # Setup the model path
  scope.to(:controller => "Users") do |c|
    c.match("/#{plural_model_path}") do |u|
      # setup the named routes          
      u.match("/new",             :method => :get ).to( :action => "new"     ).name(MA[:routes][:user][:new])
      u.match("/:id",             :method => :get ).to( :action => "show"    ).name(MA[:routes][:user][:show])
      u.match("/:id/edit",        :method => :get ).to( :action => "edit"    ).name(MA[:routes][:user][:edit])
      u.match("/:id/delete",      :method => :get ).to( :action => "delete"  ).name(MA[:routes][:user][:delete])
      u.match("/",                :method => :get ).to( :action => "index"   ).name(MA[:routes][:user][:index])
      u.match("/activate/:activation_code", :method => :get).to( :action => "activate").name(MA[:routes][:user][:activate])
      
      # Make the anonymous routes
      u.match(%r{(/|/index)?(\.:format)?$},  :method => :get    ).to( :action => "index")
      u.match(%r{/new$},                     :method => :get    ).to( :action => "new")
      u.match(%r{/:id(\.:format)?$},         :method => :get    ).to( :action => "show")
      u.match(%r{/:id/edit$},                :method => :get    ).to( :action => "edit")
      u.match(%r{/:id/delete$},              :method => :get    ).to( :action => "delete")
      u.match(%r{/?(\.:format)?$},           :method => :post   ).to( :action => "create")      
      u.match(%r{/:id(\.:format)?$},         :method => :put    ).to( :action => "update")
      u.match(%r{/:id(\.:format)?$},         :method => :delete ).to( :action => "destroy")
    end
  end
  
  scope.match("/signup").to(:controller => "Users",    :action => "new"    ).name(:signup)
  scope.match("/login" ).to(:controller => "sessions", :action => "create" ).name(:login)
  scope.match("/logout").to(:controller => "sessions", :action => "destroy").name(:logout)
  scope.match("/registered").to(:controller => "users", :action => "registered").name(:registered)
  scope.match("/activated").to(:controller => "users", :action => "activated").name(:activated)
end