Permalink
Browse files

Reorganized application

- Moved everything out of main.rb
- Separated models, routes, configurations...etc
  • Loading branch information...
1 parent 8942c37 commit 3b362acaf84c35fb50ebfd2aed80d4f6b8219c3f @fuJiin committed May 24, 2010
View
@@ -1,137 +0,0 @@
-require 'rubygems'
-require 'sinatra'
-require 'haml'
-require 'rack-flash'
-require 'mongo'
-require 'mongo_mapper'
-require 'custom_logger'
-require 'uri'
-require 'joint'
-
-# require 'rack/gridfs'
-
-# mapping database #
-case ENV['RACK_ENV']
-when "production"
- conf = {:db => {'uri' => ENV['MONGOHQ_URL']}}
- MongoMapper.config = conf
- MongoMapper.connect(:db)
-when "development"
- MongoMapper.database = "mydb"
-end
-
-GRID = Mongo::Grid.new(MongoMapper.database)
-#---------------- #
-
-class Page
- include MongoMapper::Document
- key :content, String
- key :name, String
-
- validates_uniqueness_of :name
- validates_presence_of :name
-
- many :pictures
-end
-
-class Picture
- include MongoMapper::Document
- plugin Joint
-
- attachment :image
-
- def show
- return GRID.get(self.image_id).read
- end
-end
-
-# ------------------- #
-
-set :app_file, __FILE__
-set :public, File.expand_path('public/')
-set :views, File.expand_path('app/views/')
-
-enable :sessions
-use Rack::Flash
-
-
-
-# ------- stylesheets ----------- #
-get %r{/stylesheets/(.+).sass} do
- route = "../../public/sass/#{params[:captures].first}".to_sym
- content_type 'text/css', :charset => 'utf-8'
- sass route
-end
-
-# ------- site routes ----------- #
-get '/' do
- haml :index
-end
-
-# ------- page generator -------- #
-
-get '/factory' do
- @pictures = Picture.all
- haml :factory
-end
-
-post '/factory' do
- if params[:page_name] && !params[:page_name].empty?
- page = Page.create(:name => params[:page_name].gsub(/ /,"_"), :content => params[:page_html])
- flash[:notice] = "Page created"
- else
- flash[:notice] = "Error page not created"
- end
- redirect "/factory"
-end
-
-# ------- Factory pages --------- #
-
-get '/list' do
- @pages = Page.all
- haml :list
-end
-
-get '/factory/:page/delete' do
- page = Page.first(:name => params[:page].gsub(/ /,"_"))
- if page.destroy
- flash[:notice] = 'Page removed'
- redirect "/list"
- end
-end
-
-get '/factory/:page' do
- page = Page.first(:name => params[:page].gsub(/ /,"_"))
- @content = page.content
- haml :page
-end
-
-get '/upload' do
- @pictures = Picture.all
- haml :upload
-end
-
-post '/upload' do
- # @image = Image.create(params[:picture])
- @picture = Picture.create(:image => params[:image][:tempfile])
- # @picture.file = params[:picture]
- # @picture.save
- haml :upload_post
-end
-
-get '/upload/delete' do
- pictures = Picture.all
- pictures.each do |p|
- GRID.delete(p.image_id)
- p.destroy
- end
- flash[:notice] = "Pictures removed"
- redirect '/'
-end
-
-get '/picture/:id' do
- content_type 'image/jpeg'
- @picture = Picture.find(params[:id])
- @image = GRID.get(@picture.image_id)
- @image.read
-end
View
@@ -1,2 +0,0 @@
-puts File.expand_path(__FILE__)
-puts File.expand_path('../..')
View
@@ -1,3 +1,3 @@
-require './app/routes/main.rb'
+require 'eden'
run Sinatra::Application
View
@@ -0,0 +1,16 @@
+# Check Rack Environment
+case ENV['RACK_ENV']
+
+ # Production Settings for Heroku
+ when "production"
+ conf = {:db => {'uri' => ENV['MONGOHQ_URL']}}
+ MongoMapper.config = conf
+ MongoMapper.connect(:db)
+
+ # Development Settings for Local Machine
+ when "development"
+ MongoMapper.database = "mydb"
+end
+
+# Connect GridFS
+GRID = Mongo::Grid.new(MongoMapper.database)
View
@@ -0,0 +1,35 @@
+# Factory Related Routes
+
+get '/factory' do
+ @pictures = Picture.all
+ haml :factory
+end
+
+post '/factory' do
+ if params[:page_name] && !params[:page_name].empty?
+ page = Page.create(:name => params[:page_name], :content => params[:page_html])
+ flash[:notice] = "Page created"
+ else
+ flash[:notice] = "Error page not created"
+ end
+ redirect "/factory"
+end
+
+get '/factory/:page' do
+ page = Page.first(:name => params[:page].gsub(/ /,"_"))
+ @content = page.content
+ haml :page
+end
+
+get '/factory/:page/delete' do
+ page = Page.first(:name => params[:page].gsub(/ /,"_"))
+ if page.destroy
+ flash[:notice] = 'Page removed'
+ redirect "/list"
+ end
+end
+
+get '/list' do
+ @pages = Page.all
+ haml :list
+end
View
@@ -0,0 +1,4 @@
+# Put General Routes Here
+get '/' do
+ haml :index
+end
View
@@ -0,0 +1,137 @@
+# require 'rubygems'
+# require 'sinatra'
+# require 'haml'
+# require 'rack-flash'
+# require 'mongo'
+# require 'mongo_mapper'
+# require 'custom_logger'
+# require 'uri'
+# require 'joint'
+#
+# # require 'rack/gridfs'
+#
+# mapping database #
+# case ENV['RACK_ENV']
+# when "production"
+# conf = {:db => {'uri' => ENV['MONGOHQ_URL']}}
+# MongoMapper.config = conf
+# MongoMapper.connect(:db)
+# when "development"
+# MongoMapper.database = "mydb"
+# end
+#
+# GRID = Mongo::Grid.new(MongoMapper.database)
+# #---------------- #
+#
+# class Page
+# include MongoMapper::Document
+# key :content, String
+# key :name, String
+#
+# validates_uniqueness_of :name
+# validates_presence_of :name
+#
+# many :pictures
+# end
+#
+# class Picture
+# include MongoMapper::Document
+# plugin Joint
+#
+# attachment :image
+#
+# def show
+# return GRID.get(self.image_id).read
+# end
+# end
+#
+# # ------------------- #
+#
+# set :app_file, __FILE__
+# set :public, File.expand_path('public/')
+# set :views, File.expand_path('app/views/')
+#
+# enable :sessions
+# use Rack::Flash
+#
+#
+#
+# # ------- stylesheets ----------- #
+# get %r{/stylesheets/(.+).sass} do
+# route = "../../public/sass/#{params[:captures].first}".to_sym
+# content_type 'text/css', :charset => 'utf-8'
+# sass route
+# end
+#
+# # ------- site routes ----------- #
+# get '/' do
+# haml :index
+# end
+#
+# # ------- page generator -------- #
+#
+# get '/factory' do
+# @pictures = Picture.all
+# haml :factory
+# end
+#
+# post '/factory' do
+ # if params[:page_name] && !params[:page_name].empty?
+ # page = Page.create(:name => params[:page_name].gsub(/ /,"_"), :content => params[:page_html])
+ # flash[:notice] = "Page created"
+ # else
+ # flash[:notice] = "Error page not created"
+ # end
+ # redirect "/factory"
+# end
+#
+# # ------- Factory pages --------- #
+#
+# get '/list' do
+# @pages = Page.all
+# haml :list
+# end
+#
+# get '/factory/:page/delete' do
+# page = Page.first(:name => params[:page].gsub(/ /,"_"))
+# if page.destroy
+# flash[:notice] = 'Page removed'
+# redirect "/list"
+# end
+# end
+#
+# get '/factory/:page' do
+# page = Page.first(:name => params[:page].gsub(/ /,"_"))
+# @content = page.content
+# haml :page
+# end
+#
+# get '/upload' do
+# @pictures = Picture.all
+# haml :upload
+# end
+#
+# post '/upload' do
+# # @image = Image.create(params[:picture])
+# @picture = Picture.create(:image => params[:image][:tempfile])
+# # @picture.file = params[:picture]
+# # @picture.save
+# haml :upload_post
+# end
+#
+# get '/upload/delete' do
+# pictures = Picture.all
+# pictures.each do |p|
+# GRID.delete(p.image_id)
+# p.destroy
+# end
+# flash[:notice] = "Pictures removed"
+# redirect '/'
+# end
+#
+# get '/picture/:id' do
+# content_type 'image/jpeg'
+# @picture = Picture.find(params[:id])
+# @image = GRID.get(@picture.image_id)
+# @image.read
+# end
View
@@ -0,0 +1,13 @@
+# Routing for Sass Files
+get %r{/stylesheets/(.+).css} do
+ content_type 'text/css', :charset => 'utf-8'
+ sass :"../public/sass/#{params[:captures].first}"
+end
+
+# Routing for Picture files
+get '/picture/:id' do
+ content_type 'image/jpeg'
+ @picture = Picture.find(params[:id])
+ @image = GRID.get(@picture.image_id)
+ @image.read
+end
View
@@ -0,0 +1,19 @@
+get '/upload' do
+ @pictures = Picture.all
+ haml :upload
+end
+
+post '/upload' do
+ @picture = Picture.create(:image => params[:image][:tempfile])
+ haml :upload_post
+end
+
+get '/upload/delete' do
+ pictures = Picture.all
+ pictures.each do |p|
+ GRID.delete(p.image_id)
+ p.destroy
+ end
+ flash[:notice] = "Pictures removed"
+ redirect '/'
+end
Oops, something went wrong.

0 comments on commit 3b362ac

Please sign in to comment.