Class: Object

### Home Classes Methods

### In Files

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

### Parent

BasicObject

### Methods

- #catch_error
- #collection_names
- #convert_ids
- #create_database
- #create_migration_file
- #create_model_file
- #create_model_migration
- #drop_database
- #dump_schema
- #enum_mongoid_documents
- #firebird_db_string
- #generate_controller_test
- #generate_helper_test
- #generate_model_test
- #get_mongoid_models
- #local_database?
- #migrate_as
- #mongoid_collection
- #mongoid_collections
- #mongoid_new_collection
- #move_as
- #rename_mongoid_collection
- #resolve_structure_sql
- #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::Helper
- 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_HELPER_TEST
BACON_MODEL_TEST
BACON_RAKE
BACON_SETUP
COMPASS_INIT
COMPASS_REGISTER
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
LESS_INIT
MINITEST_CONTROLLER_TEST
MINITEST_HELPER_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
PADRINO_ROOT
POSTGRES
RACK_ENV

  Defines our constants

RIOT_CONTROLLER_TEST
RIOT_HELPER_TEST
RIOT_MODEL_TEST
RIOT_RAKE
RIOT_SETUP
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
STEAK_CONTROLLER_ACCEPTANCE_TEST
STEAK_CONTROLLER_TEST
STEAK_HELPER_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 341
    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 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.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 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 315
    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 365
    def dump_schema
      Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
    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 337
    def firebird_db_string(config)
      FireRuby::Database.db_string_for(config.symbolize_keys)
    end

generate_controller_test(name, path) click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 85
    def generate_controller_test(name, path)
      bacon_contents       = BACON_CONTROLLER_TEST.gsub(/!PATH!/, path).gsub(/!EXPANDED_PATH!/, path.gsub(/:\w+?_id/, "1"))
      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_helper_test(name, project_name, app_name) click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 98
    def generate_helper_test(name, project_name, app_name)
      bacon_contents = BACON_HELPER_TEST.gsub(/!NAME!/, "#{project_name}::#{app_name}::#{name}")
      bacon_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), bacon_contents, :skip => true
    end

generate_model_test(name) click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 91
    def generate_model_test(name)
      bacon_contents  = BACON_MODEL_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize).gsub(/!DNAME!/, name.to_s.underscore)
      bacon_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), 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

migrate_as(type) click to toggle source

    # File lib/padrino-gen/padrino-tasks/activerecord.rb, line 352
    def migrate_as(type)
      version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
      raise "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 359
    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 369
    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 332
    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'", :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.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"
      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 'erubis', :version => "~> 2.7.0"
    end

setup_script() click to toggle source

    # File lib/padrino-gen/generators/components/scripts/dojo.rb, line 1
    def setup_script
      begin
        get('https://raw.github.com/padrino/padrino-static/master/js/dojo.js',  destination_root("/public/javascripts/dojo.js"))
        get('https://raw.github.com/padrino/padrino-static/master/ujs/dojo.js', destination_root("/public/javascripts/dojo-ujs.js"))
      rescue
        copy_file('templates/static/js/dojo.js',  destination_root("/public/javascripts/dojo.js"))
        copy_file('templates/static/ujs/dojo.js', destination_root("/public/javascripts/dojo-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 33
    def setup_stylesheet
      require_dependencies 'compass'
      create_file destination_root('/lib/compass_plugin.rb'), COMPASS_INIT
      inject_into_file destination_root('/app/app.rb'), COMPASS_REGISTER, :after => "register Padrino::Helpers\n"

      directory "components/stylesheets/compass/", destination_root('/app/stylesheets')
    end

setup_test() click to toggle source

    # File lib/padrino-gen/generators/components/tests/bacon.rb, line 78
    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.