Skip to content

Instantly share code, notes, and snippets.

@ciaranarcher
Created September 9, 2011 14:51
Show Gist options
  • Save ciaranarcher/1206424 to your computer and use it in GitHub Desktop.
Save ciaranarcher/1206424 to your computer and use it in GitHub Desktop.
Toy Sinatra
require 'rubygems'
require 'sinatra'
require 'active_record'
require 'logger'
class DatabaseSIA < ActiveRecord::Base
self.abstract_class = true
# rename attributes to lower case
after_initialize :relabel_atttributes
def relabel_atttributes
attributes.keys.each do |k|
self.class.alias_attribute k.parameterize.underscore.to_sym, k.to_sym
end
end
end
class Client < DatabaseSIA
# set a non-defualt primary key
set_primary_key :clientid
end
get '/' do
'Hello TorqueBox! :)'
end
get '/database-example' do
puts settings.config[:adapter]
ActiveRecord::Base.establish_connection(
settings.config
)
puts '** connected.'
ActiveRecord::Base.logger = Logger.new(STDOUT)
puts '** active record logger setup.'
# get all clients
@clients = Client.find(:all)
@clients.each do |c|
#puts c.ClientID.to_s + '.' + c.FirstName + ' ' + c.LastName + ' (' + c.AccountName + ')'
puts c.clientid.to_s + '. ' + c.firstname + ' ' + c.lastname + ' (' + c.accountname + ')'
end
ActiveRecord::Base.clear_active_connections!
puts '** connections cleared.'
'database-example is complete!! ;))'
end
require 'app'
configure do
#load the environment-specific section of the file
set :config, YAML.load_file('./' + settings.environment.to_s + '.config.yml')[settings.environment.to_s]
end
# dev only config
configure(:development) do |c|
require "sinatra/reloader"
c.also_reload "*.rb"
c.also_reload "*.ru"
end
run Sinatra::Application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment