class Object

Constants

AR
AR_CHANGE_MG
AR_MIGRATION
AR_MODEL
AR_MODEL_DOWN_MG
AR_MODEL_UP_MG
BACON_CONTROLLER_TEST
BACON_HELPER_TEST
BACON_MODEL_TEST
BACON_RAKE
BACON_SETUP
COMPASS_INIT
CONNECTION_POOL_MIDDLEWARE
COUCHREST
CR_MODEL
CUCUMBER_FEATURE
CUCUMBER_SETUP
CUCUMBER_STEP
CUCUMBER_URL
CUCUMBER_YML
DM
DM_CHANGE_MG
DM_MIGRATION
DM_MODEL
DM_MODEL_DOWN_MG
DM_MODEL_UP_MG
DYNAMOID
DYNAMOID_MODEL
IDENTITY_MAP_MIDDLEWARE
LESS_INIT
MINITEST_CONTROLLER_TEST
MINITEST_HELPER_TEST
MINITEST_MODEL_TEST
MINITEST_RAKE
MINITEST_SETUP
MM_MODEL
MONGO
MONGOID
MONGOID_MODEL
MONGOMATIC
MONGOMATIC_MODEL
MR
MR_MODEL
MYSQL
MYSQL2
OHM
OHM_MODEL
PADRINO_ROOT
POSTGRES
RACK_ENV

Defines our constants

RIPPLE_CFG
RIPPLE_DB
RIPPLE_MODEL
RSPEC_CONTROLLER_TEST
RSPEC_HELPER_TEST
RSPEC_MODEL_TEST
RSPEC_RAKE
RSPEC_SETUP
SASS_INIT
SCSS_INIT
SEQUEL
SHOULDA_CONTROLLER_TEST
SHOULDA_HELPER_TEST
SHOULDA_MODEL_TEST
SHOULDA_RAKE
SHOULDA_SETUP
SQLITE
SQ_CHANGE_MG
SQ_MIGRATION
SQ_MODEL
SQ_MODEL_DOWN_MG
SQ_MODEL_UP_MG
TESTUNIT_CONTROLLER_TEST
TESTUNIT_HELPER_TEST
TESTUNIT_MODEL_TEST
TESTUNIT_RAKE
TESTUNIT_SETUP

Public Instance Methods

catch_error(type, error, config) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 346
def catch_error(type, error, config)
  $stderr.puts *(error.backtrace)
  $stderr.puts error.inspect
  case type
  when :create
    $stderr.puts "Couldn't create database for #{config.inspect}"
  when :drop
    $stderr.puts "Couldn't drop #{config[:database]}"
  end
end
collection_names() click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 108
def collection_names
  @collection_names ||= get_mongoid_models.map{ |d| d.collection.name }.uniq
end
convert_ids(obj) click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 92
def convert_ids(obj)
  if obj.is_a?(String) && obj =~ /^[a-f0-9]{24}$/
    defined?(Moped) ? Moped::BSON::ObjectId.from_string(obj) : BSON::ObjectId(obj)
  elsif obj.is_a?(Array)
    obj.map do |v|
      convert_ids(v)
    end
  elsif obj.is_a?(Hash)
    obj.each do |k, v|
      obj[k] = convert_ids(v)
    end
  else
    obj
  end
end
create_database(config) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 39
def create_database(config)
  begin
    if config[:adapter] =~ /sqlite/
      if File.exist?(config[:database])
        $stderr.puts "#{config[:database]} already exists."
      else
        begin
          # Create the SQLite database
          FileUtils.mkdir_p File.dirname(config[:database]) unless File.exist?(File.dirname(config[:database]))
          ActiveRecord::Base.establish_connection(config)
          ActiveRecord::Base.connection
        rescue StandardError => e
          catch_error(:create, e, config)
        end
      end
      return # Skip the else clause of begin/rescue
    else
      ActiveRecord::Base.establish_connection(config)
      ActiveRecord::Base.connection
    end
  rescue
    case config[:adapter]
    when 'mysql', 'mysql2', 'em_mysql2', 'jdbcmysql'
      @charset   = ENV['CHARSET']   || 'utf8'
      @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
      creation_options = {:charset => (config[:charset] || @charset), :collation => (config[:collation] || @collation)}
      begin
        ActiveRecord::Base.establish_connection(config.merge(:database => nil))
        ActiveRecord::Base.connection.create_database(config[:database], creation_options)
        ActiveRecord::Base.establish_connection(config)
      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 StandardError => e
        catch_error(:create, e, config)
      end
    end
  else
    $stderr.puts "#{config[:database]} already exists"
  end
end
create_migration_file(migration_name, name, columns) click to toggle source
# File lib/padrino-gen/generators/components/orms/activerecord.rb, line 194
def create_migration_file(migration_name, name, columns)
  output_migration_file(migration_name, name, columns,
    :base          => AR_MIGRATION,
    :change_format => AR_CHANGE_MG,
    :add           => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
    :remove        => Proc.new { |field, kind| "t.remove :#{field}" }
  )
end
create_model_file(name, options={}) click to toggle source

options => { :fields => [“title:string”, “body:string”], :app => ‘app’ }

# File lib/padrino-gen/generators/components/orms/activerecord.rb, line 149
def create_model_file(name, options={})
  model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
  model_contents = AR_MODEL.sub(/!NAME!/, name.to_s.underscore.camelize)
  create_file(model_path, model_contents,:skip => true)
end
create_model_migration(migration_name, name, columns) click to toggle source
# File lib/padrino-gen/generators/components/orms/activerecord.rb, line 179
def create_model_migration(migration_name, name, columns)
  output_model_migration(migration_name, name, columns,
    :base          => AR_MIGRATION,
    :column_format => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
    :up            => AR_MODEL_UP_MG,
    :down          => AR_MODEL_DOWN_MG
  )
end
drop_database(config) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 320
def drop_database(config)
  case config[:adapter]
  when 'mysql', 'mysql2', 'em_mysql2', 'jdbcmysql'
    ActiveRecord::Base.establish_connection(config)
    ActiveRecord::Base.connection.drop_database config[:database]
  when /^sqlite/
    require 'pathname'
    path = Pathname.new(config[:database])
    file = path.absolute? ? path.to_s : Padrino.root(path)

    FileUtils.rm(file)
  when 'postgresql'
    ActiveRecord::Base.establish_connection(config.merge(:database => 'postgres', :schema_search_path => 'public'))
    ActiveRecord::Base.connection.drop_database config[:database]
  end
end
dump_schema() click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 370
def dump_schema
  Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
enum_mongoid_documents(collection) { |doc| ... } click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 20
def enum_mongoid_documents(collection)
  collection.find({}, :timeout => false, :sort => "_id") do |cursor|
    cursor.each do |doc|
      yield doc
    end
  end
end
firebird_db_string(config) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 342
def firebird_db_string(config)
  FireRuby::Database.db_string_for(config.symbolize_keys)
end
generate_controller_test(name, path = nil) click to toggle source
# File lib/padrino-gen/generators/components/tests/shoulda.rb, line 99
def generate_controller_test(name, path = nil)
  shoulda_contents = SHOULDA_CONTROLLER_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize)
  controller_test_path = File.join('test',options[:app],'controllers',"#{name.to_s.underscore}_controller_test.rb")
  create_file destination_root(controller_test_path), shoulda_contents, :skip => true
end
generate_helper_test(name, project_name, app_name) click to toggle source
# File lib/padrino-gen/generators/components/tests/shoulda.rb, line 112
def generate_helper_test(name, project_name, app_name)
  shoulda_contents = SHOULDA_HELPER_TEST.gsub(/!NAME!/, "#{project_name}::#{app_name}::#{name}")
  shoulda_contents.gsub!(/!PATH!/, recognize_path)
  helper_spec_path = File.join('test', options[:app], 'helpers', "#{name.underscore}_test.rb")
  create_file destination_root(helper_spec_path), shoulda_contents, :skip => true
end
generate_model_test(name) click to toggle source
# File lib/padrino-gen/generators/components/tests/shoulda.rb, line 105
def generate_model_test(name)
  shoulda_contents = SHOULDA_MODEL_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize).gsub(/!DNAME!/, name.to_s.underscore)
  shoulda_contents.gsub!(/!PATH!/, recognize_path)
  model_test_path = File.join('test',options[:app],'models',"#{name.to_s.underscore}_test.rb")
  create_file destination_root(model_test_path), shoulda_contents, :skip => true
end
get_mongoid_models() click to toggle source

Helper to retrieve a list of models.

# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 68
def get_mongoid_models
  documents = []
  Dir['{app,.}/models/**/*.rb'].sort.each do |file|
    model_path = file[0..-4].split('/')[2..-1]

    begin
      klass = model_path.map(&:classify).join('::').constantize
      if klass.ancestors.include?(Mongoid::Document) && !klass.embedded
        documents << klass
      end
    rescue => e
      # Just for non-mongoid objects that don't have the embedded
      # attribute at the class level.
    end
  end

  documents
end
local_database?(config) { || ... } click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 116
def local_database?(config, &block)
  if %w( 127.0.0.1 localhost ).include?(config[:host]) || !config[:host]
    yield
  else
    puts "This task only modifies local databases. #{config[:database]} is on a remote host."
  end
end
migrate_as(type) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 357
def migrate_as(type)
  version = env_migration_version
  fail "MIGRATION_VERSION is required" unless version
  ActiveRecord::Migrator.run(type, "db/migrate/", version)
  dump_schema
end
mongoid_collection(name) click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 12
def mongoid_collection(name)
  Mongoid.master.collection(name)
end
mongoid_collections() click to toggle source

Mongoid 2 API

# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 8
def mongoid_collections
  Mongoid.master.collections
end
mongoid_new_collection(collection, name) click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 16
def mongoid_new_collection(collection, name)
  collection.db.collection(name)
end
move_as(type) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 364
def move_as(type)
  step = ENV['STEP'] ? ENV['STEP'].to_i : 1
  ActiveRecord::Migrator.send(type, 'db/migrate/', step)
  dump_schema
end
rename_mongoid_collection(collection, new_name) click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 28
def rename_mongoid_collection(collection, new_name)
  collection.rename(new_name)
end
resolve_structure_sql() click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 374
def resolve_structure_sql
  "#{Padrino.root}/db/#{Padrino.env}_structure.sql"
end
set_firebird_env(config) click to toggle source
# File lib/padrino-gen/padrino-tasks/activerecord.rb, line 337
def set_firebird_env(config)
  ENV["ISC_USER"]     = config[:username].to_s if config[:username]
  ENV["ISC_PASSWORD"] = config[:password].to_s if config[:password]
end
setup_mock() click to toggle source
# File lib/padrino-gen/generators/components/mocks/mocha.rb, line 1
def setup_mock
  require_dependencies 'mocha', :group => 'test', :require => false
  case options[:test].to_s
    when 'rspec'
      inject_into_file 'spec/spec_helper.rb', "  conf.mock_with :mocha\n", :after => "RSpec.configure do |conf|\n"
    else
      inject_into_file 'test/test_config.rb', "require 'mocha/api'\n", :after => "require File.expand_path(File.dirname(__FILE__) + \"/../config/boot\")\n"
      insert_mocking_include "Mocha::API"
  end
end
setup_orm() click to toggle source
# File lib/padrino-gen/generators/components/orms/activerecord.rb, line 111
def setup_orm
  ar = AR
  db = @project_name.underscore
  # We're now defaulting to mysql2 since mysql is deprecated
  case options[:adapter]
  when 'mysql-gem'
    ar.sub! /!DB_DEVELOPMENT!/, MYSQL.sub(/!DB_NAME!/,"'#{db}_development'")
    ar.sub! /!DB_PRODUCTION!/, MYSQL.sub(/!DB_NAME!/,"'#{db}_production'")
    ar.sub! /!DB_TEST!/, MYSQL.sub(/!DB_NAME!/,"'#{db}_test'")
    require_dependencies 'mysql', :version => "~> 2.8.1"
  when 'mysql', 'mysql2'
    ar.sub! /!DB_DEVELOPMENT!/, MYSQL2.sub(/!DB_NAME!/,"'#{db}_development'")
    ar.sub! /!DB_PRODUCTION!/, MYSQL2.sub(/!DB_NAME!/,"'#{db}_production'")
    ar.sub! /!DB_TEST!/, MYSQL2.sub(/!DB_NAME!/,"'#{db}_test'")
    require_dependencies 'mysql2'
  when 'postgres'
    ar.sub! /!DB_DEVELOPMENT!/, POSTGRES.sub(/!DB_NAME!/,"'#{db}_development'")
    ar.sub! /!DB_PRODUCTION!/, POSTGRES.sub(/!DB_NAME!/,"'#{db}_production'")
    ar.sub! /!DB_TEST!/, POSTGRES.sub(/!DB_NAME!/,"'#{db}_test'")
    require_dependencies 'pg'
  else
    ar.sub! /!DB_DEVELOPMENT!/, SQLITE.sub(/!DB_NAME!/,"Padrino.root('db', '#{db}_development.db')")
    ar.sub! /!DB_PRODUCTION!/, SQLITE.sub(/!DB_NAME!/,"Padrino.root('db', '#{db}_production.db')")
    ar.sub! /!DB_TEST!/, SQLITE.sub(/!DB_NAME!/,"Padrino.root('db', '#{db}_test.db')")
    require_dependencies 'sqlite3'
  end
  require_dependencies 'activerecord', :require => 'active_record', :version => ">= 3.1"
  create_file("config/database.rb", ar)
  middleware :connection_pool_management, CONNECTION_POOL_MIDDLEWARE
end
setup_renderer() click to toggle source
# File lib/padrino-gen/generators/components/renderers/erb.rb, line 1
def setup_renderer
  require_dependencies 'erubi', :version => '~> 1.6'
end
setup_script() click to toggle source
# File lib/padrino-gen/generators/components/scripts/mootools.rb, line 1
def setup_script
  begin
    get('https://raw.github.com/padrino/padrino-static/master/js/mootools.js',  destination_root("/public/javascripts/mootools.js"))
    get('https://raw.github.com/padrino/padrino-static/master/ujs/mootools.js', destination_root("/public/javascripts/mootools-ujs.js"))
  rescue
    copy_file('templates/static/js/mootools.js',  destination_root("/public/javascripts/mootools.js"))
    copy_file('templates/static/ujs/mootools.js', destination_root("/public/javascripts/mootools-ujs.js"))
  end
  create_file(destination_root('/public/javascripts/application.js'), "// Put your application scripts here")
end
setup_stylesheet() click to toggle source
# File lib/padrino-gen/generators/components/stylesheets/compass.rb, line 24
def setup_stylesheet
  require_dependencies 'compass-blueprint'
  initializer :compass, COMPASS_INIT.chomp
  directory "components/stylesheets/compass/", destination_root('/app/stylesheets')
end
setup_test() click to toggle source
# File lib/padrino-gen/generators/components/tests/shoulda.rb, line 91
def setup_test
  require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
  require_dependencies 'shoulda', :group => 'test'
  require_dependencies 'test-unit', :group => 'test'
  insert_test_suite_setup SHOULDA_SETUP
  create_file destination_root("test/test.rake"), SHOULDA_RAKE
end