lib/netzke/form_panel_api.rb in netzke-basepack-0.5.4 vs lib/netzke/form_panel_api.rb in netzke-basepack-0.5.5

- old
+ new

@@ -1,17 +1,28 @@ module Netzke module FormPanelApi # API handling form submission - def submit(params) - data_hsh = ActiveSupport::JSON.decode(params[:data]) - create_or_update_record(data_hsh) + def netzke_submit(params) + success = create_or_update_record(params) + + if success + {:set_form_values => array_of_values, :set_result => "ok"} + else + # flash eventual errors + @record.errors.each_full do |msg| + flash :error => msg + end + {:feedback => @flash} + end end # Creates/updates a record from hash - def create_or_update_record(hsh) + def create_or_update_record(params) + hsh = ActiveSupport::JSON.decode(params[:data]) + hsh.merge!(config[:strong_default_attrs]) if config[:strong_default_attrs] klass = config[:data_class_name].constantize - @record = klass.find_by_id(hsh.delete("id")) + @record ||= klass.find_by_id(hsh.delete("id")) # only pick up the record specified in the params if it was not provided in the configuration success = true @record = klass.new if @record.nil? hsh.each_pair do |k,v| @@ -22,20 +33,13 @@ flash :error => exc.message success = false break end end - - if success && @record.save - {:set_form_values => array_of_values} - else - # flash eventual errors - @record.errors.each_full do |msg| - flash :error => msg - end - {:feedback => @flash} - end + + # did we have complete success? + success && @record.save end # API handling form load # def load(params) # klass = config[:data_class_name].constantize @@ -45,10 +49,10 @@ # else @record = klass.find(params[:id]) # end # {:data => [array_of_values]} # end - def load(params) + def netzke_load(params) @record = data_class && data_class.find_by_id(params[:id]) {:set_form_values => array_of_values} end # API that returns options for a combobox \ No newline at end of file