Sha256: 1036e0a59572a85633076f8ec3b6aa1bc9376889295e8858ad437d2b14eae25b

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

require 'orange-core/middleware/base'

module Orange::Middleware
  
  class Database < Base
    def init(opts = {})
      opts = opts.with_defaults(:migration_url => (orange.options[:development_mode] ? '/__ORANGE_DB__/migrate' : false), :no_auto_upgrade => false)
      orange.mixin Orange::Mixins::DBLoader
      @options = opts
    end
    
    def stack_init
      unless orange.options.has_key?('database') && orange.options['database'] == false
        db = orange.options['database'] || 'sqlite3::memory:'
        orange.load_db!(db) 
        orange.upgrade_db! unless @options[:no_auto_upgrade] || orange.options['db_no_auto_upgrade']
      end
    end
    
    def packet_call(packet)
      path = packet['route.path'] || packet.request.path_info
      if @options[:migration_url] && @options[:migration_url] == path
        orange.migrate_db!
        after = packet.flash('redirect_to') || '/'
        packet.reroute(after)
      end
      pass packet
    end
  end
  
end

module Orange::Mixins::DBLoader
  def load_db!(url)
    DataMapper.setup(:default, url)
  end
  
  def migrate_db!
    DataMapper.auto_migrate!
  end
  
  def upgrade_db!
    DataMapper.auto_upgrade!
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
orange-core-0.6.0 lib/orange-core/middleware/database.rb
orange-core-0.5.9 lib/orange-core/middleware/database.rb
orange-core-0.5.8 lib/orange-core/middleware/database.rb