Skip to content

Commit

Permalink
Clean and test code. Fix padrino#780 issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
DAddYE committed Apr 25, 2012
1 parent 50b3f9b commit 3f7081d
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 82 deletions.
38 changes: 21 additions & 17 deletions padrino-admin/lib/padrino-admin/generators/admin_app.rb
Expand Up @@ -61,16 +61,21 @@ def create_admin
append_file destination_root("config/apps.rb"), "\nPadrino.mount(\"Admin\").to(\"/admin\")"
insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement', 'admin' if [:mini_record, :activerecord].include?(orm)

account_params = [
options[:admin_model].underscore, "name:string", "surname:string", "email:string", "crypted_password:string", "role:string",

# Setup Admin Model
@model_name = options[:admin_model].classify
@model_singular = @model_name.underscore
@model_plural = @model_singular.pluralize

params = [
@model_singular, "name:string", "surname:string", "email:string", "crypted_password:string", "role:string",
"-a=#{options[:app]}",
"-r=#{options[:root]}"
]
params << "-s" if options[:skip_migration]
params << "-d" if options[:destroy]

account_params << "-s" if options[:skip_migration]
account_params << "-d" if options[:destroy]

Padrino::Generators::Model.start(account_params)
Padrino::Generators::Model.start(params)
column = Struct.new(:name, :type)
columns = [:id, :name, :surname, :email].map { |col| column.new(col) }
column_fields = [
Expand All @@ -82,11 +87,11 @@ def create_admin
{ :name => :role, :field_type => :text_field }
]

admin_app = Padrino::Generators::AdminPage.new([options[:admin_model].underscore], :root => options[:root], :destroy => options[:destroy])
admin_app.default_orm = Padrino::Admin::Generators::Orm.new(options[:admin_model].underscore, orm, columns, column_fields)
admin_app = Padrino::Generators::AdminPage.new([@model_singular], :root => options[:root], :destroy => options[:destroy])
admin_app.default_orm = Padrino::Admin::Generators::Orm.new(@model_singular, orm, columns, column_fields)
admin_app.invoke_all

template "templates/account/#{orm}.rb.tt", destination_root(options[:app], "models", "#{options[:admin_model].underscore}.rb"), :force => true
template "templates/account/#{orm}.rb.tt", destination_root(options[:app], "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 @@ -104,15 +109,14 @@ def create_admin
template "templates/#{ext}/app/layouts/application.#{ext}.tt", destination_root("admin/views/layouts/application.#{ext}")
template "templates/#{ext}/app/sessions/new.#{ext}.tt", destination_root("admin/views/sessions/new.#{ext}")

model_singular = options[:admin_model].underscore
model_plural = model_singular.pluralize

add_project_module model_plural
add_project_module @model_plural
require_dependencies('bcrypt-ruby', :require => 'bcrypt')
gsub_file destination_root("admin/views/#{model_plural}/_form.#{ext}"), "f.text_field :role, :class => :text_field", "f.select :role, :options => access_control.roles"
gsub_file destination_root("admin/views/layouts/application.#{ext}"), ":accounts", ":#{model_plural}"
gsub_file destination_root("admin/controllers/#{model_plural}.rb"), "if #{model_singular}.destroy", "if #{model_singular} != current_account && #{model_singular}.destroy"
gsub_file destination_root("admin/controllers/sessions.rb"), "Account.", "#{options[:admin_model]}."

# 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"

# 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"
return if self.behavior == :revoke

instructions = []
Expand Down
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %> < ActiveRecord::Base
class <%= @model_name %> < ActiveRecord::Base
attr_accessor :password, :password_confirmation

# Validations
Expand Down Expand Up @@ -28,11 +28,11 @@ class <%= options[:admin_model].underscore.camelize %> < ActiveRecord::Base
end

private
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end

def password_required
crypted_password.blank? || password.present?
end
def password_required
crypted_password.blank? || password.present?
end
end
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %> < CouchRest::Model::Base
class <%= @model_name %> < CouchRest::Model::Base
attr_accessor :password, :password_confirmation

# Properties
Expand Down Expand Up @@ -44,23 +44,23 @@ class <%= options[:admin_model].underscore.camelize %> < CouchRest::Model::Base
end

private
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end

def password_required
crypted_password.blank? || password.present?
end
def password_required
crypted_password.blank? || password.present?
end

def unique_email_validator
account = Account.find_by_email(email)
def unique_email_validator
account = self.class.find_by_email(email)

# didn't find email in the database
return if account.nil?
# didn't find email in the database
return if account.nil?

# account with same email in database is this account
return if has_key?('_id') && self['_id'] == account['_id']
# account with same email in database is this account
return if has_key?('_id') && self['_id'] == account['_id']

errors.add(:email, "is not unique")
end
errors.add(:email, "is not unique")
end
end
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %>
class <%= @model_name %>
include DataMapper::Resource
include DataMapper::Validate
attr_accessor :password, :password_confirmation
Expand Down Expand Up @@ -45,11 +45,11 @@ class <%= options[:admin_model].underscore.camelize %>
end

private
def password_required
crypted_password.blank? || password.present?
end
def password_required
crypted_password.blank? || password.present?
end

def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password) if password.present?
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password) if password.present?
end
end
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %> < ActiveRecord::Base
class <%= @model_name %> < ActiveRecord::Base
attr_accessor :password, :password_confirmation

# Fields
Expand Down Expand Up @@ -31,11 +31,11 @@ class <%= options[:admin_model].underscore.camelize %> < ActiveRecord::Base
end

private
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end

def password_required
crypted_password.blank? || password.present?
end
def password_required
crypted_password.blank? || password.present?
end
end
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %>
class <%= @model_name %>
include Mongoid::Document
attr_accessor :password, :password_confirmation

Expand Down Expand Up @@ -43,11 +43,11 @@ class <%= options[:admin_model].underscore.camelize %>
end

private
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(self.password)
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(self.password)
end

def password_required
crypted_password.blank? || self.password.present?
end
def password_required
crypted_password.blank? || self.password.present?
end
end
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %>
class <%= @model_name %>
include MongoMapper::Document
attr_accessor :password, :password_confirmation

Expand Down Expand Up @@ -36,11 +36,11 @@ class <%= options[:admin_model].underscore.camelize %>
end

private
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password)
end

def password_required
crypted_password.blank? || password.present?
end
def password_required
crypted_password.blank? || password.present?
end
end
Expand Up @@ -10,11 +10,11 @@ password = shell.ask "Tell me the password to use:"

shell.say ""

account = <%= options[:admin_model].underscore.camelize %>.create(:email => email, :name => "Foo", :surname => "Bar", :password => password, :password_confirmation => password, :role => "admin")
account = <%= @model_name %>.create(:email => email, :name => "Foo", :surname => "Bar", :password => password, :password_confirmation => password, :role => "admin")

if account.valid?
shell.say "================================================================="
shell.say "<%= options[:admin_model].underscore.camelize %> has been successfully created, now you can login with:"
shell.say "<%= @model_name %> has been successfully created, now you can login with:"
shell.say "================================================================="
shell.say " email: #{email}"
shell.say " password: #{password}"
Expand Down
@@ -1,4 +1,4 @@
class <%= options[:admin_model].underscore.camelize %> < ::Sequel::Model
class <%= @model_name %> < ::Sequel::Model

plugin :validation_helpers

Expand Down Expand Up @@ -42,11 +42,11 @@ class <%= options[:admin_model].underscore.camelize %> < ::Sequel::Model
end

private
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password) if password.present?
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password) if password.present?
end

def password_required
crypted_password.blank? || password.present?
end
def password_required
crypted_password.blank? || password.present?
end
end
Expand Up @@ -20,7 +20,7 @@ class Admin < Padrino::Application
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
#

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

enable :sessions
Expand Down
Expand Up @@ -5,11 +5,11 @@ Admin.controllers :sessions do
end

post :create do
if account = <%= options[:admin_model] %>.authenticate(params[:email], params[:password])
if account = <%= @model_name %>.authenticate(params[:email], params[:password])
set_current_account(account)
redirect url(:base, :index)
elsif Padrino.env == :development && params[:bypass]
account = <%= options[:admin_model] %>.first
account = <%= @model_name %>.first
set_current_account(account)
redirect url(:base, :index)
else
Expand Down
Expand Up @@ -11,7 +11,7 @@
<h1><%%= link_to "Padrino Admin", url(:base_index) %></h1>
<div id="user-navigation">
<ul class="wat-cf">
<li><%%= link_to pat(:profile), url(:<%= options[:admin_model].underscore.pluralize %>, :edit, :id => current_account.id) %></li>
<li><%%= link_to pat(:profile), url(:<%= @model_plural %>, :edit, :id => current_account.id) %></li>
<li><%%= button_to pat(:logout), url(:sessions, :destroy), :method => :delete, :class => :button_to %></li>
</ul>
</div>
Expand Down
Expand Up @@ -10,7 +10,7 @@
%h1=link_to "Padrino Admin", url(:base_index)
#user-navigation
%ul.wat-cf
%li=link_to pat(:profile), url(:<%= options[:admin_model].underscore.pluralize %>, :edit, :id => current_account.id)
%li=link_to pat(:profile), url(:<%= @model_plurarl %>, :edit, :id => current_account.id)
%li=button_to pat(:logout), url(:sessions, :destroy), :method => :delete, :class => :button_to
#main-navigation
%ul.wat-cf
Expand Down
Expand Up @@ -11,7 +11,7 @@ html lang="en" xmlns="http://www.w3.org/1999/xhtml"
h1==link_to "Padrino Admin", url(:base_index)
#user-navigation
ul.wat-cf
li==link_to pat(:profile), url(:<%= options[:admin_model].underscore.pluralize %>, :edit, :id => current_account.id)
li==link_to pat(:profile), url(:<%= @model_plural %>, :edit, :id => current_account.id)
li==button_to pat(:logout), url(:sessions, :destroy), :method => :delete, :class => :button_to
#main-navigation
ul.wat-cf
Expand Down
39 changes: 39 additions & 0 deletions padrino-admin/test/generators/test_admin_app_generator.rb
Expand Up @@ -125,6 +125,45 @@ def teardown
assert_match_in_file 'button_to pat(:logout)', "#{@apptmp}/sample_project/admin/views/layouts/application.slim"
end

should 'correctly generate a new padrino admin application with a custom model' do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=activerecord', '-e=slim') }
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project", '-m=User') }
assert_file_exists("#{@apptmp}/sample_project")
assert_file_exists("#{@apptmp}/sample_project/admin")
assert_file_exists("#{@apptmp}/sample_project/admin/app.rb")
assert_file_exists("#{@apptmp}/sample_project/admin/controllers")
assert_file_exists("#{@apptmp}/sample_project/admin/controllers/users.rb")
assert_no_match_in_file(/[^_]account/i, "#{@apptmp}/sample_project/admin/controllers/users.rb")
assert_file_exists("#{@apptmp}/sample_project/admin/controllers/base.rb")
assert_file_exists("#{@apptmp}/sample_project/admin/controllers/sessions.rb")
assert_file_exists("#{@apptmp}/sample_project/admin/views")
assert_file_exists("#{@apptmp}/sample_project/admin/views/users/_form.slim")
assert_no_match_in_file(/[^_]account/i, "#{@apptmp}/sample_project/admin/views/users/_form.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/users/edit.slim")
assert_no_match_in_file(/[^_]account/i, "#{@apptmp}/sample_project/admin/views/users/edit.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/users/index.slim")
assert_no_match_in_file(/[^_]account/i, "#{@apptmp}/sample_project/admin/views/users/index.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/users/new.slim")
assert_no_match_in_file(/[^_]account/i, "#{@apptmp}/sample_project/admin/views/users/new.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/base/index.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/sessions/new.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/base/_sidebar.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/base/index.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/layouts/application.slim")
assert_file_exists("#{@apptmp}/sample_project/admin/views/sessions/new.slim")
assert_file_exists("#{@apptmp}/sample_project/public/admin")
assert_file_exists("#{@apptmp}/sample_project/public/admin/stylesheets")
assert_file_exists("#{@apptmp}/sample_project/models/user.rb")
assert_no_match_in_file(/Account/, "#{@apptmp}/sample_project/models/user.rb")
assert_file_exists("#{@apptmp}/sample_project/db/seeds.rb")
assert_file_exists("#{@apptmp}/sample_project/db/migrate/001_create_users.rb")
assert_no_match_in_file(/[^_]account/i, "#{@apptmp}/sample_project/db/migrate/001_create_users.rb")
assert_match_in_file 'Padrino.mount("Admin").to("/admin")', "#{@apptmp}/sample_project/config/apps.rb"
assert_match_in_file 'class Admin < Padrino::Application', "#{@apptmp}/sample_project/admin/app.rb"
assert_match_in_file 'role.project_module :users, \'/users\'', "#{@apptmp}/sample_project/admin/app.rb"
assert_match_in_file 'button_to pat(:logout)', "#{@apptmp}/sample_project/admin/views/layouts/application.slim"
end

should 'correctly generate a new padrino admin application with model in non-default application path' do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=activerecord', '-e=haml') }
capture_io { generate(:admin_app,"-a=/admin", "--root=#{@apptmp}/sample_project") }
Expand Down

0 comments on commit 3f7081d

Please sign in to comment.