Skip to content

Instantly share code, notes, and snippets.

@mshock
Created May 27, 2014 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshock/fa58cc47769a95cbd15d to your computer and use it in GitHub Desktop.
Save mshock/fa58cc47769a95cbd15d to your computer and use it in GitHub Desktop.
sinatra + DM
#! ruby
require 'sinatra'
require 'data_mapper'
require 'haml'
require 'sinatra/reloader'
debug = true
if debug
DataMapper::Logger.new($stdout, :debug)
end
DataMapper::setup(:default, "sqlite://#{Dir.pwd}/rowcounts.db")
class Server
include DataMapper::Resource
property :id, Serial
property :name, String
property :ip, String
has n, :serverTables
has n, :tables, :through => :serverTables
has n, :counts
end
class Table
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :serverTables
has n, :servers, :through => :serverTables
has n, :counts
end
class Count
include DataMapper::Resource
property :id, Serial
property :count, Integer
property :timestamp, DateTime
belongs_to :server
belongs_to :table
end
class ServerTable
include DataMapper::Resource
property :id, Serial
property :enabled, Boolean
belongs_to :server
belongs_to :table
end
DataMapper.finalize
get '/' do
haml :index
end
get '/tables' do
@tables = Table.all
haml :tables
end
# trying to select from a subquery here! it builds:
# SELECT "tables"."id", "tables"."name" FROM "tables" INNER JOIN "server_tables" ON "tables"."id" = "server_tables"."table_id" INNER JOIN "servers" ON "server_tables"."server_id" = "servers"."id" WHERE "server_tables"."server_id" = 1 GROUP BY "tables"."id", "tables"."name" ORDER BY "tables"."id"
get '/tables/:server_id' do
server_id = params[:server_id]
@server = Server.get server_id
#@tables = Table.all("servers.id"=> server_id)
@tables = @server.tables(:fields => [:id, :name, 'server_tables.enabled'])
#@tables = @server.tables
haml :tables
end
get '/counts' do
@counts = Count.all
haml :counts
end
get '/servers' do
@servers = Server.all
haml :servers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment