Module: Gitgo::Rest

Included in:
Controllers::Code, Controllers::Issue
Defined in:
lib/gitgo/rest.rb

Instance Method Summary collapse

Instance Method Details

#attrsObject



7
8
9
# File 'lib/gitgo/rest.rb', line 7

def attrs
  request['doc'] || {'at' => session_head}
end

#create(sha = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitgo/rest.rb', line 21

def create(sha=nil)
  return(sha.nil? ? preview : show(sha)) if preview?
  
  doc = model.save(attrs)
  
  parents = request['parents']
  if parents.nil? || parents.empty?
    doc.create
  else
    parents = [parents] unless parents.kind_of?(Array)
    parents.collect! do |parent|
      model[parent] or raise "invalid parent: #{parent.inspect}"
    end
    doc.link_to(*parents)
  end
  
  doc.commit!
  redirect_to_doc(doc)
end

#destroy(sha) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/gitgo/rest.rb', line 72

def destroy(sha)
  doc = model.delete(sha).commit!
  
  if doc.graph_head?
    redirect "/#{model.type}"
  else
    redirect_to_doc(doc)
  end
end

#edit(sha) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/gitgo/rest.rb', line 41

def edit(sha)
  unless doc = model.read(sha)
    raise "unknown #{model.type}: #{sha.inspect}"
  end
  
  doc.merge!(attrs)
  erb :edit, :locals => {:doc => doc}
end

#modelObject

Raises:

  • (NotImplementedError)


3
4
5
# File 'lib/gitgo/rest.rb', line 3

def model
  raise NotImplementedError
end

#previewObject



15
16
17
18
19
# File 'lib/gitgo/rest.rb', line 15

def preview
  doc = model.new(attrs)
  # doc.normalize!
  erb :new, :locals => {:doc => doc}
end

#preview?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/gitgo/rest.rb', line 11

def preview?
  request['preview'] == 'true'
end

#redirect_to_doc(doc) ⇒ Object



82
83
84
85
# File 'lib/gitgo/rest.rb', line 82

def redirect_to_doc(doc)
  sha = doc.graph_head? ? doc.graph.head : "#{doc.graph.head}##{doc.sha}"
  redirect "/#{model.type}/#{sha}"
end

#show(sha) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gitgo/rest.rb', line 57

def show(sha)
  unless doc = model.read(sha)
    raise "unknown #{model.type}: #{sha.inspect}"
  end
  
  new_doc = doc.inherit(attrs)
  # new_doc.normalize!
  
  erb :show, :locals => {
    :doc => doc,
    :new_doc => new_doc,
    :active_sha => session_head
  }
end

#update(sha) ⇒ Object



50
51
52
53
54
55
# File 'lib/gitgo/rest.rb', line 50

def update(sha)
  return edit(sha) if preview?
  
  doc = model.update(sha, attrs).commit!
  redirect_to_doc(doc)
end