Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
Conflicts:
	padrino-helpers/test/test_tag_helpers.rb
  • Loading branch information
agios committed Mar 7, 2012
2 parents 21312fe + a35faf0 commit d2de420
Show file tree
Hide file tree
Showing 29 changed files with 137 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,4 +1,4 @@
# before_install: sudo apt-get install redis-server libmemcache-dev memcached
lang: ruby
install:
- gem update --system
- bundle update
Expand Down
5 changes: 2 additions & 3 deletions Gemfile
Expand Up @@ -31,9 +31,8 @@ group :development do
gem "builder", ">= 2.1.2"
gem "bcrypt-ruby", :require => "bcrypt"
platforms :mri_18 do
gem "ruby-debug"
gem "rcov", "~> 0.9.8"
gem "ruby-prof", ">= 0.9.1"
# gem "rcov", "~> 0.9.8"
# gem "ruby-prof", ">= 0.9.1"
gem "system_timer", ">= 1.0"
end
platforms :jruby do
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -57,10 +57,10 @@ desc "Bumps the version number based on given version"
task :bump, [:version] do |t, args|
raise "Please specify version=x.x.x !" unless args.version
version_path = File.dirname(__FILE__) + '/padrino-core/lib/padrino-core/version.rb'
version_text = File.read(version_path).sub(/VERSION = '[\d\.]+'/, "VERSION = '#{args.version}'")
version_text = File.read(version_path).sub(/VERSION = '[a-z0-9\.]+'/, "VERSION = '#{args.version}'")
say "Updating Padrino to version #{args.version}"
File.open(version_path, 'w') { |f| f.write version_text }
sh 'git commit -a -m "Bumped version to %s"' % args.version
sh 'git commit -am "Bumped version to %s"' % args.version
end

desc "Executes a fresh install removing all padrino version and then reinstall all gems"
Expand Down
10 changes: 7 additions & 3 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' if [:mini_record, :activerecord].include?(orm)

account_params = [
options[:admin_model].underscore, "name:string", "surname:string", "email:string", "crypted_password:string", "role:string",
Expand Down Expand Up @@ -103,10 +104,13 @@ 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}")

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

add_project_module model_plural
require_dependencies('bcrypt-ruby', :require => 'bcrypt')
gsub_file destination_root("admin/views/accounts/_form.#{ext}"), "f.text_field :role, :class => :text_field", "f.select :role, :options => access_control.roles"
gsub_file destination_root("admin/controllers/accounts.rb"), "if account.destroy", "if account != current_account && account.destroy"
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/controllers/#{model_plural}.rb"), "if #{model_singular}.destroy", "if #{model_singular} != current_account && #{model_singular}.destroy"
return if self.behavior == :revoke

instructions = []
Expand Down
7 changes: 7 additions & 0 deletions padrino-admin/lib/padrino-admin/generators/orm.rb
Expand Up @@ -25,6 +25,13 @@ def initialize(name, orm, columns=nil, column_fields=nil)
raise OrmError, "Model '#{klass_name}' could not be found!" if @columns.nil? && @klass.nil?
end

def activerecord?
case orm
when :activerecord, :mini_record then true
else false
end
end

def field_type(type)
type = :string if type.nil? # couchrest-Hack to avoid the next line to fail
type = type.to_s.demodulize.downcase.to_sym unless type.is_a?(Symbol)
Expand Down
Expand Up @@ -5,11 +5,11 @@
end

post :create do
if account = Account.authenticate(params[:email], params[:password])
if account = <%= options[:admin_model] %>.authenticate(params[:email], params[:password])
set_current_account(account)
redirect url(:base, :index)
elsif Padrino.env == :development && params[:bypass]
account = Account.first
account = <%= options[:admin_model] %>.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(:accounts, :edit, :id => current_account.id) %></li>
<li><%%= link_to pat(:profile), url(:<%= options[:admin_model].underscore.pluralize %>, :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(:accounts, :edit, :id => current_account.id)
%li=link_to pat(:profile), url(:<%= options[:admin_model].underscore.pluralize %>, :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(:accounts, :edit, :id => current_account.id)
li==link_to pat(:profile), url(:<%= options[:admin_model].underscore.pluralize %>, :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
2 changes: 1 addition & 1 deletion padrino-admin/lib/padrino-admin/helpers/view_helpers.rb
Expand Up @@ -41,7 +41,7 @@ def padrino_admin_translate(word, default=nil)
# @return [String] The translated attribute name for the current locale.
#
# @example
# # => t("models.account.email", :default => "Email")
# # => t("models.account.attributes.email", :default => "Email")
# mat(:account, :email)
#
def model_attribute_translate(model, attribute)
Expand Down
18 changes: 18 additions & 0 deletions padrino-admin/test/generators/test_admin_app_generator.rb
Expand Up @@ -158,6 +158,24 @@ def teardown
assert_match_in_file 'button_to pat(:logout)', "#{@apptmp}/sample_project/admin/views/layouts/application.haml"
end

should 'not add activerecord middleware for #datamapper' do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=datamapper', '-e=haml') }
capture_io { generate(:admin_app,"-a=/admin", "--root=#{@apptmp}/sample_project") }
assert_no_match_in_file(/ use ActiveRecord::ConnectionAdapters::ConnectionManagemen/m, "#{@apptmp}/sample_project/admin/app.rb")
end

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

should 'add activerecord middleware for #mini_record' do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=mini_record', '-e=haml') }
capture_io { generate(:admin_app,"-a=/admin", "--root=#{@apptmp}/sample_project") }
assert_match_in_file(/ use ActiveRecord::ConnectionAdapters::ConnectionManagemen/m, "#{@apptmp}/sample_project/admin/app.rb")
end

should 'not conflict with existing seeds file' do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=activerecord', '-e=erb') }

Expand Down
3 changes: 3 additions & 0 deletions padrino-cache/lib/padrino-cache/helpers/fragment.rb
Expand Up @@ -60,6 +60,9 @@ def cache(key, opts = nil, &block)
logger.debug "SET Fragment", began_at, key.to_s if defined?(logger)
concat_content(value)
end
else
value = capture_html(&block)
concat_content(value)
end
end
end # Fragment
Expand Down
2 changes: 1 addition & 1 deletion padrino-core/lib/padrino-core/cli/base.rb
Expand Up @@ -60,10 +60,10 @@ def console
prepare :console
require File.expand_path("../../version", __FILE__)
ARGV.clear
puts "=> Loading #{options.environment} console (Padrino v.#{Padrino.version})"
require 'irb'
require "irb/completion"
require File.expand_path('config/boot.rb')
puts "=> Loading #{Padrino.env} console (Padrino v.#{Padrino.version})"
require File.expand_path('../console', __FILE__)
IRB.start
end
Expand Down
1 change: 1 addition & 0 deletions padrino-core/lib/padrino-core/loader.rb
Expand Up @@ -94,6 +94,7 @@ def clear!
# Method for reloading required applications and their files.
#
def reload!
return unless Padrino::Reloader.changed?
Padrino.before_load.each(&:call) # Run before hooks
Padrino::Reloader.reload! # detects the modified files
Padrino.after_load.each(&:call) # Run after hooks
Expand Down
2 changes: 1 addition & 1 deletion padrino-core/lib/padrino-core/support_lite.rb
Expand Up @@ -11,7 +11,7 @@
require 'active_support/inflector/inflections' # pluralize
require 'active_support/inflections' # load default inflections
require 'yaml' unless defined?(YAML) # load yaml for i18n
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/ # ruby color suppor for win
require 'win32console' if RUBY_PLATFORM =~ /(win|m)32/ # ruby color support for win

##
# This is an adapted version of active_support/core_ext/string/inflections.rb
Expand Down
2 changes: 1 addition & 1 deletion padrino-core/lib/padrino-core/version.rb
Expand Up @@ -6,7 +6,7 @@
#
module Padrino
# The version constant for the current version of Padrino.
VERSION = '0.10.6.c' unless defined?(Padrino::VERSION)
VERSION = '0.10.6.e' unless defined?(Padrino::VERSION)

#
# The current Padrino version.
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
2 changes: 1 addition & 1 deletion padrino-gen/lib/padrino-gen/generators/cli.rb
Expand Up @@ -29,7 +29,7 @@ def load_boot
# If we are outside app we need to load support_lite
require 'padrino-core/support_lite' unless defined?(SupportLite)
end
rescue Exception => e
rescue StandardError => e
puts "=> Problem loading #{boot}"
puts ["=> #{e.message}", *e.backtrace].join("\n ")
ensure
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 All @@ -134,6 +135,7 @@ def create_model_file(name, options={})
create_file(model_path, model_contents,:skip => true)
end


AR_MIGRATION = (<<-MIGRATION) unless defined?(AR_MIGRATION)
class !FILECLASS! < ActiveRecord::Migration
def self.up
Expand All @@ -149,6 +151,7 @@ def self.down
AR_MODEL_UP_MG = (<<-MIGRATION).gsub(/^/, ' ') unless defined?(AR_MODEL_UP_MG)
create_table :!TABLE! do |t|
!FIELDS!
t.timestamps
end
MIGRATION

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
2 changes: 1 addition & 1 deletion padrino-gen/lib/padrino-gen/generators/templates/static
Submodule static updated 1 files
+4 −16 js/jquery.js
44 changes: 19 additions & 25 deletions padrino-gen/lib/padrino-gen/padrino-tasks/activerecord.rb
Expand Up @@ -42,8 +42,9 @@ def create_database(config)
Dir.mkdir File.dirname(config[:database]) unless File.exist?(File.dirname(config[:database]))
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection
rescue
$stderr.puts $!, *($!.backtrace)
rescue StandardError => e
$stderr.puts *(e.backtrace)
$stderr.puts e.inspect
$stderr.puts "Couldn't create database for #{config.inspect}"
end
end
Expand All @@ -62,32 +63,21 @@ def create_database(config)
ActiveRecord::Base.establish_connection(config.merge(:database => nil))
ActiveRecord::Base.connection.create_database(config[:database], creation_options)
ActiveRecord::Base.establish_connection(config)
rescue Mysql::Error => sqlerr
if sqlerr.errno == Mysql::Error::ER_ACCESS_DENIED_ERROR
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
root_password = $stdin.gets.strip
grant_statement = "GRANT ALL PRIVILEGES ON #{config[:database]}.* " \
"TO '#{config[:username]}'@'localhost' " \
"IDENTIFIED BY '#{config[:password]}' WITH GRANT OPTION;"
ActiveRecord::Base.establish_connection(config.merge(
:database => nil, :username => 'root', :password => root_password))
ActiveRecord::Base.connection.create_database(config[:database], creation_options)
ActiveRecord::Base.connection.execute grant_statement
ActiveRecord::Base.establish_connection(config)
else
$stderr.puts sqlerr.error
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config[:charset] || @charset}, collation: #{config[:collation] || @collation}"
$stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config[:charset]
end
rescue StandardError => e
$stderr.puts *(e.backtrace)
$stderr.puts e.inspect
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config[:charset] || @charset}, collation: #{config[:collation] || @collation}"
$stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config[:charset]
end
when 'postgresql'
@encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
begin
ActiveRecord::Base.establish_connection(config.merge(:database => 'postgres', :schema_search_path => 'public'))
ActiveRecord::Base.connection.create_database(config[:database], config.merge(:encoding => @encoding))
ActiveRecord::Base.establish_connection(config)
rescue
$stderr.puts $!, *($!.backtrace)
rescue StandardError => e
$stderr.puts *(e.backtrace)
$stderr.puts e.inspect
$stderr.puts "Couldn't create database for #{config.inspect}"
end
end
Expand All @@ -105,8 +95,10 @@ def create_database(config)
begin
# Only connect to local databases
local_database?(config) { drop_database(config) }
rescue Exception => e
puts "Couldn't drop #{config[:database]} : #{e.inspect}"
rescue StandardError => e
$stderr.puts *(e.backtrace)
$stderr.puts e.inspect
$stderr.puts "Couldn't drop #{config[:database]}"
end
end
end
Expand All @@ -117,8 +109,10 @@ def create_database(config)
config = ActiveRecord::Base.configurations[Padrino.env || :development]
begin
drop_database(config)
rescue Exception => e
puts "Couldn't drop #{config[:database]} : #{e.inspect}"
rescue StandardError => e
$stderr.puts *(e.backtrace)
$stderr.puts e.inspect
$stderr.puts "Couldn't drop #{config[:database]}"
end
end

Expand Down
6 changes: 3 additions & 3 deletions padrino-gen/lib/padrino-gen/padrino-tasks/mongoid.rb
Expand Up @@ -9,7 +9,7 @@
# Helper to retrieve a list of models.
def get_mongoid_models
documents = []
Dir['{app,}/models/*.rb'].sort.each do |file|
Dir['{app,.}/models/*.rb'].sort.each do |file|
model_path = file[0..-4].split('/')[2..-1]

begin
Expand Down Expand Up @@ -85,7 +85,7 @@ def collection_names

begin
collection.rename("#{collection_name}_old")
rescue Exception => e
rescue StandardError => e
puts "Unable to rename database #{collection_name} to #{collection_name}_old"
puts "reason: #{e.message}\n\n"
end
Expand All @@ -95,7 +95,7 @@ def collection_names

begin
new_collection.rename(collection_name)
rescue Exception => e
rescue StandardError => e
puts "Unable to rename database #{new_collection.name} to #{collection_name}"
puts "reason: #{e.message}\n\n"
end
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 d2de420

Please sign in to comment.