PageRenderTime 22ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ruby-mode/Ruby on Rails/scaffold/scaffold.extscaffold.1

http://github.com/ridgetang/snippets
Unknown | 52 lines | 44 code | 8 blank | 0 comment | 0 complexity | c3f8f7f0d9dca90316838a56c1b6f597 MD5 | raw file
  1. # name : ExtScaffold Controller
  2. # key : extscaffold.1
  3. # group : rails.scaffold
  4. # contributor : crazycode@gmail.com
  5. # --
  6. include ExtScaffold
  7. rescue_from ActiveRecord::RecordNotFound do |exception|
  8. render :json => { :success => false }, :status => :not_found
  9. end
  10. before_filter :find_${1:`(singularize-string (rails/cur-res-title))`}, :only => [ :update, :destroy ]
  11. before_filter :find_${1:$(pluralize-string text)}, :only => [ :index ]
  12. belong_to_right "ADMIN_USER"
  13. $0
  14. # GET /${1:$(pluralize-string text)}
  15. # GET /${1:$(pluralize-string text)}.ext_json
  16. def index
  17. respond_to do |format|
  18. format.html # index.html.erb (no data required)
  19. format.ext_json { render :json => @${1:$(pluralize-string text)}.to_ext_json(:class => ${2:`(decamelize-string (singularize-string (rails/cur-res-title)))`}, :methods => [:role_names, :role_ids], :count => $2.count(options_from_search($2))) }
  20. format.json { render @${1:$(pluralize-string text)}.to_json }
  21. end
  22. end
  23. # POST /${1:$(pluralize-string text)}
  24. def create
  25. @$1 = $2.new(params[:$1])
  26. render :json => @$1.to_ext_json(:success => @$1.save)
  27. end
  28. # PUT /${1:$(pluralize-string text)}/1
  29. def update
  30. render :json => @$1.to_ext_json(:success => @$1.update_attributes(params[:$1]))
  31. end
  32. # DELETE /${1:$(pluralize-string text)}/1
  33. def destroy
  34. @$1.destroy
  35. head :ok
  36. end
  37. protected
  38. def find_$1
  39. @$1 = $2.find(params[:id])
  40. end
  41. def find_${1:$(pluralize-string text)}
  42. pagination_state = update_pagination_state_with_params!($2)
  43. @${1:$(pluralize-string text)} = $2.find(:all, options_from_pagination_state(pagination_state).merge(options_from_search($2)))
  44. end