In Files

Parent

Object

Public Instance Methods

collection_names() click to toggle source
# File lib/padrino-gen/padrino-tasks/mongoid.rb, line 50
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 34
def convert_ids(obj)
  if obj.is_a?(String) && obj =~ /^[a-f0-9]{24}$/
    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 34
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
          $stderr.puts $!, *($!.backtrace)
          $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'
      @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 Mysql::Error => sqlerr
        if sqlerr.errno == Mysql::Error::ER_ACCESS_DENIED_ERROR
          print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
          root_password = $stdin.gets.strip
          grant_statement = "GRANT ALL PRIVILEGES ON #{config[:database]}.* "                  "TO '#{config[:username]}'@'localhost' "                  "IDENTIFIED BY '#{config[:password]}' WITH GRANT OPTION;"
          ActiveRecord::Base.establish_connection(config.merge(
              :database => nil, :username => 'root', :password => root_password))
          ActiveRecord::Base.connection.create_database(config[:database], creation_options)
          ActiveRecord::Base.connection.execute grant_statement
          ActiveRecord::Base.establish_connection(config)
        else
          $stderr.puts sqlerr.error
          $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
      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
        $stderr.puts $!, *($!.backtrace)
        $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/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.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 341
def drop_database(config)
  case config[:adapter]
  when 'mysql', 'mysql2'
    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
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

Generates a controller test given the controllers name

# File lib/padrino-gen/generators/components/tests/rspec.rb, line 66
def generate_controller_test(name)
  rspec_contents = RSPEC_CONTROLLER_TEST.gsub(/!NAME!/, name.to_s.camelize)
  controller_spec_path = File.join('spec',options[:app],'controllers',"#{name.to_s.underscore}_controller_spec.rb")
  create_file destination_root(controller_spec_path), rspec_contents, :skip => true
end
generate_model_test(name) click to toggle source
# File lib/padrino-gen/generators/components/tests/rspec.rb, line 72
def generate_model_test(name)
  rspec_contents = RSPEC_MODEL_TEST.gsub(/!NAME!/, name.to_s.camelize).gsub(/!DNAME!/, name.to_s.underscore)
  model_spec_path = File.join('spec',options[:app],'models',"#{name.to_s.underscore}_spec.rb")
  create_file destination_root(model_spec_path), rspec_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 10
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 125
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
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/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 # default include
      insert_mocking_include "RR::Adapters::RRMethods", :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!/, @app_name.underscore))
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/rightjs.rb, line 1
def setup_script
  begin
    get('https://raw.github.com/padrino/padrino-static/master/js/right.js',  destination_root("/public/javascripts/right.js"))
    get('https://raw.github.com/padrino/padrino-static/master/ujs/right.js', destination_root("/public/javascripts/right-ujs.js"))
  rescue
    copy_file('templates/static/js/right.js',  destination_root("/public/javascripts/right.js"))
    copy_file('templates/static/ujs/right.js', destination_root("/public/javascripts/right-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/rspec.rb, line 58
def setup_test
  require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
  require_dependencies 'rspec', :group => 'test'
  insert_test_suite_setup RSPEC_SETUP, :path => "spec/spec_helper.rb"
  create_file destination_root("spec/spec.rake"), RSPEC_RAKE
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.