### Home Classes Methods

### In Files

- lib/padrino-gen/generators/components/tests/bacon.rb
- lib/padrino-gen/generators/components/tests/minitest.rb
- lib/padrino-gen/generators/components/tests/riot.rb
- lib/padrino-gen/generators/components/tests/shoulda.rb
- lib/padrino-gen/generators/components/tests/rspec.rb
- lib/padrino-gen/generators/components/tests/cucumber.rb
- lib/padrino-gen/generators/components/tests/steak.rb
- lib/padrino-gen/generators/components/scripts/prototype.rb
- lib/padrino-gen/generators/components/scripts/extcore.rb
- lib/padrino-gen/generators/components/scripts/rightjs.rb
- lib/padrino-gen/generators/components/scripts/mootools.rb
- lib/padrino-gen/generators/components/scripts/dojo.rb
- lib/padrino-gen/generators/components/scripts/jquery.rb
- lib/padrino-gen/generators/components/renderers/erb.rb
- lib/padrino-gen/generators/components/renderers/slim.rb
- lib/padrino-gen/generators/components/renderers/liquid.rb
- lib/padrino-gen/generators/components/renderers/haml.rb
- lib/padrino-gen/generators/components/orms/activerecord.rb
- lib/padrino-gen/generators/components/orms/ohm.rb
- lib/padrino-gen/generators/components/orms/mongomatic.rb
- lib/padrino-gen/generators/components/orms/mongoid.rb
- lib/padrino-gen/generators/components/orms/sequel.rb
- lib/padrino-gen/generators/components/orms/ripple.rb
- lib/padrino-gen/generators/components/orms/datamapper.rb
- lib/padrino-gen/generators/components/orms/mongomapper.rb
- lib/padrino-gen/generators/components/orms/couchrest.rb
- lib/padrino-gen/generators/components/orms/minirecord.rb
- lib/padrino-gen/generators/components/stylesheets/less.rb
- lib/padrino-gen/generators/components/stylesheets/scss.rb
- lib/padrino-gen/generators/components/stylesheets/sass.rb
- lib/padrino-gen/generators/components/stylesheets/compass.rb
- lib/padrino-gen/generators/components/mocks/rr.rb
- lib/padrino-gen/generators/components/mocks/mocha.rb
- lib/padrino-gen/padrino-tasks/mongoid.rb
- lib/padrino-gen/padrino-tasks/activerecord.rb

### Parent

BasicObject

### Methods

- #catch_error
- #collection_names
- #convert_ids
- #create_database
- #create_migration_file
- #create_model_file
- #create_model_migration
- #drop_database
- #enum_mongoid_documents
- #firebird_db_string
- #generate_controller_test
- #generate_model_test
- #get_mongoid_models
- #local_database?
- #mongoid_collection
- #mongoid_collections
- #mongoid_new_collection
- #rename_mongoid_collection
- #set_firebird_env
- #setup_mock
- #setup_orm
- #setup_renderer
- #setup_script
- #setup_stylesheet
- #setup_test

### Files

- README.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Generators
- Padrino::Generators::Actions
- Padrino::Generators::Actions::ClassMethods
- Padrino::Generators::App
- Padrino::Generators::AppRootNotFound
- Padrino::Generators::Cli
- Padrino::Generators::Component
- Padrino::Generators::Components
- Padrino::Generators::Components::Actions
- Padrino::Generators::Controller
- Padrino::Generators::Mailer
- Padrino::Generators::Migration
- Padrino::Generators::Model
- Padrino::Generators::Plugin
- Padrino::Generators::Project
- Padrino::Generators::Runner
- Padrino::Generators::SqlHelpers
- Padrino::Generators::Task
- ActiveRecord
- ActiveRecord::Schema
- Object

No matching classes.

# Object

### Constants

AR
AR_CHANGE_MG
AR_MIGRATION
AR_MODEL
AR_MODEL_DOWN_MG
AR_MODEL_UP_MG
BACON_CONTROLLER_TEST
BACON_MODEL_TEST
BACON_RAKE
BACON_SETUP
COMPASS_INIT
COMPASS_REGISTER
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
LESS_INIT
MINITEST_CONTROLLER_TEST
MINITEST_MODEL_TEST
MINITEST_RAKE
MINITEST_SETUP
MM_MODEL
MONGO
MONGOID
MONGOID3
MONGOID_MODEL
MONGOMATIC
MONGOMATIC_MODEL
MR
MR_MODEL
MYSQL
MYSQL2
OHM
OHM_MODEL
POSTGRES
RIOT_CONTROLLER_TEST
RIOT_MODEL_TEST
RIOT_RAKE
RIOT_SETUP
RIPPLE_CFG
RIPPLE_DB
RIPPLE_MODEL
RSPEC_CONTROLLER_TEST
RSPEC_MODEL_TEST
RSPEC_RAKE
RSPEC_SETUP
SASS_INIT
SCSS_INIT
SEQUEL
SHOULDA_CONTROLLER_TEST
SHOULDA_MODEL_TEST
SHOULDA_RAKE
SHOULDA_SETUP
SQLITE
SQ_CHANGE_MG
SQ_MIGRATION
SQ_MODEL
SQ_MODEL_DOWN_MG
SQ_MODEL_UP_MG
STEAK_CONTROLLER_ACCEPTANCE_TEST
STEAK_CONTROLLER_TEST
STEAK_MODEL_TEST
STEAK_RAKE
STEAK_SETUP

### Public Instance Methods

catch_error(type, error, config) click to toggle source

    # File lib/padrino-gen/padrino-tasks/activerecord.rb, line 358
    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
              Dir.mkdir 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 180
    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 135
    def create_model_file(name, options={})
      model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
      model_contents = AR_MODEL.gsub(/!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 165
    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 332
    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

enum_mongoid_documents(collection) 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 354
    def firebird_db_string(config)
      FireRuby::Database.db_string_for(config.symbolize_keys)
    end

generate_controller_test(name) click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 69
    def generate_controller_test(name)
      bacon_contents       = BACON_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), bacon_contents, :skip => true
    end

generate_model_test(name) click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 75
    def generate_model_test(name)
      bacon_contents  = BACON_MODEL_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize).gsub(/!DNAME!/, name.to_s.underscore)
      path = options[:app] == '.' ? '/..' : '/../..'
      bacon_contents.gsub!(/!PATH!/,path)
      model_test_path = File.join('test',options[:app],'models',"#{name.to_s.underscore}_test.rb")
      create_file destination_root(model_test_path), bacon_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, &block) click to toggle source

    # File lib/padrino-gen/padrino-tasks/activerecord.rb, line 116
    def local_database?(config, &block)
      if %( 127.0.0.1 localhost ).include?(config[:host]) || config[:host].blank?
        yield
      else
        puts "This task only modifies local databases. #{config[:database]} is on a remote host."
      end
    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

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

set_firebird_env(config) click to toggle source

    # File lib/padrino-gen/padrino-tasks/activerecord.rb, line 349
    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/rr.rb, line 1
    def setup_mock
      require_dependencies 'rr', :group => 'test'
      case options[:test].to_s
        when 'rspec'
          inject_into_file 'spec/spec_helper.rb', "  conf.mock_with :rr\n", :after => "RSpec.configure do |conf|\n"
        when 'riot'
          inject_into_file "test/test_config.rb","require 'riot/rr'\n", :after => "\"/../config/boot\")\n"
        when 'minitest'
          insert_mocking_include "RR::Adapters::MiniTest", :path => "test/test_config.rb"
        else
          insert_mocking_include "RR::Adapters::TestUnit", :path => "test/test_config.rb"
      end
    end

setup_orm() click to toggle source

    # File lib/padrino-gen/generators/components/orms/activerecord.rb, line 97
    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.gsub! /!DB_DEVELOPMENT!/, MYSQL.gsub(/!DB_NAME!/,"'#{db}_development'")
        ar.gsub! /!DB_PRODUCTION!/, MYSQL.gsub(/!DB_NAME!/,"'#{db}_production'")
        ar.gsub! /!DB_TEST!/, MYSQL.gsub(/!DB_NAME!/,"'#{db}_test'")
        require_dependencies 'mysql', :version => "~> 2.8.1"
      when 'mysql', 'mysql2'
        ar.gsub! /!DB_DEVELOPMENT!/, MYSQL2.gsub(/!DB_NAME!/,"'#{db}_development'")
        ar.gsub! /!DB_PRODUCTION!/, MYSQL2.gsub(/!DB_NAME!/,"'#{db}_production'")
        ar.gsub! /!DB_TEST!/, MYSQL2.gsub(/!DB_NAME!/,"'#{db}_test'")
        require_dependencies 'mysql2'
      when 'postgres'
        ar.gsub! /!DB_DEVELOPMENT!/, POSTGRES.gsub(/!DB_NAME!/,"'#{db}_development'")
        ar.gsub! /!DB_PRODUCTION!/, POSTGRES.gsub(/!DB_NAME!/,"'#{db}_production'")
        ar.gsub! /!DB_TEST!/, POSTGRES.gsub(/!DB_NAME!/,"'#{db}_test'")
        require_dependencies 'pg'
      else
        ar.gsub! /!DB_DEVELOPMENT!/, SQLITE.gsub(/!DB_NAME!/,"Padrino.root('db', '#{db}_development.db')")
        ar.gsub! /!DB_PRODUCTION!/, SQLITE.gsub(/!DB_NAME!/,"Padrino.root('db', '#{db}_production.db')")
        ar.gsub! /!DB_TEST!/, SQLITE.gsub(/!DB_NAME!/,"Padrino.root('db', '#{db}_test.db')")
        require_dependencies 'sqlite3'
      end
      require_dependencies 'activerecord', :require => 'active_record', :version => ">= 3.1"
      insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement'
      create_file("config/database.rb", ar)
    end

setup_renderer() click to toggle source

    # File lib/padrino-gen/generators/components/renderers/erb.rb, line 1
    def setup_renderer
      require_dependencies 'erubis', :version => "~> 2.7.0"
    end

setup_script() click to toggle source

    # File lib/padrino-gen/generators/components/scripts/prototype.rb, line 1
    def setup_script
      begin
        get('https://raw.github.com/padrino/padrino-static/master/js/protopak.js',   destination_root("/public/javascripts/protopak.js"))
        get('https://raw.github.com/padrino/padrino-static/master/js/lowpro.js',     destination_root("/public/javascripts/lowpro.js"))
        get('https://raw.github.com/padrino/padrino-static/master/ujs/prototype.js', destination_root("/public/javascripts/prototype-ujs.js"))
      rescue
        copy_file('templates/static/js/protopak.js',   destination_root("/public/javascripts/protopak.js"))
        copy_file('templates/static/js/lowpro.js',     destination_root("/public/javascripts/lowpro.js"))
        copy_file('templates/static/ujs/prototype.js', destination_root("/public/javascripts/prototype-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/less.rb, line 19
    def setup_stylesheet
      require_dependencies 'less'
      require_dependencies 'rack-less'
      require_dependencies 'therubyracer'
      initializer :less, LESS_INIT
      empty_directory destination_root('/app/stylesheets')
    end

setup_test() click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 62
    def setup_test
      require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
      require_dependencies 'bacon', :group => 'test'
      insert_test_suite_setup BACON_SETUP, :path => 'test/test_config.rb'
      create_file destination_root("test/test.rake"), BACON_RAKE
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.