PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms 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. # --
  6. before_filter :capture_${1:owner}_and_${2:`(singularize-string (rails/cur-res-title))`}
  7. $0
  8. def 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
  14. end
  15. def show
  16. respond_to do |format|
  17. format.html # show.html.erb
  18. format.xml { render :xml => @$2 }
  19. end
  20. end
  21. def new
  22. respond_to do |format|
  23. format.html # new.html.erb
  24. format.xml { render :xml => @$2 }
  25. end
  26. end
  27. def edit
  28. end
  29. def create
  30. @$2.update_attributes!(params[:$2])
  31. flash[:notice] = t('.flash')
  32. respond_to do |format|
  33. format.html { redirect_to($1_$2_path(@$1, @$2)) }
  34. format.xml { render :xml => @$2, :status => :created, :location => $1_$2_url(@$1, @$2) }
  35. end
  36. rescue ActiveRecord::RecordInvalid
  37. respond_to do |format|
  38. format.html { render :action => "new" }
  39. format.xml { render :xml => @$2.errors, :status => :unprocessable_entity }
  40. end
  41. end
  42. def update
  43. @$2.update_attributes!(params[:$2])
  44. flash[:notice] = t('.flash')
  45. respond_to do |format|
  46. format.html { redirect_to($1_$2_path(@$1, @$2)) }
  47. format.xml { head :ok }
  48. end
  49. rescue ActiveRecord::RecordInvalid
  50. respond_to do |format|
  51. format.html { render :action => "edit" }
  52. format.xml { render :xml => @$2.errors, :status => :unprocessable_entity }
  53. end
  54. end
  55. def destroy
  56. @$2.destroy
  57. flash[:notice] = t('.flash')
  58. respond_to do |format|
  59. format.html { redirect_to($1_${2:$(pluralize-string text)}_path(@$1)) }
  60. format.xml { head :ok }
  61. end
  62. end
  63. private
  64. def capture_$1_and_$2
  65. @$1 = ${1:$(decamelize-string text)}.find(params[:$1_id])
  66. @$2 = @$1.${2:$(pluralize-string text)}.find(params[:id]) if params[:id]
  67. @$2 ||= ${2:$(decamelize-string text)}.new(:$1 => @$1)
  68. end