PageRenderTime 81ms CodeModel.GetById 40ms app.highlight 1ms RepoModel.GetById 39ms 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# --
 6include ExtScaffold
 7
 8rescue_from ActiveRecord::RecordNotFound do |exception|
 9  render :json => { :success => false }, :status => :not_found
10end
11
12before_filter :find_${1:`(singularize-string (rails/cur-res-title))`}, :only => [ :update, :destroy ]
13before_filter :find_${1:$(pluralize-string text)}, :only => [ :index ]
14belong_to_right "ADMIN_USER"
15$0
16# GET /${1:$(pluralize-string text)}
17# GET /${1:$(pluralize-string text)}.ext_json
18def index
19  respond_to do |format|
20    format.html     # index.html.erb (no data required)
21    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))) }
22    format.json { render @${1:$(pluralize-string text)}.to_json }
23  end
24end
25
26# POST /${1:$(pluralize-string text)}
27def create
28  @$1 = $2.new(params[:$1])
29  render :json => @$1.to_ext_json(:success => @$1.save)
30end
31
32# PUT /${1:$(pluralize-string text)}/1
33def update
34  render :json => @$1.to_ext_json(:success => @$1.update_attributes(params[:$1]))
35end
36
37# DELETE /${1:$(pluralize-string text)}/1
38def destroy
39  @$1.destroy
40  head :ok
41end
42
43protected
44
45def find_$1
46  @$1 = $2.find(params[:id])
47end
48
49def find_${1:$(pluralize-string text)}
50  pagination_state = update_pagination_state_with_params!($2)
51  @${1:$(pluralize-string text)} = $2.find(:all, options_from_pagination_state(pagination_state).merge(options_from_search($2)))
52end