Skip to content

Commit

Permalink
Merge branch 'super-admin' of github.com:WaYdotNET/padrino-framework …
Browse files Browse the repository at this point in the history
…into super-admin

* 'super-admin' of github.com:WaYdotNET/padrino-framework:
  Refactored the admin generators to support namespaced apps and to read a bit better. Also disabled `-a` since it wasn't really doing anything and left a few notes to implement them. Please, see the conversation at: padrino#854 (comment) I don't think this should be a blocker to release 0.11.0 and the rest of the admin needs to get there in time!..
  • Loading branch information
WaYdotNET committed Mar 13, 2013
2 parents d93b945 + 3d8e34c commit b97825f
Show file tree
Hide file tree
Showing 15 changed files with 372 additions and 276 deletions.
16 changes: 16 additions & 0 deletions padrino-admin/lib/padrino-admin/generators/actions.rb
Expand Up @@ -55,6 +55,22 @@ def remove_project_module(controller)
content.gsub!(/^\s+role\.project_module :#{controller}, '\/#{controller}'\n/, '')
File.open(path, 'wb') { |f| f.write content }
end

# Returns the app_name for the application at root.
#
# @param [String] app
# folder name of application.
#
# @return [String] module name for application.
#
# @example
# fetch_app_name('subapp')
#
# @api public
def fetch_app_name(app='app')
app_path = destination_root(app, 'app.rb')
@app_name ||= File.read(app_path).scan(/module\s(.*?)\n/).flatten[0]
end
end # Actions
end # Admin
end # Generators
Expand Down
18 changes: 13 additions & 5 deletions padrino-admin/lib/padrino-admin/generators/admin_app.rb
Expand Up @@ -21,7 +21,10 @@ def self.banner; "padrino-gen admin"; end
desc "Description:\n\n\tpadrino-gen admin generates a new Padrino Admin application"

class_option :skip_migration, :aliases => "-s", :default => false, :type => :boolean
class_option :app, :aliases => "-a", :desc => "The model destination path", :default => '.', :type => :string
# TODO FIXME Review these and implement accordingly.
# See https://github.com/padrino/padrino-framework/issues/854#issuecomment-14749356
# class_option :app, :desc => 'The application destination path', :aliases => '-a', :default => '/app', :type => :string
# class_option :models_path, :desc => 'The models destination path', :default => '.', :type => :string
class_option :root, :desc => "The root destination", :aliases => '-r', :default => ".", :type => :string
class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
class_option :renderer, :aliases => '-e', :desc => "Rendering engine (erb, haml)", :type => :string
Expand All @@ -42,6 +45,9 @@ def create_admin
raise SystemExit
end

# Get the app's namespace
@app_name = fetch_app_name

store_component_choice(:admin_renderer, tmp_ext)

self.behavior = :revoke if options[:destroy]
Expand All @@ -56,14 +62,14 @@ def create_admin
directory "templates/app", destination_root("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\")"
append_file destination_root("config/apps.rb"), "\nPadrino.mount(\"#{@app_name}::Admin\", :app_file => File.expand_path('../../admin/app.rb', __FILE__)).to(\"/admin\")"
unless options[:destroy]
insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement', 'admin' if [:mini_record, :activerecord].include?(orm)
end

params = [
@model_singular, "name:string", "surname:string", "email:string", "crypted_password:string", "role:string",
"-a=#{options[:app]}",
"-a=#{options[:models_path]}",
"-r=#{options[:root]}"
]
params << "-s" if options[:skip_migration]
Expand All @@ -87,7 +93,8 @@ def create_admin
admin_app.invoke_all
end

template "templates/account/#{orm}.rb.tt", destination_root(options[:app], "models", "#{@model_singular}.rb"), :force => true
# TODO See this, there's something wrong it's not being applied properly or something because test_account_model_generator last test fails.
template "templates/account/#{orm}.rb.tt", destination_root("models", "#{@model_singular}.rb"), :force => true

if File.exist?(destination_root("db/seeds.rb"))
run "mv #{destination_root('db/seeds.rb')} #{destination_root('db/seeds.old')}"
Expand All @@ -110,7 +117,8 @@ def create_admin
end

# A nicer select box
gsub_file destination_root("admin/views/#{@model_plural}/_form.#{ext}"), "f.text_field :role, :class => :text_field", "f.select :role, :options => access_control.roles"
# TODO FIXME This doesn't make much sense in here. Review.
# gsub_file destination_root("admin/views/#{@model_plural}/_form.#{ext}"), "f.text_field :role, :class => :text_field", "f.select :role, :options => access_control.roles"

# Destroy account only if not logged in
gsub_file destination_root("admin/controllers/#{@model_plural}.rb"), "if #{@model_singular}.destroy", "if #{@model_singular} != current_account && #{@model_singular}.destroy"
Expand Down
8 changes: 7 additions & 1 deletion padrino-admin/lib/padrino-admin/generators/admin_page.rb
Expand Up @@ -22,6 +22,9 @@ def self.banner; "padrino-gen admin_page [model]"; end
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
# TODO FIXME Review these and implement accordingly.
# See https://github.com/padrino/padrino-framework/issues/854#issuecomment-14749356
# class_option :app, :desc => 'The application destination path', :aliases => '-a', :default => '/app', :type => :string
class_option :root, :desc => "The root destination", :aliases => '-r', :type => :string
class_option :destroy, :aliases => '-d', :default => false, :type => :boolean

Expand All @@ -31,8 +34,11 @@ def self.banner; "padrino-gen admin_page [model]"; end
# Create controller for admin
def create_controller
self.destination_root = options[:root]
self.source_paths.unshift Padrino.root("vendor/padrino-admin/generators")
# TODO FIXME ??? Review
# self.source_paths.unshift Padrino.root("vendor/padrino-admin/generators")
if in_app_root?
@app_name = fetch_app_name

models.each do |model|
@orm = default_orm || Padrino::Admin::Generators::Orm.new(model, adapter)
self.behavior = :revoke if options[:destroy]
Expand Down
2 changes: 1 addition & 1 deletion padrino-admin/lib/padrino-admin/generators/orm.rb
Expand Up @@ -22,7 +22,7 @@ def initialize(name, orm, columns=nil, column_fields=nil)
@orm = orm.to_sym
@columns = columns
@column_fields = column_fields
raise OrmError, "Model '#{klass_name}' could not be found!" if @columns.nil? && @klass.nil?
raise OrmError, "Model '#{klass_name}' could not be found!\nPerhaps you would like to run 'bundle exec padrino g model #{klass_name}' to create it first?" if @columns.nil? && @klass.nil?
end

def activerecord?
Expand Down
@@ -1,4 +1,4 @@
class <%= @model_name %> < ::Sequel::Model
class <%= @model_name %> < Sequel::Model

plugin :validation_helpers

Expand Down
60 changes: 31 additions & 29 deletions padrino-admin/lib/padrino-admin/generators/templates/app.rb.tt
@@ -1,36 +1,38 @@
class Admin < Padrino::Application
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
register Padrino::Admin::AccessControl
module <%= @app_name %>
class Admin < Padrino::Application
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
register Padrino::Admin::AccessControl

##
# Application configuration options
#
# set :raise_errors, true # Raise exceptions (will stop application) (default for test)
# set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development)
# set :show_exceptions, true # Shows a stack trace in browser (default for development)
# set :logging, true # Logging in STDOUT for development and file for production (default only for development)
# set :public_folder, "foo/bar" # Location for static assets (default root/public)
# set :reload, false # Reload application files (default in development)
# set :default_builder, "foo" # Set a custom form builder (default 'StandardFormBuilder')
# set :locale_path, "bar" # Set path for I18n translations (default your_app/locales)
# disable :sessions # Disabled sessions by default (enable if needed)
# disable :flash # Disables sinatra-flash (enabled by default if Sinatra::Flash is defined)
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
#
##
# Application configuration options
#
# set :raise_errors, true # Raise exceptions (will stop application) (default for test)
# set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development)
# set :show_exceptions, true # Shows a stack trace in browser (default for development)
# set :logging, true # Logging in STDOUT for development and file for production (default only for development)
# set :public_folder, "foo/bar" # Location for static assets (default root/public)
# set :reload, false # Reload application files (default in development)
# set :default_builder, "foo" # Set a custom form builder (default 'StandardFormBuilder')
# set :locale_path, "bar" # Set path for I18n translations (default your_app/locales)
# disable :sessions # Disabled sessions by default (enable if needed)
# disable :flash # Disables sinatra-flash (enabled by default if Sinatra::Flash is defined)
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
#

set :admin_model, '<%= @model_name %>'
set :login_page, "/admin/sessions/new"
set :admin_model, '<%= @model_name %>'
set :login_page, "/admin/sessions/new"

enable :sessions
disable :store_location
enable :sessions
disable :store_location

access_control.roles_for :any do |role|
role.protect '/'
role.allow '/sessions'
end
access_control.roles_for :any do |role|
role.protect '/'
role.allow '/sessions'
end

access_control.roles_for :admin do |role|
access_control.roles_for :admin do |role|
end
end
end
@@ -1,4 +1,4 @@
Admin.controllers :base do
<%= @app_name %>::Admin.controllers :base do
get :index, :map => "/" do
render "base/index"
end
Expand Down
@@ -1,5 +1,4 @@
Admin.controllers :sessions do

<%= @app_name %>::Admin.controllers :sessions do
get :new do
render "/sessions/new", nil, :layout => false
end
Expand Down
@@ -1,4 +1,4 @@
Admin.controllers :<%= @orm.name_plural %> do
<%= @app_name %>::Admin.controllers :<%= @orm.name_plural %> do
get :index do
@title = "<%= @orm.name_plural.capitalize %>"
@<%= @orm.name_plural %> = <%= @orm.all %>
Expand Down
2 changes: 1 addition & 1 deletion padrino-admin/lib/padrino-admin/locale/admin/cs.yml
Expand Up @@ -7,7 +7,7 @@ cs:
list: "List"
edit: "Edit"
new: "Nový"
show:: "Show"
show: "Show"
delete: "Delete"
confirm: "Jsi si jistý?"
created_at: "Vytvořeno"
Expand Down
124 changes: 108 additions & 16 deletions padrino-admin/test/generators/test_account_model_generator.rb
@@ -1,38 +1,130 @@
require File.expand_path(File.dirname(__FILE__) + '/../helper')

describe "AccountModelGenerator" do
def setup
before do
@apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
`mkdir -p #{@apptmp}`
end

def teardown
after do
`rm -rf #{@apptmp}`
end

# COUCHREST
context 'account model using couchrest' do
setup do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=couchrest') }
capture_io { generate(:admin_app,"-a=/admin", "--root=#{@apptmp}/sample_project") }
describe "activerecord" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=activerecord') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

@model = "#{@apptmp}/sample_project/admin/models/account.rb"
it 'should be a activerecord model instance' do
assert_match_in_file(/class Account < ActiveRecord::Base/m, @model)
end

should 'be a couchrest model instance' do
assert_match_in_file(/class Account < CouchRest::Model::Base/m, @model)
it "should implement validations" do
skip "Expand and implement"
end
end
describe "mini_record" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=mini_record') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should be a activerecord model instance' do
assert_match_in_file(/class Account < ActiveRecord::Base/m, @model)
end

it "should implement validations" do
skip "Expand and implement"
end
end
describe "datamapper" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=datamapper') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should include the datamapper resource' do
assert_match_in_file(/include DataMapper::Resource/m, @model)
end

it "should implement validations" do
skip "Expand and implement"
end
end
describe "mongoid" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=mongoid') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should include the mongoid document' do
assert_match_in_file(/include Mongoid::Document/m, @model)
end
end
describe "mongomapper" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=mongomapper') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should include the mongomapper document' do
assert_match_in_file(/include MongoMapper::Document/m, @model)
end

it "should implement validations" do
skip "Expand and implement"
end
end
describe "ohm" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=ohm') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should be an ohm model instance' do
assert_match_in_file(/class Account < Ohm::Model/m, @model)
end

it "should implement validations" do
skip "Expand and implement"
end
end
describe "sequel" do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=sequel') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should be a sequel model instance' do
assert_match_in_file(/class Account < Sequel::Model/m, @model)
end

should 'not require additional validations' do
assert_no_match_in_file(/include CouchRest::Validation/m, @model)
it "should implement validations" do
skip "Expand and implement"
end
end

should 'no longer have validates_with_method' do
assert_no_match_in_file(/validates_with_method/m, @model)
describe 'couchrest' do
before do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=couchrest') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
@model = "#{@apptmp}/sample_project/models/account.rb"
end

it 'should be a couchrest model instance' do
assert_match_in_file(/class Account < CouchRest::Model::Base/m, @model)
end

should 'validate report errors using ActiveModel errors method' do
assert_match_in_file(/errors\.add\(:email, "is not unique"\)/m, @model)
it "should implement validations" do
skip "Expand and implement"
end
end
end

0 comments on commit b97825f

Please sign in to comment.