Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 122 lines (106 sloc) 3.361 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
%w(rubygems sinatra dm-core dm-validations fileutils do_sqlite3).each { |f| require f }
Dir[File.join(File.dirname(__FILE__), *%w[lib *])].each { |f| require f }

configure do
  DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}entry_systems.db")
  DataMapper.auto_migrate!
end

layout 'layout.erb'

get '/' do
  @entry_systems = EntrySystem.all
  erb :entry_systems_index
end

get '/entry_systems/new' do
  @entry_system = EntrySystem.new
  erb :entry_systems_new
end

get '/entry_systems/:id/image/new' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/entry_systems/new" unless @entry_system
  @image = Image.new
  erb :images_new
end

get '/entry_systems/:id/pdf_manual/new' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/entry_systems/new" unless @entry_system
  @pdf_manual = PdfManual.new
  erb :pdf_manuals_new
end

get '/entry_systems/:id/pdf_manual' do
  @entry_system = EntrySystem.get(params[:id])
  send_file(File.join(File.dirname(__FILE__), *%W[public pdfs #{@entry_system.pdf_manual.filename}]), :type => "application/pdf", :filename => "#{@entry_system.pdf_manual.filename}.pdf")
end

get '/entry_systems/:id' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/" unless @entry_system
  erb :entry_systems_show
end

get '/entry_systems/:id/edit' do
  @entry_system = EntrySystem.get(params[:id])
  erb :entry_systems_edit
end

post '/entry_systems' do
  @entry_system = EntrySystem.new(params)
  if @entry_system.save
    redirect "/"
  else
    erb :entry_systems_new
  end
end

post '/entry_systems/:id/image' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/entry_systems/new" unless @entry_system
  @image = Image.new(
    :filename => @entry_system.name.gsub(/\W/, '_'),
    :data => params[:image][:tempfile].read
  )
  if @image.save
    @entry_system.image = @image
    @entry_system.save
    redirect "/entry_systems/#{@entry_system.id}"
  else
    erb :images_new
  end
end

post '/entry_systems/:id/pdf_manual' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/entry_systems/new" unless @entry_system
  @pdf_manual = PdfManual.new(
    :link => params[:link],
    :filename => @entry_system.name.gsub(/\W/, '_'),
    :data => params[:pdf_manual][:tempfile]
  )
  if @pdf_manual.save
    @entry_system.pdf_manual = @pdf_manual
    @entry_system.save
    redirect "/entry_systems/#{@entry_system.id}"
  else
    erb :pdf_manuals_new
  end
end

put '/entry_systems/:id' do
  @entry_system = EntrySystem.get(params[:id])
  attributes = params.dup
  attributes.delete(:id)
  @entry_system.update_attributes(attributes)
  redirect "/entry_systems/#{@entry_system.id}"
end

delete '/entry_systems/:id' do
  es = EntrySystem.get(params[:id])
  es.image.destroy if es.image
  es.pdf_manual.destroy if es.pdf_manual
  es.destroy
  redirect "/"
end

delete '/entry_systems/:id/image' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/" unless @entry_system
  @entry_system.image.destroy if @entry_system.image
  @entry_system.pdf_manual.destroy if @entry_system.pdf_manual
  redirect "/entry_systems/#{@entry_system.id}"
end

delete '/entry_systems/:id/pdf_manual' do
  @entry_system = EntrySystem.get(params[:id])
  redirect "/" unless @entry_system
  @entry_system.pdf_manual.destroy if @entry_system.pdf_manual
  redirect "/entry_systems/#{@entry_system.id}"
end
Something went wrong with that request. Please try again.