Changeset 1064
- Timestamp:
- 12/03/07 08:01:18 (10 months ago)
- Files:
-
- plugins/merb_sequel/lib/merb/orms/sequel/connection.rb (modified) (3 diffs)
- plugins/merb_sequel/lib/merb/session/001_add_sessions_table.rb (modified) (2 diffs)
- plugins/merb_sequel/lib/merb/session/sequel_session.rb (modified) (5 diffs)
- plugins/merb_sequel/lib/merbtasks.rb (modified) (2 diffs)
- plugins/merb_sequel/sequel_generators/migration/migration_generator.rb (modified) (1 diff)
- plugins/merb_sequel/sequel_generators/migration/templates/new_migration.erb (modified) (1 diff)
- plugins/merb_sequel/sequel_generators/model/model_generator.rb (modified) (1 diff)
- plugins/merb_sequel/sequel_generators/resource_controller/resource_controller_generator.rb (modified) (3 diffs)
- plugins/merb_sequel/sequel_generators/resource_controller/templates/controller.rb (modified) (5 diffs)
- plugins/merb_sequel/sequel_generators/resource_controller/templates/helper.rb (modified) (1 diff)
- plugins/merb_sequel/specs/merb_sequel_spec.rb (modified) (1 diff)
- plugins/merb_sequel/specs/spec_helper.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/merb_sequel/lib/merb/orms/sequel/connection.rb
r686 r1064 1 require 'fileutils'1 require "fileutils" 2 2 3 3 module Merb … … 26 26 # Database connects as soon as the gem is loaded 27 27 def connect 28 29 require "sequel" 30 28 31 if File.exists?(config_file) 29 32 puts "Connecting to database..." 30 # Load the correct Sequel adapter and set it up according to the yaml file 31 case config[:adapter] 32 when 'mysql' 33 require "sequel/mysql" 34 host = config[:host] || 'localhost' 35 user = config[:user] || config[:username] || 'root' 36 password = config[:password] 37 # Use Sequel::Model.db to access this object 38 ::Sequel.mysql(config[:database], :host => host, :user => user, :password => password, :logger => MERB_LOGGER) 39 when 'postgresql' 40 require "sequel/postgres" 41 host = config[:host] || 'localhost' 42 user = config[:user] || config[:username] || 'root' 43 password = config[:password] 44 encoding = config[:encoding] || config[:charset] || nil 45 46 if encoding 47 ::Sequel.postgres(config[:database], :host => host, :user => user, :password => password, :encoding => encoding, :logger => MERB_LOGGER) 48 else 49 ::Sequel.postgres(config[:database], :host => host, :user => user, :password => password, :logger => MERB_LOGGER) 50 end 51 when 'sqlite' 52 require "sequel/sqlite" 53 if config[:database] 54 ::Sequel.sqlite(config[:database], :logger => MERB_LOGGER) 55 else 56 ::Sequel.sqlite(:logger => MERB_LOGGER) 57 end 58 else 59 require "sequel/sqlite" 60 p full_config 61 puts "No adapter specified in config/database.yml... trying a memory-only sqlite database" 62 ::Sequel.sqlite 63 end 33 options = {} 34 options[:adapter] = (config[:adaptor] || "sqlite") 35 options[:host] = (config[:host] || "localhost") 36 options[:user] = (config[:username] || config[:user] || "root") 37 options[:encoding] = (config[:encoding] || config[:charset]) if (config[:encoding] || config[:charset]) 38 options[:database] = config[:database] if config[:database] 39 options[:logger] = MERB_LOGGER 40 41 uri = "#{options[:adapter]}://" 42 uri << options[:username] if options[:username] 43 uri << (':' + options[:password]) if options[:password] 44 uri << '@' if (options[:user] || options[:password]) 45 uri << options[:host] 46 uri << ('/' + options[:database]) if options[:database] 47 48 connection = ::Sequel.connect(uri, options) 49 50 MERB_LOGGER.error("Connection Error: #{e}") unless connection 64 51 else 65 52 copy_sample_config 66 53 puts "No database.yml file found in #{MERB_ROOT}/config." 67 puts "A sample file was created called database.sample.yml for you to copy and edit."54 puts "A sample file was created called config/database.sample.yml for you to copy and edit." 68 55 exit(1) 69 56 end 57 70 58 end 71 59 … … 77 65 "Using Sequel database sessions") 78 66 end 67 79 68 end 80 69 end 81 70 end 71 82 72 end plugins/merb_sequel/lib/merb/session/001_add_sessions_table.rb
r687 r1064 1 1 class AddSessionsTable < Sequel::Migration 2 2 3 def up 3 4 create_table :sessions do 4 5 primary_key :id 5 varchar :session_id, :size => 32, :unique => true6 varchar :session_id, :size => 64, :unique => true 6 7 timestamp :created_at 7 8 text :data … … 10 11 11 12 def down 12 execute 'DROP TABLE sessions'13 execute "DROP TABLE sessions" 13 14 end 15 14 16 end plugins/merb_sequel/lib/merb/session/sequel_session.rb
r1062 r1064 3 3 module Merb 4 4 module SessionMixin 5 5 6 def setup_session 6 7 MERB_LOGGER.info("Setting up session") … … 16 17 set_cookie(_session_id_key, request.session.values[:session_id], _session_expiry) if (@_new_cookie || request.session.needs_new_cookie) 17 18 end 19 18 20 end 19 21 … … 58 60 end 59 61 60 def marshal(data) Base64.encode64(Marshal.dump(data)) if data end 62 def marshal(data) 63 Base64.encode64(Marshal.dump(data)) if data 64 end 65 61 66 def unmarshal(data) 62 67 Marshal.load(Base64.decode64(data)) if data … … 111 116 112 117 private 118 113 119 attr_writer :data 114 120 … … 139 145 puts "Created sessions table." 140 146 end 147 141 148 end plugins/merb_sequel/lib/merbtasks.rb
r687 r1064 1 require 'fileutils'1 require "fileutils" 2 2 3 3 namespace :sequel do 4 4 5 namespace :db do 6 5 7 desc "Perform migration using migrations in schema/migrations" 6 8 task :migrate => :merb_env do 7 9 Sequel::Migrator.apply(Merb::Orms::Sequel.connect, "schema/migrations", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) 8 10 end 11 9 12 end 10 13 11 14 namespace :sessions do 15 12 16 desc "Creates session migration" 13 17 task :create => :merb_env do 18 # TODO: this should not use '001' always... 14 19 dest = File.join(MERB_ROOT, "schema", "migrations","001_add_sessions_table.rb") 15 20 source = File.join(File.dirname(__FILE__), "merb", "session","001_add_sessions_table.rb") … … 24 29 Merb::Orms::Sequel.connect.execute("DELETE FROM #{table_name}") 25 30 end 31 26 32 end 33 27 34 end plugins/merb_sequel/sequel_generators/migration/migration_generator.rb
r825 r1064 1 require 'merb/generators/merb_generator_helpers'1 require "merb/generators/merb_generator_helpers" 2 2 3 3 class MigrationGenerator < Merb::GeneratorHelpers::MigrationGeneratorBase plugins/merb_sequel/sequel_generators/migration/templates/new_migration.erb
r1034 r1064 1 # For details on Sequel migrations see 2 # http://sequel.rubyforge.org/ 3 # http://code.google.com/p/ruby-sequel/wiki/Migrations 4 1 5 class <%= class_name.snake_case.camel_case %> < Sequel::Migration 6 2 7 def up 3 # For details on migrations in sequel see4 # http://sequel.rubyforge.org/5 # http://code.google.com/p/ruby-sequel/wiki/Migrations6 7 8 <%= "create_table :#{table_name} do" if table_name %> 9 <% if table_attributes.empty? -%> 8 10 primary_key :id 9 <% for attribute in table_attributes -%> 11 <% else -%> 12 <% table_attributes.each do |attribute| -%> 10 13 <%= attribute.type %> :<%= attribute.name %> 11 14 <% end -%> 12 <%= "end" if table_name %> 15 <% end -%> 16 <%= "end" if table_name %> 13 17 end 14 18 15 19 def down 16 20 <% if table_name -%> 17 execute 'DROP TABLE <%= table_name %>'21 execute "DROP TABLE <%= table_name %>" 18 22 <% end -%> 19 23 end 24 20 25 end 21 22 plugins/merb_sequel/sequel_generators/model/model_generator.rb
r825 r1064 1 require 'merb/generators/merb_generator_helpers'1 require "merb/generators/merb_generator_helpers" 2 2 3 3 class ModelGenerator < Merb::GeneratorHelpers::ModelGeneratorBase plugins/merb_sequel/sequel_generators/resource_controller/resource_controller_generator.rb
r825 r1064 1 require 'merb/generators/merb_generator_helpers'1 require "merb/generators/merb_generator_helpers" 2 2 3 3 class ResourceControllerGenerator < Merb::GeneratorHelpers::ControllerGeneratorBase … … 8 8 runtime_options[:actions] = %w[index show new edit] 9 9 runtime_options[:test_stub_generator] = "merb_controller_test" 10 super( [name], runtime_options)10 super([name], runtime_options) 11 11 end 12 12 … … 16 16 17 17 protected 18 18 19 def banner 19 <<-EOS20 <<-EOS 20 21 Creates a Merb controller, views and specs using Sequel Models 21 22 plugins/merb_sequel/sequel_generators/resource_controller/templates/controller.rb
r906 r1064 2 2 <% ivar = class_name.snake_case.singularize -%> 3 3 class <%= class_name %> < Application 4 4 5 provides :xml, :js, :yaml 5 6 … … 10 11 11 12 def show 12 @<%= ivar %> = <%= klass %>[ :id =>params[:id]]13 @<%= ivar %> = <%= klass %>[params[:id]] 13 14 render @<%= ivar %> 14 15 end … … 31 32 def edit 32 33 only_provides :html 33 @<%= ivar %> = <%= klass %>[ :id =>params[:id]]34 @<%= ivar %> = <%= klass %>[params[:id]] 34 35 render 35 36 end 36 37 37 38 def update 38 @<%= ivar %> = <%= klass %>[ :id =>params[:id]]39 @<%= ivar %> = <%= klass %>[params[:id]] 39 40 if @<%= ivar %>.update(params[:<%= ivar %>]) 40 41 redirect url(:<%= ivar %>, @<%= ivar %>) … … 45 46 46 47 def destroy 47 @<%= ivar %> = <%= klass %>[ :id =>params[:id]]48 @<%= ivar %> = <%= klass %>[params[:id]] 48 49 if @<%= ivar %>.destroy 49 50 redirect url(:<%= ivar %>s) … … 52 53 end 53 54 end 55 54 56 end plugins/merb_sequel/sequel_generators/resource_controller/templates/helper.rb
r825 r1064 1 1 module Merb 2 module <%= class_name %>Helper2 module <%= class_name %>Helper 3 3 4 4 end plugins/merb_sequel/specs/merb_sequel_spec.rb
r567 r1064 1 require File.dirname(__FILE__) + '/spec_helper'1 require File.dirname(__FILE__) + "/spec_helper" 2 2 3 3 describe "merb_sequel" do 4 4 5 it "should do nothing" do 5 6 true.should == true 6 7 end 8 7 9 end 10 11 describe "merb_sequel Generators" do 12 it "should description" do 13 14 end 15 end plugins/merb_sequel/specs/spec_helper.rb
r567 r1064 1 1 $TESTING=true 2 $:.push File.join(File.dirname(__FILE__), '..', 'lib')2 $:.push File.join(File.dirname(__FILE__), "..", "lib")
