Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequel Support #414

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ pkg
#
# For MacOS:
#
#.DS_Store
.DS_Store
#
# For TextMate
#*.tmproj
tmtags
#
# For emacs:
#*~
#\#*
#.\#*
*~
\#*
.\#*
#
# For vim:
#*.swp
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ group :development do
gem 'capybara'
gem 'mongo_mapper'
gem 'mongoid', "~> 2.4.4"
gem 'sequel', '~> 3.40.0'
gem 'sequel_simple_callbacks', "~> 0.1.2"
end
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ GEM
ffi (~> 1.0)
multi_json (~> 1.0)
rubyzip
sequel (3.40.0)
sequel_simple_callbacks (0.1.2)
simplecov (0.6.1)
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
Expand Down Expand Up @@ -171,6 +173,8 @@ DEPENDENCIES
rails (>= 3.0.0)
rspec (~> 2.5.0)
rspec-rails (~> 2.5.0)
sequel (~> 3.40.0)
sequel_simple_callbacks (~> 0.1.2)
simplecov (>= 0.3.8)
sqlite3-ruby
timecop
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.8.2
10 changes: 8 additions & 2 deletions lib/sorcery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Adapters
autoload :ActiveRecord, 'sorcery/model/adapters/active_record'
autoload :Mongoid, 'sorcery/model/adapters/mongoid'
autoload :MongoMapper, 'sorcery/model/adapters/mongo_mapper'
autoload :Sequel, 'sorcery/model/adapters/sequel'
end
module Submodules
autoload :UserActivation, 'sorcery/model/submodules/user_activation'
Expand Down Expand Up @@ -65,7 +66,12 @@ module Internal
ActiveRecord::Base.send(:include, Sorcery::Model)
ActiveRecord::Base.send(:include, Sorcery::Model::Adapters::ActiveRecord)
end


if defined?(Sequel)
Sequel::Model.send(:include, Sorcery::Model)
Sequel::Model.send(:include, Sorcery::Model::Adapters::Sequel)
end

if defined?(Mongoid)
Mongoid::Document.module_eval do
included do
Expand All @@ -81,4 +87,4 @@ module Internal
end

require 'sorcery/engine' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
end
end
52 changes: 52 additions & 0 deletions lib/sorcery/model/adapters/sequel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Sorcery
module Model
module Adapters
module Sequel
def self.included(klass)
klass.extend ClassMethods
klass.send(:include, InstanceMethods)
# This is required for compatibility with ActiveRecord API for class hooks
# (i.e. before_create, after_create, etc.)
klass.send(:plugin, SequelSimpleCallbacks)
end

module InstanceMethods
def update_many_attributes(attrs)
attrs.each do |name, value|
self.send(:"#{name}=", value)
end
primary_key = self.class.primary_key
self.class.dataset.where(:"#{primary_key}" => self.send(:"#{primary_key}")).all{ |a| a.update(attrs) }
end

def update_single_attribute(name, value)
update_many_attributes(name => value)
end
end

module ClassMethods
def column_name(attribute)
return "LOWER(#{attribute})" if (@sorcery_config.downcase_username_before_authenticating)
return "#{attribute}"
end

def find_by_credentials(credentials)
sql = @sorcery_config.username_attribute_names.map{|attribute| column_name(attribute) + " = :login"}
dataset.where(sql.join(' OR '), :login => credentials[0]).first
end

def find_by_sorcery_token(token_attr_name, token)
dataset.where("#{token_attr_name} = ?", token).first
end

def get_current_users
config = sorcery_config
dataset.where("#{config.last_activity_at_attribute_name} IS NOT NULL") \
.where("#{config.last_logout_at_attribute_name} IS NULL OR #{config.last_activity_at_attribute_name} > #{config.last_logout_at_attribute_name}") \
.where("#{config.last_activity_at_attribute_name} > ? ", config.activity_timeout.seconds.ago.utc.to_s(:db))
end
end
end
end
end
end
7 changes: 7 additions & 0 deletions sorcery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Gem::Specification.new do |s|
"lib/sorcery/model/adapters/active_record.rb",
"lib/sorcery/model/adapters/mongo_mapper.rb",
"lib/sorcery/model/adapters/mongoid.rb",
"lib/sorcery/model/adapters/sequel.rb",
"lib/sorcery/model/submodules/activity_logging.rb",
"lib/sorcery/model/submodules/brute_force_protection.rb",
"lib/sorcery/model/submodules/external.rb",
Expand Down Expand Up @@ -329,6 +330,8 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<capybara>, [">= 0"])
s.add_development_dependency(%q<mongo_mapper>, [">= 0"])
s.add_development_dependency(%q<mongoid>, ["~> 2.4.4"])
s.add_development_dependency(%q<sequel>, ["~> 3.40.0"])
s.add_development_dependency(%q<sequel_simple_callbacks>, ["~> 0.1.2"])
else
s.add_dependency(%q<oauth>, ["~> 0.4.4"])
s.add_dependency(%q<oauth2>, ["~> 0.8.0"])
Expand All @@ -348,6 +351,8 @@ Gem::Specification.new do |s|
s.add_dependency(%q<capybara>, [">= 0"])
s.add_dependency(%q<mongo_mapper>, [">= 0"])
s.add_dependency(%q<mongoid>, ["~> 2.4.4"])
s.add_dependency(%q<sequel>, ["~> 3.40.0"])
s.add_dependency(%q<sequel_simple_callbacks>, ["~> 0.1.2"])
end
else
s.add_dependency(%q<oauth>, ["~> 0.4.4"])
Expand All @@ -368,6 +373,8 @@ Gem::Specification.new do |s|
s.add_dependency(%q<capybara>, [">= 0"])
s.add_dependency(%q<mongo_mapper>, [">= 0"])
s.add_dependency(%q<mongoid>, ["~> 2.4.4"])
s.add_dependency(%q<sequel>, ["~> 3.40.0"])
s.add_dependency(%q<sequel_simple_callbacks>, ["~> 0.1.2"])
end
end