Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Added the snippets folder.
Browse files Browse the repository at this point in the history
Make aliases `camelize-string' -> `string-ext/camelize' and `decamelize-string' -> `string-ext/decamelize'.
Added functions for snippets: `rails/cur-res-title', `rails/controller?', `rails/controller-spec?'
  • Loading branch information
dmexe committed Mar 6, 2009
1 parent c7c2f35 commit 328dfe4
Show file tree
Hide file tree
Showing 7 changed files with 660 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rails-lib.el
Expand Up @@ -260,4 +260,20 @@ else return nil"
(defmacro rails/toggle-environment-menu-func (env)
`'(lambda() (interactive) (rails/set-default-environment ,(symbol-value env))))

;;; ---------------------------------------------------------
;;; - Snippet support functions
;;;

(defun rails/controller? ()
(rails/with-current-buffer
(eq 'controller (rails/resource-buffer-type rails/current-buffer))))

(defun rails/controller-spec? ()
(rails/with-current-buffer
(eq 'controller-spec (rails/resource-buffer-type rails/current-buffer))))

(defun rails/cur-res-title ()
(rails/with-current-buffer
(rails/resource-buffer-title rails/current-buffer)))

(provide 'rails-lib)
3 changes: 3 additions & 0 deletions rails-resources.el
Expand Up @@ -282,6 +282,9 @@
;; dir
(when-bind (dir (rails/resource-dir resource))
(setq file (concat dir file)))
;; skip-file-suffix
(when-bind (skip-file-suffix (rails/resource-skip-file-suffix resource))
(setq file (concat file skip-file-suffix)))
;; file-suffix
(when-bind (file-suffix (rails/resource-file-suffix resource))
(setq file (concat file file-suffix)))
Expand Down
75 changes: 75 additions & 0 deletions snippets/ruby-mode/controller.restful.basic
@@ -0,0 +1,75 @@
#name : generating a RESTful controller
#key : restful
#group : rails.controller
#condition : (rails/controller?)
# --
before_filter :capture_${1:`(singularize-string (rails/cur-res-title))`}
$0
def index
@${1:$(pluralize-string text)} = ${2:`(decamelize-string (singularize-string (rails/cur-res-title)))`}.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @${1:$(pluralize-string text)} }
end
end

def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @$1 }
end
end

def new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @$1 }
end
end

def edit
end

def create
@$1.update_attributes!(params[:$1])
flash[:notice] = t('.flash')
respond_to do |format|
format.html { redirect_to($1_path(@$1)) }
format.xml { render :xml => @$1, :status => :created, :location => $1_url(@$1) }
end
rescue ActiveRecord::RecordInvalid
respond_to do |format|
format.html { render :action => "new" }
format.xml { render :xml => @$1.errors, :status => :unprocessable_entity }
end
end

def update
@$1.update_attributes!(params[:$1])
flash[:notice] = t('.flash')
respond_to do |format|
format.html { redirect_to($1_path(@$1)) }
format.xml { head :ok }
end
rescue ActiveRecord::RecordInvalid
respond_to do |format|
format.html { render :action => "edit" }
format.xml { render :xml => @$1.errors, :status => :unprocessable_entity }
end
end

def destroy
@$1.destroy
flash[:notice] = t('.flash')
respond_to do |format|
format.html { redirect_to(${1:$(pluralize-string text)}_path) }
format.xml { head :ok }
end
end

private

def capture_$1
@$1 = $2.find(params[:id]) if params[:id]
@$1 ||= $2.new
end
76 changes: 76 additions & 0 deletions snippets/ruby-mode/controller.restful.nested
@@ -0,0 +1,76 @@
#name : generating a nested RESTful controller
#key : restful.nested
#group : rails.controller
#condition : (rails/controller?)
# --
before_filter :capture_${1:owner}_and_${2:`(singularize-string (rails/cur-res-title))`}
$0
def index
@${2:$(pluralize-string text)} = @$1.${2:$(pluralize-string text)}
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @${2:$(pluralize-string text)} }
end
end

def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @$2 }
end
end

def new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @$2 }
end
end

def edit
end

def create
@$2.update_attributes!(params[:$2])
flash[:notice] = t('.flash')
respond_to do |format|
format.html { redirect_to($1_$2_path(@$1, @$2)) }
format.xml { render :xml => @$2, :status => :created, :location => $1_$2_url(@$1, @$2) }
end
rescue ActiveRecord::RecordInvalid
respond_to do |format|
format.html { render :action => "new" }
format.xml { render :xml => @$2.errors, :status => :unprocessable_entity }
end
end

def update
@$2.update_attributes!(params[:$2])
flash[:notice] = t('.flash')
respond_to do |format|
format.html { redirect_to($1_$2_path(@$1, @$2)) }
format.xml { head :ok }
end
rescue ActiveRecord::RecordInvalid
respond_to do |format|
format.html { render :action => "edit" }
format.xml { render :xml => @$2.errors, :status => :unprocessable_entity }
end
end

def destroy
@$2.destroy
flash[:notice] = t('.flash')
respond_to do |format|
format.html { redirect_to($1_${2:$(pluralize-string text)}_path(@$1)) }
format.xml { head :ok }
end
end

private

def capture_$1_and_$2
@$1 = ${1:$(decamelize-string text)}.find(params[:$1_id])
@$2 = @$1.${2:$(pluralize-string text)}.find(params[:id]) if params[:id]
@$2 ||= ${2:$(decamelize-string text)}.new(:$1 => @$1)
end

0 comments on commit 328dfe4

Please sign in to comment.