Skip to content

Commit

Permalink
Adapt mapping to store hash of models that are used, refs socialcast#23
Browse files Browse the repository at this point in the history
  • Loading branch information
BRMatt committed Dec 4, 2011
1 parent 2fc252b commit a212d27
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/devise/oauth2_providable/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Engine < Rails::Engine
config.devise_oauth2_providable.access_token_expires_in = 15.minutes
config.devise_oauth2_providable.refresh_token_expires_in = 1.month
config.devise_oauth2_providable.authorization_code_expires_in = 1.minute
config.devise_oauth2_providable.scope_settings = {}

engine_name 'oauth2'
isolate_namespace Devise::Oauth2Providable
Expand Down
22 changes: 21 additions & 1 deletion lib/devise/oauth2_providable/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Oauth2Providable
#
# mapping.prefix
class Mapping
attr_reader :scope_name, :path_prefix, :controllers
attr_reader :scope_name, :path_prefix, :controllers, :models

class << self
def default_controllers
Expand All @@ -26,12 +26,22 @@ def default_controllers
:tokens => "devise/oauth2_providable/tokens"
}
end

def default_models
{
:access_token => 'Devise::Oauth2Providable::AccessToken',
:client => 'Devise::Oauth2Providable::Client',
:refresh_token => 'Devise::Oauth2Providable::RefreshToken',
:user => 'User',
}
end
end

def initialize(scope_name, options = {})
@scope_name = (options[:scope_name] || scope_name.to_s.singularize).to_sym
@path_prefix = options[:path_prefix]
@controllers = self.select_controllers(options)
@models = self.select_models(options)
end

# Returns the devise scope mapping object associated with this oauth endpoint
Expand All @@ -43,6 +53,16 @@ def devise_scope
def select_controllers(options)
self.class.default_controllers.merge(options[:controllers] || {})
end

def select_models(options)
models = self.class.default_models.merge(self.scope_config[:models] || {})

models.each { |key,value| models[key] = value.constantize }
end

def scope_config
::Rails.application.config.devise_oauth2_providable.scope_settings[@scope_name] || {}
end
end
end
end
41 changes: 41 additions & 0 deletions spec/lib/devise/oauth2_providable/mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@
end
end

describe "#models" do
subject { mapping_class.new(:users) }

context "when no model overrides exist for the scope" do
before(:each) do
::Rails.application.config.
stub_chain(:devise_oauth2_providable, :scope_settings).
and_return({})
end

it "the defaults are used" do
subject.models.should eql({
:access_token => Devise::Oauth2Providable::AccessToken,
:client => Devise::Oauth2Providable::Client,
:refresh_token => Devise::Oauth2Providable::RefreshToken,
:user => User
})
end
end

context "when model overrides are specified" do
class MyTotallyAwesomeUser
end

before(:each) do
::Rails.application.config.
stub_chain(:devise_oauth2_providable, :scope_settings).
and_return({:user => {:models => {:user => 'MyTotallyAwesomeUser'}}})
end

it "uses the overrides instead" do
subject.models.should eql({
:access_token => Devise::Oauth2Providable::AccessToken,
:client => Devise::Oauth2Providable::Client,
:refresh_token => Devise::Oauth2Providable::RefreshToken,
:user => MyTotallyAwesomeUser
})
end
end
end

describe "#path_prefix" do
context "is specified" do
subject { mapping = mapping_class.new(:users, {:path_prefix => "member"}) }
Expand Down

0 comments on commit a212d27

Please sign in to comment.