From 51dd258fd9c2f118d68d9366dec3026b8bd42c8c Mon Sep 17 00:00:00 2001 From: "miguel.camba" Date: Mon, 7 May 2012 01:04:33 +0200 Subject: [PATCH] Totally restfull controllers --- app/assets/javascripts/common.js.coffee | 8 ++- app/controllers/issues_controller.rb | 70 +++++++------------------ app/controllers/projects_controller.rb | 55 +++++-------------- app/controllers/users_controller.rb | 8 ++- 4 files changed, 38 insertions(+), 103 deletions(-) diff --git a/app/assets/javascripts/common.js.coffee b/app/assets/javascripts/common.js.coffee index 77ff44a..59534fc 100644 --- a/app/assets/javascripts/common.js.coffee +++ b/app/assets/javascripts/common.js.coffee @@ -4,13 +4,11 @@ jQuery -> $button = $(this) $form = $button.closest('form') if $button.hasClass 'edit' - $form.find('input').each (index, input) -> - $(input).removeClass('disabled').prop('disabled', false) - # TODO: error, se está eliminando el texto del boton también + $(input).removeClass('disabled').prop('disabled', false) for input in $form.find('input') $button.removeClass('edit').addClass('btn-danger').html(' Cancelar') else - $form.find('input').each (index, input) -> - $(input).addClass('disabled').prop('disabled', true) + for input in $form.find('input') + $(input).addClass('disabled').prop 'disabled', true $(input).val($(input).data('original')) if input.type != 'submit' $button.addClass('edit').removeClass('btn-danger').html(' Editar') $form.hideValidationErrors() diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 2663af5..ac007c1 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -1,72 +1,42 @@ # -*- encoding : utf-8 -*- class IssuesController < ApplicationController - - def index - @issues = Issue.all - - respond_to do |format| - format.html - format.json { render json: @issues } - end + respond_to :html, :json + + # + # Decent exposure + # + expose(:project) { Project.find(params[:project_id]) } + expose(:issues) { project.issues } + expose(:issue) + + def index + respond_with issues end def show - @issue = Issue.find(params[:id]) - - respond_to do |format| - format.html - format.json { render json: @issue } - end + respond_with issue end def new - @issue = Issue.new - - respond_to do |format| - format.html - format.json { render json: @issue } - end + respond_with issue end def edit - @issue = Issue.find(params[:id]) + respond_with issue end def create - @issue = Issue.new(params[:issue]) - - respond_to do |format| - if @issue.save - format.html { redirect_to @issue, notice: 'Issue was successfully created.' } - format.json { render json: @issue, status: :created, location: @issue } - else - format.html { render action: "new" } - format.json { render json: @issue.errors, status: :unprocessable_entity } - end - end + issue.save + respond_with issue end def update - @issue = Issue.find(params[:id]) - - respond_to do |format| - if @issue.update_attributes(params[:issue]) - format.html { redirect_to @issue, notice: 'Issue was successfully updated.' } - format.json { head :no_content } - else - format.html { render action: "edit" } - format.json { render json: @issue.errors, status: :unprocessable_entity } - end - end + issue.save + respond_with issue end def destroy - @issue = Issue.find(params[:id]) - @issue.destroy - - respond_to do |format| - format.html { redirect_to issues_url } - format.json { head :no_content } - end + issue.destroy + respond_with issue end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index d9aaff6..d6ffa53 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,6 +1,7 @@ # -*- encoding : utf-8 -*- class ProjectsController < ApplicationController - + respond_to :html, :json + # # Decent exposure # @@ -8,65 +9,33 @@ class ProjectsController < ApplicationController expose(:project) def index - respond_to do |format| - format.html - format.json { render json: projects } - end + respond_with projects end def show - respond_to do |format| - format.html - format.json { render json: project } - end + respond_with project end def new - respond_to do |format| - format.html - format.json { render json: project } - end + respond_with project end def edit - @project = Project.find(params[:id]) + respond_with project end def create - @project = Project.new(params[:project]) - - respond_to do |format| - if @project.save - format.html { redirect_to @project, notice: 'Project was successfully created.' } - format.json { render json: @project, status: :created, location: @project } - else - format.html { render action: "new" } - format.json { render json: @project.errors, status: :unprocessable_entity } - end - end + project.save + respond_with project end def update - @project = Project.find(params[:id]) - - respond_to do |format| - if @project.update_attributes(params[:project]) - format.html { redirect_to @project, notice: 'Project was successfully updated.' } - format.json { head :no_content } - else - format.html { render action: "edit" } - format.json { render json: @project.errors, status: :unprocessable_entity } - end - end + project.save + respond_with project end def destroy - @project = Project.find(params[:id]) - @project.destroy - - respond_to do |format| - format.html { redirect_to projects_url } - format.json { head :no_content } - end + project.destroy + respond_with project end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 395752f..b67018e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,6 @@ class UsersController < ApplicationController respond_to :html, :json + # # Filters # @@ -21,15 +22,12 @@ def edit end def create - if user.save - auto_login user - flash[:notice] = "User was created successfully." - end + auto_login user if user.save respond_with user, location: root_url end def update - flash[:notice] = "User was created updated." if user.save + user.save respond_with user end