PageRenderTime 44ms CodeModel.GetById 20ms app.highlight 4ms RepoModel.GetById 16ms app.codeStats 0ms

/ruby-mode/Ruby on Rails/scaffold/scaffold.restful.nested

http://github.com/ridgetang/snippets
Unknown | 76 lines | 68 code | 8 blank | 0 comment | 0 complexity | f3134a2dfdb20fac6753f93b89eda592 MD5 | raw file
 1#name : generating a nested RESTful controller
 2#key : restful.nested
 3#group : rails.scaffold
 4#condition : (rails/controller?)
 5# --
 6before_filter :capture_${1:owner}_and_${2:`(singularize-string (rails/cur-res-title))`}
 7$0
 8def index
 9  @${2:$(pluralize-string text)} = @$1.${2:$(pluralize-string text)}
10  respond_to do |format|
11    format.html # index.html.erb
12    format.xml  { render :xml => @${2:$(pluralize-string text)} }
13  end
14end
15
16def show
17  respond_to do |format|
18    format.html # show.html.erb
19    format.xml  { render :xml => @$2 }
20  end
21end
22
23def new
24  respond_to do |format|
25    format.html # new.html.erb
26    format.xml  { render :xml => @$2 }
27  end
28end
29
30def edit
31end
32
33def create
34  @$2.update_attributes!(params[:$2])
35  flash[:notice] = t('.flash')
36  respond_to do |format|
37    format.html { redirect_to($1_$2_path(@$1, @$2)) }
38    format.xml  { render :xml => @$2, :status => :created, :location => $1_$2_url(@$1, @$2) }
39  end
40rescue ActiveRecord::RecordInvalid
41  respond_to do |format|
42    format.html { render :action => "new" }
43    format.xml  { render :xml => @$2.errors, :status => :unprocessable_entity }
44  end
45end
46
47def update
48  @$2.update_attributes!(params[:$2])
49  flash[:notice] = t('.flash')
50  respond_to do |format|
51    format.html { redirect_to($1_$2_path(@$1, @$2)) }
52    format.xml  { head :ok }
53  end
54rescue ActiveRecord::RecordInvalid
55  respond_to do |format|
56    format.html { render :action => "edit" }
57    format.xml  { render :xml => @$2.errors, :status => :unprocessable_entity }
58  end
59end
60
61def destroy
62  @$2.destroy
63  flash[:notice] = t('.flash')
64  respond_to do |format|
65    format.html { redirect_to($1_${2:$(pluralize-string text)}_path(@$1)) }
66    format.xml  { head :ok }
67  end
68end
69
70private
71
72  def capture_$1_and_$2
73    @$1 = ${1:$(decamelize-string text)}.find(params[:$1_id])
74    @$2 = @$1.${2:$(pluralize-string text)}.find(params[:id]) if params[:id]
75    @$2 ||= ${2:$(decamelize-string text)}.new(:$1 => @$1)
76  end