Permalink
Browse files

Merge branch 'feature/formaction_take2' into develop

  • Loading branch information...
2 parents 39109a7 + d45ee53 commit 17c1f47c10102a9bacebf92195688b99eb7055d3 Javier Martín committed Feb 9, 2012
@@ -3,7 +3,7 @@ jQuery.fn.autosaveHighlightForm = ->
form = $(this)
form.autosaveForm(
target: '#actualizado'
- data: {draft: true}
+ url: $('input[formaction*="draft"]').attr("formaction")
success: ->
$('#actualizado time', form).effect "highlight", {}, 3000
$('input[name*="borrador_id"]', form).val(
@@ -0,0 +1,3 @@
+if(($('input[formaction]').length > 0) && !('formAction' in document.createElement('input'))) {
+ $('<div id="error_formaction">¡Atención! <strong>¡Tu navegador no es compatible con la edición de este formulario!</strong> Puedes usar navegadores compatibles, como Firefox (versión 4 o más reciente), Chrome (versión 10 o más reciente) o versiones recientes de Opera</div>').insertBefore("#content")
+}
@@ -3,19 +3,19 @@ jQuery.fn.liveForm = ->
form = $(this)
$('#main').prepend('<div id="preview"></div>')
- $('input[name=preview]').remove()
+ preview_button = $('input[formaction*="preview"]')
+ preview_button.remove()
send_form = (form) ->
form.ajaxSubmit(
+ url: preview_button.attr("formaction")
beforeSend: ->
$('#preview').addCargando()
success: (data) ->
$('#preview').html($('#preview', data).html())
.removeCargando().imagenesQuitables().worldMaps()
$('#sidebar').remove()
$('#sidebar', data).prependTo('#extra')
-
- data: { preview: true }
)
$('input[type=text], textarea', form).typeWatch(
@@ -0,0 +1,11 @@
+@import "compass/css3"
+
+#error_formaction
+ background: no-repeat 0.2em center
+ background-color: #fdc
+ background-image: image-url("error.png")
+ +background-size(auto 90%)
+ border: solid 1px red
+ line-height: 1.5
+ padding: 0.3em 0.5em 0.3em 3em
+ margin: 1em 0
@@ -2,19 +2,17 @@
class PaginasController < ApplicationController
# Tengo que declarar antes el JS para que las peticiones AJAX respondan así.
- respond_to :js, only: [:index, :search, :destroy]
+ respond_to :js, only: [:index, :search, :destroy, :save_draft, :preview]
respond_to :html
public_actions :show, :search
resource :pagina
- before_filter :params_updated_by, only: [:create, :update]
- before_filter :find_pagina, only: [:edit, :update, :destroy, :historial]
+ before_filter :params_updated_by, only: [:create, :update, :save_draft, :publish]
+ before_filter :find_pagina, only: [:edit, :update, :destroy, :historial, :publish]
before_filter :new_pagina, only: [:new, :create]
- before_filter :asignar_cajas, only: [:create, :update]
- before_filter :preview, only: [:create, :update]
- before_filter :save_draft, only: [:create, :update]
- before_filter :publish_draft, only: [:create, :update]
+ before_filter :find_or_new_pagina, only: [:preview, :save_draft]
+ before_filter :asignar_cajas, only: [:create, :update, :save_draft]
before_filter :paginate_paginas, only: :index
def index
@@ -44,6 +42,28 @@ def update
respond_with @pagina
end
+ def save_draft
+ @pagina.save_draft(params[:pagina])
+ respond_with @pagina.draft, location: edit_pagina_path(@pagina.draft)
+ end
+
+ def publish
+ if @pagina.publish params[:pagina]
+ respond_with @pagina.published
+ else
+ respond_with @pagina
+ end
+ end
+
+ def preview
+ # HACK: asignar caja_ids guarda la relación en la BD. Ver:
+ # https://github.com/rails/rails/issues/674
+ attributes = params[:pagina].clone
+ @cajas = Caja.find_all_by_id(attributes.delete(:caja_ids) || [])
+ @pagina.attributes = attributes
+ respond_with @pagina
+ end
+
def destroy
if @pagina.destroy
@deshacer = deshacer_borrado_path(@pagina)
@@ -72,42 +92,11 @@ def asignar_cajas
end
end
- # TODO: pasar a acción independiente en cuanto funcione "formaction".
- def preview
- if params[:preview]
- # HACK: asignar caja_ids guarda la relación en la BD. Ver:
- # https://rails.lighthouseapp.com/projects/8994/tickets/4521
- attributes = params[:pagina].clone
- @cajas = Caja.find_all_by_id(attributes.delete(:caja_ids) || [])
- @pagina.attributes = attributes
- if request.xhr?
- render 'preview.js'
- else
- render 'preview'
- end
- end
- end
-
- def save_draft
- if params[:draft]
- if request.xhr?
- @pagina.save_draft(params[:pagina])
- render 'borrador.js'
- else
- flash[:notice] = 'Borrador guardado.' if @pagina.save_draft(params[:pagina])
- redirect_to edit_pagina_path(@pagina.draft)
- end
- end
- end
-
- def publish_draft
- if params[:publish]
- if @pagina.publish(params[:pagina])
- flash[:notice] = 'Borrador publicado.'
- redirect_to(pagina_path(@pagina.published))
- else
- render 'edit'
- end
+ def find_or_new_pagina
+ if params[:id]
+ find_pagina
+ else
+ new_pagina
end
end
end
@@ -9,6 +9,21 @@ def edit_pagina_title(pagina)
end
end
+ def save_draft_path(pagina)
+ if pagina.new_record?
+ save_draft_paginas_path
+ else
+ save_draft_pagina_path(pagina)
+ end
+ end
+
+ def preview_path(pagina)
+ if pagina.new_record?
+ preview_paginas_path
+ else
+ preview_pagina_path(pagina)
+ end
+ end
def time_tag(date_or_time, *args)
options = args.extract_options!
View
@@ -130,7 +130,9 @@ def set_borrador
end
def copy_errors(pagina)
- pagina.errors.each { |field, message| errors.add(field, message) }
+ unless pagina.errors == errors
+ pagina.errors.each { |field, message| errors.add(field, message) }
+ end
end
def titulo_nil_si_blank
@@ -18,12 +18,14 @@
= buttons do
- if pagina.borrador?
- = form.submit 'Publicar', name: 'publish'
+ = form.submit 'Publicar', formaction: publish_pagina_path(pagina)
- else
= form.button :submit
- = form.submit 'Guardar borrador', name: 'draft', id: 'draft_submit'
- = form.submit 'Vista previa', name: 'preview', id: 'preview_submit'
+ = form.submit 'Guardar borrador', id: 'draft_submit',
+ formaction: save_draft_path(pagina)
+ = form.submit 'Vista previa', id: 'preview_submit',
+ formaction: preview_path(pagina)
%section#fotos
%header
@@ -12,3 +12,7 @@ es:
notice: "%{resource_name} se envió correctamente."
restore:
notice: "%{resource_name} se recuperó correctamente."
+ save_draft:
+ notice: "Borrador guardado"
+ publish:
+ notice: "Borrador publicado"
View
@@ -38,10 +38,16 @@
match '/autocomplete' => "ajax_form#autocomplete"
match '/ayuda-textile' => "static#ayuda_textile"
- resources :paginas, only: [:index, :create]
+ resources :paginas, only: [:index, :create] do
+ post :save_draft, on: :collection
+ post :preview, on: :collection
+ end
resources :paginas, path: "", except: [:index, :create] do
- get :search, on: :collection
- get :historial, on: :member
+ get :search, on: :collection
+ get :historial, on: :member
+ put :save_draft, on: :member
+ put :publish, on: :member
+ put :preview, on: :member
end
root to: "portadas#principal"
Oops, something went wrong.

0 comments on commit 17c1f47

Please sign in to comment.