Skip to content

Instantly share code, notes, and snippets.

View sushant12's full-sized avatar
๐Ÿ˜Ž
Focusing

Suss Buzz sushant12

๐Ÿ˜Ž
Focusing
  • Kathmandu
View GitHub Profile
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require_relative 'lib/todo'
require_relative 'app/controllers/tasks_controller'
require_relative 'app/models/task'
use Rack::Static, root: 'public', urls: ['/images', '/js', '/css']
use Rack::ContentLength
require_relative 'todo/application'
require_relative 'todo/base_controller'
require_relative 'todo/router'
module Todo
autoload :Application, 'todo/application'
autoload :BaseController, 'todo/base_controller'
autoload :Router, 'todo/router'
end
module Todo
class Application
class << self
attr_accessor :env
def call(env)
self.env = env
Rack::Response.new( *dispatch)
# if(env['PATH_INFO'] == '/')
# Rack::Response.new(TasksController.new.index)
# end
module Todo
class Router
attr_reader :app
ROUTES = {
'/' => {'controller' => 'tasks', 'action' => 'index'},
'/save_task' => {'controller' => 'tasks', 'action' => 'save'},
'/edit' => {'controller' => 'tasks', 'action' => 'edit'},
'/update' => {'controller' => 'tasks', 'action' => 'update'},
'/delete' => {'controller' => 'tasks', 'action' => 'destroy'}
class TasksController < Todo::BaseController
def index
@tasks = Task.all
render("tasks/index")
end
def edit
@task = Task.where(params["id"])
render("tasks/edit")
module Todo
class BaseController
attr_reader :env
def initialize(env)
@env = env
end
def params
Rack::Request.new(env).params
class Task
DB = PG.connect :hostaddr => "127.0.0.1", :port => 5432, :dbname => 'testdb', :user => "postgres", :password => "postgres"
#uncomment while deploying to heroku
#DB = PG.connect ENV["HEROKU_POSTGRESQL_SILVER_URL"]
# uncomment to create pg database
# DB = PG.connect(hostaddr: "127.0.0.1", port: 5432, dbname: 'postgres', user: 'postgres', password: "postgres")
# DB.exec("CREATE DATABASE testdb")
# uncomment to create table
# DB.exec "DROP TABLE IF EXISTS tasks"
source 'https://rubygems.org'
gem 'activerecord', '~> 5.0', '>= 5.0.0.1'
gem 'pg'
gem 'sinatra'
require 'active_record'
require 'pg'
require 'sinatra'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
username: 'postgres',
password: 'postgres',
database: 'testdb'
<html>
<head><title>Todo</title></head>
<link rel="stylesheet" href="/css/style.css">
<body>
<%= yield %>
<script src="/js/main.js"></script>
</body>
</html>