In Files

Parent

Object

Public Instance Methods

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
          $stderr.puts *(e.backtrace)
          $stderr.puts e.inspect
          $stderr.puts "Couldn't create database for #{config.inspect}"
        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', '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
        $stderr.puts *(e.backtrace)
        $stderr.puts e.inspect
        $stderr.puts "Couldn't create database for #{config.inspect}"
      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 178
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 133
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 163
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 341
def drop_database(config)
  case config[:adapter]
  when 'mysql', '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 363
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 dont 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 124
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 358
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'
  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
      insert_mocking_include "Mocha::API"
  end
end
setup_orm() click to toggle source
# File lib/padrino-gen/generators/components/orms/activerecord.rb, line 95
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/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 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.