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/couchrest.rb, line 53
def create_migration_file(migration_name, name, columns)
  # NO MIGRATION NEEDED
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/couchrest.rb, line 40
def create_model_file(name, options={})
  model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
  field_tuples = options[:fields].map { |value| value.split(":") }
  column_declarations = field_tuples.map { |field, kind| "property :#{field}" }.join("\n  ")
  model_contents = CR_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
  model_contents.gsub!(/!FIELDS!/, column_declarations)
  create_file(model_path, model_contents)
end
create_model_migration(filename, name, fields) click to toggle source
# File lib/padrino-gen/generators/components/orms/couchrest.rb, line 49
def create_model_migration(filename, name, fields)
  # NO MIGRATION NEEDED
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) click to toggle source
# File lib/padrino-gen/generators/components/tests/minitest.rb, line 94
def generate_controller_test(name, path)
  minitest_contents = MINITEST_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), minitest_contents, :skip => true
end
generate_helper_test(name, project_name, app_name) click to toggle source
# File lib/padrino-gen/generators/components/tests/minitest.rb, line 107
def generate_helper_test(name, project_name, app_name)
  minitest_contents = MINITEST_HELPER_TEST.gsub(/!NAME!/, "#{project_name}::#{app_name}::#{name}")
  minitest_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), minitest_contents, :skip => true
end
generate_model_test(name) click to toggle source
# File lib/padrino-gen/generators/components/tests/minitest.rb, line 100
def generate_model_test(name)
  minitest_contents = MINITEST_MODEL_TEST.gsub(/!NAME!/, name.to_s.underscore.camelize).gsub(/!DNAME!/, name.to_s.underscore)
  minitest_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), minitest_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/rr.rb, line 1
def setup_mock
  require_dependencies 'rr', :require => false, :group => 'test'
  case options[:test].to_s
    when 'rspec'
      inject_into_file 'spec/spec_helper.rb', "require '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/couchrest.rb, line 25
def setup_orm
  require_dependencies 'couchrest_model', :version => '~>1.1.0'
  require_dependencies 'json_pure'
  create_file("config/database.rb", COUCHREST.gsub(/!NAME!/, @project_name.underscore))
end
setup_renderer() click to toggle source
# File lib/padrino-gen/generators/components/renderers/slim.rb, line 1
def setup_renderer
  require_dependencies 'slim'
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/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/minitest.rb, line 87
def setup_test
  require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
  require_dependencies 'minitest', :require => 'minitest/autorun', :group => 'test'
  insert_test_suite_setup MINITEST_SETUP
  create_file destination_root("test/test.rake"), MINITEST_RAKE
end