Skip to content

Commit

Permalink
Add correclty ActiveRecord Middleware. Fix #792
Browse files Browse the repository at this point in the history
  • Loading branch information
DAddYE committed Feb 22, 2012
1 parent 4a09c0e commit 84f050f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions padrino-admin/lib/padrino-admin/generators/admin_app.rb
Expand Up @@ -59,6 +59,7 @@ def create_admin
directory "templates/assets", destination_root("public", "admin")
template "templates/app.rb.tt", destination_root("admin/app.rb")
append_file destination_root("config/apps.rb"), "\nPadrino.mount(\"Admin\").to(\"/admin\")"
insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement', 'admin'

This comment has been minimized.

Copy link
@nesquena

nesquena Feb 23, 2012

Member

Shouldn't this only happen if 'activerecord' is being used for admin??


account_params = [
options[:admin_model].underscore, "name:string", "surname:string", "email:string", "crypted_password:string", "role:string",
Expand Down
1 change: 1 addition & 0 deletions padrino-admin/test/generators/test_admin_app_generator.rb
Expand Up @@ -59,6 +59,7 @@ def teardown
assert_match_in_file 'class Admin < Padrino::Application', "#{@apptmp}/sample_project/admin/app.rb"
assert_match_in_file 'role.project_module :accounts, \'/accounts\'', "#{@apptmp}/sample_project/admin/app.rb"
assert_match_in_file 'button_to pat(:logout)', "#{@apptmp}/sample_project/admin/views/layouts/application.haml"
assert_match_in_file(/ use ActiveRecord::ConnectionAdapters::ConnectionManagemen/m, "#{@apptmp}/sample_project/admin/app.rb")
end

should 'correctly generate a new padrino admin application with erb renderer' do
Expand Down
14 changes: 14 additions & 0 deletions padrino-gen/lib/padrino-gen/generators/actions.rb
Expand Up @@ -293,6 +293,20 @@ def insert_hook(include_text, where)
inject_into_file('config/boot.rb', " #{include_text}\n", :after => "Padrino.#{where} do\n")
end

# Inserts a middlware inside app.rb
#
# @param [String] include_text
# Text to include into hooks in boot.rb
#
# @example
# insert_middleware(ActiveRecord::ConnectionAdapters::ConnectionManagement)
#
# @api public
def insert_middleware(include_text, app=nil)
name = app || (options[:name].present? ? @app_name.downcase : 'app')
inject_into_file("#{name}/app.rb", " use #{include_text}\n", :after => "Padrino::Application\n")
end

# Registers and Creates Initializer.
#
# @param [Symbol] name
Expand Down
Expand Up @@ -118,6 +118,7 @@ def setup_orm
require_dependencies 'sqlite3'
end
require_dependencies 'activerecord', :require => 'active_record'
insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement'
create_file("config/database.rb", ar)
end

Expand Down
Expand Up @@ -102,7 +102,8 @@ def setup_orm
end
require_dependencies 'mini_record'
create_file('config/database.rb', ar)
insert_hook('ActiveRecord::Base.descendants.each(&:auto_upgrade!)', :after_load)
insert_hook('ActiveRecord::Base.auto_upgrade!', :after_load)
insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement'
end

MR_MODEL = (<<-MODEL) unless defined?(MR_MODEL)
Expand Down
12 changes: 11 additions & 1 deletion padrino-gen/test/test_model_generator.rb
Expand Up @@ -80,6 +80,11 @@ def teardown

# ACTIVERECORD
context "model generator using activerecord" do
should "add activerecord middleware" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '--script=none', '-d=activerecord') }
assert_match_in_file(/ use ActiveRecord::ConnectionAdapters::ConnectionManagemen/m, "#{@apptmp}/sample_project/app/app.rb")
end

should "generate model file" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '--script=none', '-t=bacon', '-d=activerecord') }
capture_io { generate(:model, 'user', "-r=#{@apptmp}/sample_project") }
Expand Down Expand Up @@ -122,11 +127,16 @@ def teardown
should "generate hooks for auto upgrade" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '--script=none', '-t=bacon', '-d=mini_record') }
assert_match_in_file(
"Padrino.after_load do\n ActiveRecord::Base.descendants.each(&:auto_upgrade!)",
"Padrino.after_load do\n ActiveRecord::Base.auto_upgrade!",
"#{@apptmp}/sample_project/config/boot.rb"
)
end

should "add activerecord middleware" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '--script=none', '-d=activerecord') }
assert_match_in_file(/ use ActiveRecord::ConnectionAdapters::ConnectionManagemen/m, "#{@apptmp}/sample_project/app/app.rb")
end

should "generate model file" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '--script=none', '-t=bacon', '-d=mini_record') }
capture_io { generate(:model, 'user', 'name:string', 'surname:string', 'age:integer', "-r=#{@apptmp}/sample_project") }
Expand Down

0 comments on commit 84f050f

Please sign in to comment.