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

provide an after_connect configuration option to run a Proc immadiately ... #46

Merged
merged 1 commit into from
Sep 17, 2013
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ You can configure some options with the usual rails mechanism, in
config.sequel.load_database_tasks = false
```

Enabling plugins
================

If you want to enable plugins for all your models, you should use the
after_connect configuration option in `config/application.rb` (0.6.2+):

```ruby
config.sequel.after_connect = proc do
Sequel::Model.plugin :timestamps, update_on_create: true
end
```

This will ensure that these plugins are loaded before any Sequel models are
loaded. Loading plugins into `Sequel::Model` after subclasses are already
created is not supported by Sequel. You can also load extensions in
`after_connect` or perform any custom actions that you need.

Available sequel specific rake tasks
====================================

Expand Down
9 changes: 7 additions & 2 deletions lib/sequel_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ module SequelRails

mattr_accessor :configuration

def self.setup(environment)
def self.setup(environment, app)
config = configuration.environment_for(environment.to_s)
if config['url']
db = if config['url']
::Sequel.connect config['url'], config
else
::Sequel.connect config
end

callback = app.config.sequel.after_connect

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use configuration instead of the app.config mess?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it should simplify the code. Thanks for the code review!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot I've changed how the Configuration is structured after having merged this, there is no such thing anymore in the code.

Thanks anyway for reviewing it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome; just taking a peek at what changed before upgrading my apps :)

And sorry, my bad, if I'd have checked the current code I'd have noticed you fixed it already.

callback.call if callback.respond_to?(:call)

db
end

class Configuration < ActiveSupport::OrderedOptions
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel_rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Railtie < Rails::Railtie
end

initializer 'sequel.connect' do |app|
::SequelRails.setup ::Rails.env
::SequelRails.setup ::Rails.env, app
end

# Support overwriting crucial steps in subclasses
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel_rails/railties/database.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'sequel_rails/storage'
namespace :db do
def db_for_current_env
@db_for_current_env ||= {}
@db_for_current_env[Rails.env] ||= ::SequelRails.setup(Rails.env)
@db_for_current_env[Rails.env] ||= ::SequelRails.setup(Rails.env, Rails.application)
end

# desc "Raises an error if there are pending migrations"
Expand Down
16 changes: 15 additions & 1 deletion spec/lib/sequel_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@
SequelRails.stub(:jruby?).and_return is_jruby
end

subject { SequelRails.setup(environment) }
let(:sequel_config) { OpenStruct.new }
let(:app_config) { OpenStruct.new(sequel: sequel_config) }
let(:app) { OpenStruct.new(config: app_config) }
subject { SequelRails.setup(environment, app) }

context "after_connect hook" do
let(:environment) { 'development' }
let(:hook) { double }
let(:sequel_config) { OpenStruct.new(after_connect: hook) }

it "runs hook if provided" do
hook.should_receive(:call)
subject
end
end

shared_examples "max_connections" do
context "with max_connections config option" do
Expand Down