Module: Netzke::FormPanelApi

Included in:
FormPanel
Defined in:
lib/netzke/form_panel_api.rb

Instance Method Summary collapse

Instance Method Details

#array_of_valuesObject

Returns array of form values according to the configured columns



69
70
71
# File 'lib/netzke/form_panel_api.rb', line 69

def array_of_values
  @record && @record.to_array(columns, self)
end

#configuration_panel__fields__get_combobox_options(params) ⇒ Object



63
64
65
66
# File 'lib/netzke/form_panel_api.rb', line 63

def configuration_panel__fields__get_combobox_options(params)
  query = params[:query]
  {:data => (predefined_columns.map{ |c| c[:name].to_s }).grep(/^#{query}/).map{ |n| [n] }}.to_nifty_json
end

#create_or_update_record(hsh) ⇒ Object

Creates/updates a record from hash



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/netzke/form_panel_api.rb', line 10

def create_or_update_record(hsh)
  klass = config[:data_class_name].constantize
  @record = klass.find_by_id(hsh.delete("id"))
  success = true

  @record = klass.new if @record.nil?

  hsh.each_pair do |k,v|
    begin
      @record.send("#{k}=",v)
    rescue StandardError => exc
      logger.debug "!!! FormPanelApi#create_or_update_record exception: #{exc.inspect}\n"
      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
end

#get_combobox_options(params) ⇒ Object

API that returns options for a combobox



56
57
58
59
60
61
# File 'lib/netzke/form_panel_api.rb', line 56

def get_combobox_options(params)
  column = params[:column]
  query = params[:query]
  
  {:data => config[:data_class_name].constantize.options_for(column, query).map{|s| [s]}}
end

#load(params) ⇒ Object

API handling form load def load(params)

klass = config[:data_class_name].constantize
case params[:neighbour]
  when "previous" then @record = klass.previous(params[:id])
  when "next"     then @record = klass.next(params[:id])
  else                 @record = klass.find(params[:id])
end
{:data => [array_of_values]}

end



50
51
52
53
# File 'lib/netzke/form_panel_api.rb', line 50

def load(params)
  @record = data_class && data_class.find_by_id(params[:id])
  {:set_form_values => array_of_values}
end

#submit(params) ⇒ Object

API handling form submission



4
5
6
7
# File 'lib/netzke/form_panel_api.rb', line 4

def submit(params)
  data_hsh = ActiveSupport::JSON.decode(params[:data])
  create_or_update_record(data_hsh)
end