Permalink
Browse files

Merge pull request #4 from binarypaladin/rails-model-plugin

Rails model plugin
  • Loading branch information...
2 parents 47d0a58 + bff4b6b commit 778b51c5ab0cdc81189e06c498574805798eddcd @JonathanTron JonathanTron committed Jun 18, 2012
Showing with 48 additions and 10 deletions.
  1. +1 −1 lib/generators/sequel.rb
  2. +12 −9 lib/sequel-rails/railtie.rb
  3. +35 −0 lib/sequel/plugins/rails_extensions.rb
View
@@ -38,7 +38,7 @@ def self.all(klass)
end
def self.find(klass, params=nil)
- "#{klass}[#{params}]"
+ "#{klass}.find!(#{params})"
end
def self.build(klass, params=nil)
@@ -12,8 +12,8 @@
require 'action_controller/railtie'
require 'sequel-rails/setup'
-require "sequel-rails/railties/log_subscriber"
-require "sequel-rails/railties/i18n_support"
+require 'sequel-rails/railties/log_subscriber'
+require 'sequel-rails/railties/i18n_support'
module Rails
@@ -26,6 +26,12 @@ class Railtie < Rails::Railtie
config.app_generators.orm :sequel, :migration => true
config.rails_fancy_pants_logging = true
+ config.action_dispatch.rescue_responses.merge!(
+ 'Sequel::Plugins::RailsExtensions::ModelNotFound' => :not_found,
+ 'Sequel::ValidationFailed' => :unprocessable_entity,
+ 'Sequel::NoExistingObject' => :unprocessable_entity
+ )
+
rake_tasks do
load 'sequel-rails/railties/database.rake'
end
@@ -43,28 +49,25 @@ class Railtie < Rails::Railtie
end
# Expose database runtime to controller for logging.
- initializer "sequel.log_runtime" do |app|
+ initializer 'sequel.log_runtime' do |app|
setup_controller_runtime(app)
end
- initializer "sequel.connect" do |app|
+ initializer 'sequel.connect' do |app|
Rails::Sequel.setup(Rails.env)
end
# Run setup code after_initialize to make sure all config/initializers
# are in effect once we setup the connection. This is especially necessary
# for the cascaded adapter wrappers that need to be declared before setup.
-
config.after_initialize do |app|
::Sequel::Model.plugin :active_model
::Sequel::Model.plugin :validation_helpers
-
+ ::Sequel::Model.plugin :rails_extensions
::Sequel::Model.raise_on_save_failure = false
end
-
# Support overwriting crucial steps in subclasses
-
def configure_sequel(app)
app.config.sequel = Rails::Sequel::Configuration.for(
Rails.root, app.config.database_configuration
@@ -76,7 +79,7 @@ def setup_i18n_support(app)
end
def setup_controller_runtime(app)
- require "sequel-rails/railties/controller_runtime"
+ require 'sequel-rails/railties/controller_runtime'
ActionController::Base.send :include, Rails::Sequel::Railties::ControllerRuntime
end
@@ -0,0 +1,35 @@
+require 'sequel'
+
+module Sequel
+ module Plugins
+ # The RailsExtensions plugin adds a single class method to Sequel::Model in
+ # order to make its use in controllers a little more like ActiveRecord's.
+ # The +find!+ method is added which will raise an exception if no object is
+ # found. By adding the following code to a Railtie:
+ #
+ # config.action_dispatch.rescue_responses.merge!(
+ # 'SSequel::Plugins::RailsExtensions::ModelNotFound' => :not_found
+ # )
+ #
+ # Usage:
+ #
+ # # Apply plugin to all models:
+ # Sequel::Model.plugin :rails_extensions
+ #
+ # # Apply plugin to a single model:
+ # Album.plugin :rails_extensions
+ module RailsExtensions
+ class ModelNotFound < Sequel::Error
+ end
+
+ module ClassMethods
+ def find!(args)
+ m = self[args]
+ raise ModelNotFound, "Couldn't find #{self} matching #{args}." unless m
+ m
+ end
+ end
+
+ end
+ end
+end

0 comments on commit 778b51c

Please sign in to comment.