Module: Coupler::Extensions::Projects

Defined in:
lib/coupler/extensions/projects.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
# File 'lib/coupler/extensions/projects.rb', line 4

def self.registered(app)
  app.before do
    md = request.path_info.match(%r{/projects/(\d+)/?})
    if md
      # NOTE: Using regex matching here sucks, but apparently Sinatra
      #       calls before filters before params are parsed.
      @project = Models::Project[:id => md[1]]
      raise ProjectNotFound   unless @project
      @project.touch!
    end
  end

  app.get "/projects" do
    @projects = Models::Project.order(:id)
    @resource_counts = Models::Resource.count_by_project
    @scenario_counts = Models::Scenario.count_by_project
    erb 'projects/index'.to_sym
  end

  app.get "/projects/new" do
    @project = Models::Project.new
    erb 'projects/form'.to_sym
  end

  app.post "/projects" do
    @project = Models::Project.create(params['project'])
    flash[:newly_created] = true
    redirect "/projects/#{@project.id}"
  end

  app.get "/projects/:project_id" do
    @resources = @project.resources
    @scenarios = @project.scenarios
    erb 'projects/show'.to_sym
  end

  app.get "/projects/:project_id/edit" do
    erb 'projects/form'.to_sym
  end

  app.put "/projects/:project_id" do
    @project.set(params[:project])
    if @project.valid?
      @project.save
      redirect "/projects/#{@project.id}"
    else
      erb 'projects/form'.to_sym
    end
  end

  app.delete "/projects/:project_id" do
    @project.delete_versions_on_destroy = true  if params[:nuke] == "true"
    @project.destroy
    redirect '/projects'
  end
end