Skip to content

Commit

Permalink
Some minor refactoring to dry up the xml & json format handling while…
Browse files Browse the repository at this point in the history
… preserving clarity and readability.
  • Loading branch information
hopsoft committed Apr 12, 2012
1 parent 8c9f0ec commit c7ab825
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
6 changes: 4 additions & 2 deletions coast.gemspec
@@ -1,13 +1,15 @@
Gem::Specification.new do |s|
s.name = "coast"
s.version = "0.9.0"
s.version = "0.9.1"
s.date = "2012-04-06"
s.summary = "Coast"
s.description = "Resourceful behavior for Rails controllers with a Sinatra like DSL."
s.authors = [ "Nathan Hopkins" ]
s.email = "nate.hop@gmail.com"
s.email = [ "nate.hop@gmail.com" ]
s.files = [ "lib/coast.rb" ]
s.test_files = [ "spec/coast_spec.rb" ]
s.homepage ="https://github.com/hopsoft/coast"
s.add_dependency("activesupport")
s.license = "MIT"

end
49 changes: 14 additions & 35 deletions lib/coast.rb
Expand Up @@ -80,23 +80,19 @@ def abstract_authorize(*args); end
# -----------------------------------------------------------------------------------------------
# begin restful actions


# begin UI actions
def new
invoke_callback(:before_new)
@resourceful_item ||= resourceful_model.new
send(self.class.authorize_method, :new, @resourceful_item, request)
init_instance_variables

invoke_callback(:respond_to_new)
unless performed?
respond_to do |format|
format.html { render :new }
format.xml { render :xml => { :message => "Format not supported! Use the html format." } }
format.json { render :json => { :message => "Format not supported! Use the html format." } }
format_json_and_xml(format, :message => "Format not supported! Use the html format.")
end
end

invoke_callback(:after_new)
end

Expand All @@ -106,36 +102,29 @@ def edit
send(self.class.authorize_method, :edit, @resourceful_item, request)
init_instance_variables
invoke_callback(:respond_to_edit)

unless performed?
respond_to do |format|
format.html { render :edit }
format.xml { render :xml => { :message => "Format not supported! Use the html format." } }
format.json { render :json => { :message => "Format not supported! Use the html format." } }
format_json_and_xml(format, :message => "Format not supported! Use the html format.")
end
end

invoke_callback(:after_edit)
end
# end UI actions


# begin READ actions
def index
invoke_callback(:before_index)
@resourceful_list ||= resourceful_model.all
send(self.class.authorize_method, :index, @resourceful_list, request)
init_instance_variables

invoke_callback(:respond_to_index)
unless performed?
respond_to do |format|
format.html { render :index }
format.xml { render :xml => @resourceful_list }
format.json { render :json => @resourceful_list }
format_json_and_xml(format, @resourceful_list)
end
end

invoke_callback(:after_index)
end

Expand All @@ -144,45 +133,37 @@ def show
@resourceful_item ||= resourceful_model.find(params[:id])
send(self.class.authorize_method, :show, @resourceful_item, request)
init_instance_variables

invoke_callback(:respond_to_show)
unless performed?
respond_to do |format|
format.html { render :show }
format.xml { render :xml => @resourceful_item }
format.json { render :json => @resourceful_item }
format_json_and_xml(format, @resourceful_item)
end
end

invoke_callback(:after_show)
end
# end READ actions


# begin MUTATING actions
def create
invoke_callback(:before_create)
@resourceful_item ||= resourceful_model.new(params[resourceful_model.name.underscore])
send(self.class.authorize_method, :create, @resourceful_item, request)
init_instance_variables
success = @skip_db_create || @resourceful_item.save

invoke_callback(:respond_to_create)
unless performed?
respond_to do |format|
if success
flash[:notice] = "#{resourceful_model.name} was successfully created."
format.html { redirect_to(@resourceful_item) }
format.xml { render :xml => @resourceful_item, :status => :created, :location => @resourceful_item }
format.json { render :json => @resourceful_item, :status => :created, :location => @resourceful_item }
format_json_and_xml(format, @resourceful_item, :status => :created, :location => @resourceful_item)
else
format.html { render :action => "new" }
format.xml { render :xml => @resourceful_item.errors, :status => :unprocessable_entity }
format.json { render :json => @resourceful_item.errors, :status => :unprocessable_entity }
format_json_and_xml(format, @resourceful_item.errors, :status => :unprocessable_entity)
end
end
end

invoke_callback(:after_create)
end

Expand All @@ -192,23 +173,19 @@ def update
send(self.class.authorize_method, :update, @resourceful_item, request)
init_instance_variables
success = @skip_db_update || @resourceful_item.update_attributes(params[resourceful_model.name.underscore])

invoke_callback(:respond_to_update)
unless performed?
respond_to do |format|
if success
flash[:notice] = "#{resourceful_model.name} was successfully updated."
format.html { redirect_to(@resourceful_item) }
format.xml { render :xml => @resourceful_item }
format.json { render :json => @resourceful_item }
format_json_and_xml(format, @resourceful_item)
else
format.html { render :action => "edit" }
format.xml { render :xml => @resourceful_item.errors, :status => :unprocessable_entity }
format.json { render :json => @resourceful_item.errors, :status => :unprocessable_entity }
format_json_and_xml(format, @resourceful_item.errors, :status => :unprocessable_entity)
end
end
end

invoke_callback(:after_update)
end

Expand All @@ -218,17 +195,14 @@ def destroy
send(self.class.authorize_method, :destroy, @resourceful_item, request)
init_instance_variables
@resourceful_item.destroy unless @skip_db_destroy

invoke_callback(:respond_to_destroy)
unless performed?
flash[:notice] = "#{resourceful_model.name} was successfully destroyed" if @resourceful_item.destroyed?
respond_to do |format|
format.html { redirect_to root_url }
format.xml { render :xml => @resourceful_item }
format.json { render :json => @resourceful_item }
format_json_and_xml(format, @resourceful_item)
end
end

invoke_callback(:after_destroy)
end
# end MUTATING actions
Expand Down Expand Up @@ -263,4 +237,9 @@ def invoke_callback(name)
send(name) if respond_to?(name)
end

def format_json_and_xml(format, data, options={})
format.xml { render({:xml => data}.merge(options)) }
format.json { render({:json => data}.merge(options)) }
end

end

0 comments on commit c7ab825

Please sign in to comment.