Skip to content

Commit

Permalink
[padrino-admin] Improved generator, now admin_page accepts multiple m…
Browse files Browse the repository at this point in the history
…odels

This: padrino-gen admin_page post setting category

Generate a admin scaffold for Post, Setting and Category model
  • Loading branch information
DAddYE committed Mar 23, 2010
1 parent d428189 commit 6d346ad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES.rdoc
Expand Up @@ -2,6 +2,7 @@

== 0.9.8 (unreleased)

* Improved Padrino Admin Generator, now admin_page accepts multiple models
* Fixed Padrino Admin Account where the accounts password field is in plain text (text_field)
* Added layouts folder back into application skeleton
* Added support for forcing render of specified template (i.e render 'foo.haml' in js request)
Expand Down
2 changes: 1 addition & 1 deletion padrino-admin/lib/padrino-admin/generators/admin_app.rb
Expand Up @@ -63,7 +63,7 @@ def create_admin
]

admin_app = Padrino::Generators::AdminPage.new(["account"], :root => options[:root])
admin_app.orm = Padrino::Admin::Generators::Orm.new(:account, orm, columns, column_fields)
admin_app.default_orm = Padrino::Admin::Generators::Orm.new(:account, orm, columns, column_fields)
admin_app.invoke

template "templates/account/#{orm}.rb.tt", destination_root("app", "models", "account.rb"), :force => true
Expand Down
30 changes: 16 additions & 14 deletions padrino-admin/lib/padrino-admin/generators/admin_page.rb
Expand Up @@ -2,7 +2,7 @@ module Padrino
module Generators

class AdminPage < Thor::Group
attr_accessor :orm
attr_accessor :default_orm

# Add this generator to our padrino-gen
Padrino::Generators.add_generator(:admin_page, self)
Expand All @@ -16,8 +16,8 @@ def self.banner; "padrino-gen admin_page [Model]"; end
include Padrino::Generators::Actions
include Padrino::Generators::Admin::Actions

desc "Description:\n\n\tpadrino-gen admin_page YourModel"
argument :model, :desc => "The name of your model"
desc "Description:\n\n\tpadrino-gen admin_page Model(s)"
argument :models, :desc => "The name(s) of your model(s)", :type => :array
class_option :skip_migration, :aliases => "-s", :default => false, :type => :boolean
class_option :root, :desc => "The root destination", :aliases => '-r', :type => :string
class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
Expand All @@ -29,17 +29,19 @@ def self.banner; "padrino-gen admin_page [Model]"; end
def create_controller
self.destination_root = options[:root]
if in_app_root?
@orm ||= Padrino::Admin::Generators::Orm.new(model, adapter)
self.behavior = :revoke if options[:destroy]
ext = fetch_component_choice(:renderer)

template "templates/page/controller.rb.tt", destination_root("/admin/controllers/#{@orm.name_plural}.rb")
template "templates/#{ext}/page/_form.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/_form.#{ext}")
template "templates/#{ext}/page/edit.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/edit.#{ext}")
template "templates/#{ext}/page/index.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/index.#{ext}")
template "templates/#{ext}/page/new.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/new.#{ext}")

add_project_module(@orm.name_plural)
models.each do |model|
@orm = default_orm || Padrino::Admin::Generators::Orm.new(model, adapter)
self.behavior = :revoke if options[:destroy]
ext = fetch_component_choice(:renderer)

template "templates/page/controller.rb.tt", destination_root("/admin/controllers/#{@orm.name_plural}.rb")
template "templates/#{ext}/page/_form.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/_form.#{ext}")
template "templates/#{ext}/page/edit.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/edit.#{ext}")
template "templates/#{ext}/page/index.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/index.#{ext}")
template "templates/#{ext}/page/new.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/new.#{ext}")

add_project_module(@orm.name_plural)
end
else
say "You are not at the root of a Padrino application! (config/boot.rb not found)" and return unless in_app_root?
end
Expand Down
42 changes: 38 additions & 4 deletions padrino-admin/test/generators/test_admin_page_generator.rb
Expand Up @@ -10,6 +10,12 @@ def self.properties
end
end

class Page
def self.properties
[:id, :name, :body].map { |c| OpenStruct.new(:name => c) }
end
end

class TestAdminPageGenerator < Test::Unit::TestCase

def setup
Expand All @@ -34,8 +40,7 @@ def setup
assert_raise(Padrino::Admin::Generators::OrmError) { @page.start(['foo', '-r=/tmp/sample_project']) }
end

should 'correctyl generate a new padrino admin application default renderer' do
'Person'.classify.constantize
should 'correctly generate a new padrino admin application default renderer' do
silence_logger { @project.start(['sample_project', '--root=/tmp', '-d=datamapper']) }
silence_logger { @admin.start(['--root=/tmp/sample_project']) }
silence_logger { @model.start(['person', "name:string", "age:integer", "email:string", '-root=/tmp/sample_project']) }
Expand All @@ -52,8 +57,7 @@ def setup
assert_match_in_file 'role.project_module :people, "/people"', '/tmp/sample_project/admin/app.rb'
end

should 'correctyl generate a new padrino admin application with erb renderer' do
'Person'.classify.constantize
should 'correctly generate a new padrino admin application with erb renderer' do
silence_logger { @project.start(['sample_project', '--root=/tmp', '-d=datamapper', '-e=erb']) }
silence_logger { @admin.start(['--root=/tmp/sample_project']) }
silence_logger { @model.start(['person', "name:string", "age:integer", "email:string", '-root=/tmp/sample_project']) }
Expand All @@ -69,5 +73,35 @@ def setup
end
assert_match_in_file 'role.project_module :people, "/people"', '/tmp/sample_project/admin/app.rb'
end

should 'correctly generate a new padrino admin application with multiple models' do
silence_logger { @project.start(['sample_project', '--root=/tmp', '-d=datamapper']) }
silence_logger { @admin.start(['--root=/tmp/sample_project']) }
silence_logger { @model.start(['person', "name:string", "age:integer", "email:string", '-root=/tmp/sample_project']) }
silence_logger { @model.start(['page', "name:string", "body:string", '-root=/tmp/sample_project']) }
silence_logger { @page.start(['person', 'page', '--root=/tmp/sample_project']) }
# For Person
assert_file_exists '/tmp/sample_project/admin/controllers/people.rb'
assert_file_exists '/tmp/sample_project/admin/views/people/_form.haml'
assert_file_exists '/tmp/sample_project/admin/views/people/edit.haml'
assert_file_exists '/tmp/sample_project/admin/views/people/index.haml'
assert_file_exists '/tmp/sample_project/admin/views/people/new.haml'
%w(name age email).each do |field|
assert_match_in_file "label :#{field}", '/tmp/sample_project/admin/views/people/_form.haml'
assert_match_in_file "text_field :#{field}", '/tmp/sample_project/admin/views/people/_form.haml'
end
assert_match_in_file 'role.project_module :people, "/people"', '/tmp/sample_project/admin/app.rb'
# For Page
assert_file_exists '/tmp/sample_project/admin/controllers/pages.rb'
assert_file_exists '/tmp/sample_project/admin/views/pages/_form.haml'
assert_file_exists '/tmp/sample_project/admin/views/pages/edit.haml'
assert_file_exists '/tmp/sample_project/admin/views/pages/index.haml'
assert_file_exists '/tmp/sample_project/admin/views/pages/new.haml'
%w(name body).each do |field|
assert_match_in_file "label :#{field}", '/tmp/sample_project/admin/views/pages/_form.haml'
assert_match_in_file "text_field :#{field}", '/tmp/sample_project/admin/views/pages/_form.haml'
end
assert_match_in_file 'role.project_module :pages, "/pages"', '/tmp/sample_project/admin/app.rb'
end
end
end

0 comments on commit 6d346ad

Please sign in to comment.