Skip to content

Commit

Permalink
Adds notification on successful create/update. Fixes #1315.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice TEXIER committed Nov 9, 2016
1 parent 283d492 commit 5d0a361
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 27 deletions.
29 changes: 12 additions & 17 deletions app/controllers/backend/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,27 @@ def find_and_check(*args)
record
end

def save_and_redirect(record, options = {}, &_block)
def save_and_redirect(record, options = {})
record.attributes = options[:attributes] if options[:attributes]
ActiveRecord::Base.transaction do
if options[:saved] || record.send(:save)
yield record if block_given?
response.headers['X-Return-Code'] = 'success'
response.headers['X-Saved-Record-Id'] = record.id.to_s
if params[:dialog]
head :ok
else
# TODO: notify if success
if options[:url] == :back
redirect_to_back
elsif params[:redirect]
redirect_to params[:redirect]
else
url = options[:url]
record.reload
if url.is_a? Hash
url.each do |k, v|
url[k] = (v.is_a?(CodeString) ? record.send(v) : v)
end
end
redirect_to(url)
return true
end

notify_success options[:notify] if options[:notify]

url = options[:url]
record.reload
if url.is_a? Hash
url.each do |k, v|
url[k] = (v.is_a?(CodeString) ? record.send(v) : v)
end
end
redirect_to(url)
return true
end
end
Expand Down
32 changes: 26 additions & 6 deletions app/controllers/concerns/restfully_manageable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ClassMethods
def manage_restfully(defaults = {})
name = controller_name
path = controller_path
options = defaults.extract!(:t3e, :creation_t3e, :redirect_to, :xhr, :destroy_to, :subclass_inheritance, :partial, :multipart, :except, :only, :cancel_url, :scope)
options = defaults.extract!(:t3e, :creation_t3e, :redirect_to, :xhr, :destroy_to, :subclass_inheritance, :partial, :multipart, :except, :only, :cancel_url, :scope, :identifier)
after_save_url = options[:redirect_to]
after_destroy_url = options[:destroy_to] || :index
actions = [:index, :show, :new, :create, :edit, :update, :destroy]
Expand All @@ -16,6 +16,7 @@ def manage_restfully(defaults = {})
record_name = name.to_s.singularize
model_name = name.to_s.classify
model = model_name.constantize
columns = model.columns_definition.keys

if after_save_url.blank?
if instance_methods(true).include?(:show) || actions.include?(:show)
Expand All @@ -25,16 +26,21 @@ def manage_restfully(defaults = {})
end
end

notify = true
if after_save_url == :show
after_save_url = "{action: :show, id: 'id'.c}".c
after_save_url = "{ action: :show, id: 'id'.c }".c
notify = false
elsif after_save_url == :index
after_save_url = '{action: :index}'.c
after_save_url = '{ action: :index }'.c
elsif after_save_url.is_a?(CodeString)
after_save_url.gsub!(/RECORD/, "@#{record_name}")
elsif after_save_url.is_a?(Hash)
after_save_url = after_save_url.inspect.gsub(/RECORD/, "@#{record_name}")
end

options[:identifier] ||= %w(name number id).detect { |i| columns.include?(i) }
raise 'Need a :identifier option' if options[:identifier].blank?

render_form_options = []
render_form_options << "partial: '#{options[:partial]}'" if options[:partial]
render_form_options << 'multipart: true' if options[:multipart]
Expand All @@ -43,7 +49,7 @@ def manage_restfully(defaults = {})
else
:back
end
render_form_options << "locals: {cancel_url: #{options[:cancel_url].inspect}}"
render_form_options << "locals: { cancel_url: #{options[:cancel_url].inspect} }"
render_form = 'render(' + render_form_options.join(', ') + ')'

after_save_url ||= options[:cancel_url].inspect
Expand Down Expand Up @@ -154,7 +160,14 @@ def manage_restfully(defaults = {})
code << "def create\n"
# code << " raise params.inspect.red\n"
code << " @#{record_name} = resource_model.new(permitted_params)\n"
code << " return if save_and_redirect(@#{record_name}#{', url: (' + after_save_url + ')' if after_save_url})\n"
code << " return if save_and_redirect(@#{record_name}, url: params[:redirect] || (#{after_save_url})"
notification_message = ":record_x_created.tn(record: #{model.name}.model_name.human, name: @#{record_name}.#{options[:identifier]}, column: #{model.name}.human_attribute_name(#{options[:identifier].inspect}))"
code << if notify
", notify: #{notification_message}"
else
", notify: (params[:redirect] ? #{notification_message} : false)"
end
code << ")\n"
code << " #{t3e_code}\n" if creation_t3e
code << " #{render_form}\n"
code << "end\n"
Expand All @@ -173,7 +186,14 @@ def manage_restfully(defaults = {})
code << find_and_check_code
code << " #{t3e_code}\n"
code << " @#{record_name}.attributes = permitted_params\n"
code << " return if save_and_redirect(@#{record_name}#{', url: (' + after_save_url + ')' if after_save_url})\n"
code << " return if save_and_redirect(@#{record_name}, url: params[:redirect] || (#{after_save_url})"
notification_message = ":record_x_updated.tn(record: #{model.name}.model_name.human, name: @#{record_name}.#{options[:identifier]}, column: #{model.name}.human_attribute_name(#{options[:identifier].inspect}))"
code << if notify
", notify: #{notification_message}"
else
", notify: (params[:redirect] ? #{notification_message} : false)"
end
code << ")\n"
code << " #{render_form}\n"
code << "end\n"
end
Expand Down
1 change: 0 additions & 1 deletion app/helpers/backend/helps_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,5 @@ def wikize(content, options = {})

content.html_safe
end

end
end
2 changes: 2 additions & 0 deletions config/locales/cmn/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,8 @@ cmn:
record_has_been_correctly_removed: "该记录已成功删除"
record_has_been_imported: "记录已导入"
record_not_found: "未发现记录"
record_x_created: "%{record}%{name}已建立"
record_x_updated: "%{record}%{name}已更新"
sale_is_not_duplicatable: "销售不duplicatable"
select_entry_items_to_mark_together: "选择条目的项目一起庆祝"
# sensor_has_been_fixed: "Sensor %{name} has been fixed"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/deu/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,8 @@ deu:
record_has_been_correctly_removed: "Die Aufzeichnung wurde erfolgreich entfernt"
record_has_been_imported: "Die Bilanz wurde importiert"
record_not_found: "Aufnahme nicht gefunden"
record_x_created: "%{record} %{name} erstellt"
record_x_updated: "%{record} %{name} aktualisiert"
sale_is_not_duplicatable: "Verkauf ist nicht duplizierbar"
select_entry_items_to_mark_together: "Eintrag auswählen Elemente zu kennzeichnen zusammen"
# sensor_has_been_fixed: "Sensor %{name} has been fixed"
Expand Down
4 changes: 3 additions & 1 deletion config/locales/eng/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,8 @@ eng:
record_has_been_correctly_removed: "The record has been removed successfully"
record_has_been_imported: "Record has been imported"
record_not_found: "Record not found" #?
record_x_created: "%{record} %{name} created"
record_x_updated: "%{record} %{name} updated"
sale_is_not_duplicatable: "Sale is not duplicatable"
select_entry_items_to_mark_together: "Select entry items to mark together"
sensor_has_been_fixed: "Sensor %{name} has been fixed"
Expand Down Expand Up @@ -2082,4 +2084,4 @@ eng:
backend/product_natures: "%{name}"
backend/products: "%{work_number} %{name} (%{number})"
backend/trackings: "%{serial} ‒ %{name} ‒ %{producer_full_name}"
backend/users: "%{first_name} %{last_name}"
backend/users: "%{first_name} %{last_name}"
2 changes: 2 additions & 0 deletions config/locales/fra/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,8 @@ fra:
record_has_been_imported: "L’enregistrement a été importé"
record_has_not_been_removed: "L’enregistrement n’a pas être supprimé correctement" #?
record_not_found: "Enregistrement introuvable"
record_x_created: "%{record} %{name} créé"
record_x_updated: "%{record} %{name} mise à jour"
restoration_finished: "La restauration de la sauvegarde s’est terminée (en %{value} secondes)." #?
sale_already_invoiced: "La vente a déjà été facturée: vous ne pouvez pas définir de livraison" #?
sale_cannot_be_updated: "Le devis n’est plus modifiable." #?
Expand Down
2 changes: 2 additions & 0 deletions config/locales/ita/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,8 @@ ita:
record_has_been_correctly_removed: "Il record è stato rimosso con successo"
record_has_been_imported: "Record è stato importato"
record_not_found: "Inserimento non trovato"
record_x_created: "%{record} %{name} creato"
record_x_updated: "%{record} %{name} aggiornato"
sale_is_not_duplicatable: "Sale non è duplicabile"
select_entry_items_to_mark_together: "Selezionare gli elementi di ingresso per segnare insieme"
# sensor_has_been_fixed: "Sensor %{name} has been fixed"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/por/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,8 @@ por:
record_has_been_correctly_removed: "O registro foi removido com sucesso"
record_has_been_imported: "Registro foi importada"
record_not_found: "Registro não encontrado"
record_x_created: "%{record} %{name} criado"
record_x_updated: "%{record} %{name} atualizado"
sale_is_not_duplicatable: "Venda não é duplicatable"
select_entry_items_to_mark_together: "Selecione os itens de entrada para marcar juntos"
# sensor_has_been_fixed: "Sensor %{name} has been fixed"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/spa/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,8 @@ spa:
record_has_been_imported: "El registro ha sido importado"
record_has_not_been_removed: "El registro no puede ser suprimido" #?
record_not_found: "Registro no encontrado"
record_x_created: "%{record} %{name} creado"
record_x_updated: "%{record} %{name} actualizado"
restoration_finished: "La restauración de la salvaguardia se tremino (en %{name} secondos). Su codigo sociedad es %{code}" #?
sale_already_invoiced: "La vente a déjà été facturée: vous ne pouvez pas définir de livraison" #?
sale_cannot_be_updated: "El presupuesto ya no esta modificable." #?
Expand Down
4 changes: 2 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 9.5.4
-- Dumped by pg_dump version 9.5.4
-- Dumped from database version 9.5.5
-- Dumped by pg_dump version 9.5.5

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down

0 comments on commit 5d0a361

Please sign in to comment.