Permalink
Browse files

Set up ActiveRecord.

  • Loading branch information...
1 parent 9a5098b commit 6402e1e2c4413f8e970ac2325c489e8b9cc23e1d @markoa markoa committed Dec 13, 2010
Showing with 88 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +23 −0 Rakefile
  3. +45 −5 coming_soon.rb
  4. +5 −0 database.yml.example
  5. +14 −0 db/migrate/001_users.rb
View
@@ -1,2 +1,3 @@
*.swp
config.yml
+database.yml
View
@@ -0,0 +1,23 @@
+namespace :db do
+ task :environment do
+ require 'active_record'
+ require 'logger'
+
+ DB_CONFIG = YAML.load_file("database.yml")
+
+ ActiveRecord::Base.establish_connection(
+ :adapter => DB_CONFIG['adapter'],
+ :host => DB_CONFIG['host'],
+ :username => DB_CONFIG['username'],
+ :password => DB_CONFIG['password'],
+ :database => DB_CONFIG['name']
+ )
+ end
+
+ desc "Migrate the database"
+ task(:migrate => :environment) do
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
+ ActiveRecord::Migration.verbose = true
+ ActiveRecord::Migrator.migrate("db/migrate")
+ end
+end
View
@@ -1,20 +1,60 @@
require 'sinatra/base'
+require 'active_record'
+
+
+def load_configuration(file, name)
+ if !File.exist?(file)
+ puts "There's no configuration file at #{file}!"
+ exit!
+ end
+ const_set(name, YAML.load_file(file))
+end
class ComingSoon < Sinatra::Base
configure do
- if !File.exist?('config.yml')
- puts "There's no configuration file at config.yml!"
- exit!
- end
- APP_CONFIG = YAML.load_file('config.yml')
+ load_configuration("config.yml", "APP_CONFIG")
+ load_configuration("database.yml", "DB_CONFIG")
+
+ ActiveRecord::Base.establish_connection(
+ :adapter => DB_CONFIG['adapter'],
+ :host => DB_CONFIG['host'],
+ :username => DB_CONFIG['username'],
+ :password => DB_CONFIG['password'],
+ :database => DB_CONFIG['name']
+ )
+ end
+
+ class User < ActiveRecord::Base
end
before do
@app_name = APP_CONFIG['app_name']
end
get '/' do
+ flash_message(params[:m])
erb :index
end
+
+ post '/create' do
+ redirect "/?m=success"
+ end
+
+ ##
+
+ helpers do
+
+ def flash_message(message)
+ case message
+ when "email_blank"
+ @notice = "But there is no point if you don't tell us your email."
+ when "email_invalid"
+ @notice = "The format of the email seems odd. "
+ when "success"
+ @success = "Thank you! We promise a nice surprise soon."
+ else ""
+ end
+ end
+ end
end
View
@@ -0,0 +1,5 @@
+adapter: mysql
+host: localhost
+username: root
+name: coming_soon_development
+password:
View
@@ -0,0 +1,14 @@
+class Users < ActiveRecord::Migration
+
+ def self.up
+ create_table :users do |t|
+ t.string :email
+ t.string :referer
+ end
+ add_index :users, :email
+ end
+
+ def self.down
+ drop_table :users
+ end
+end

0 comments on commit 6402e1e

Please sign in to comment.