Module: Netzke::GridPanelApi

Included in:
GridPanel
Defined in:
lib/netzke/grid_panel_api.rb

Instance Method Summary collapse

Instance Method Details

#configuration_panel__columns__get_combobox_options(params) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/netzke/grid_panel_api.rb', line 107

def configuration_panel__columns__get_combobox_options(params)
  query = params[:query]
  
  data_arry = case params[:column]
              when "name"
                predefined_columns.map{ |c| c[:name].to_s }
              else
                raise RuntimeError, "Don't know about options for column '#{params[:column]}'"
              end
  
  {:data => data_arry.grep(/^#{query}/).map{ |n| [n] }}.to_nifty_json
end

#delete_data(params = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/netzke/grid_panel_api.rb', line 31

def delete_data(params = {})
  if !ext_config[:prohibit_delete]
    record_ids = ActiveSupport::JSON.decode(params[:records])
    klass = config[:data_class_name].constantize
    klass.delete(record_ids)
    {:feedback => "Deleted #{record_ids.size} record(s)", :load_store_data => get_data}
  else
    {:feedback => "You don't have permissions to delete data"}
  end
end

#get_combobox_options(params) ⇒ Object

Return the choices for the column



69
70
71
72
73
# File 'lib/netzke/grid_panel_api.rb', line 69

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

#get_data(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/netzke/grid_panel_api.rb', line 21

def get_data(params = {})
  if !ext_config[:prohibit_read]
    records = get_records(params)
    {:data => records.map{|r| r.to_array(normalized_columns, self)}, :total => ext_config[:enable_pagination] && records.total_entries}
  else
    flash :error => "You don't have permissions to read data"
    {:feedback => @flash}
  end
end

#get_search(params) ⇒ Object

Returns searchlogic’s search with all the conditions



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/netzke/grid_panel_api.rb', line 76

def get_search(params)
  @search ||= begin
    raise ArgumentError, "No data_class_name specified for widget '#{name}'" if !config[:data_class_name]

    # make params coming from Ext grid filters understandable by searchlogic
    search_params = normalize_params(params)

    # merge with conditions coming from the config
    search_params[:conditions].deep_merge!(config[:conditions] || {})

    # merge with extra conditions (in searchlogic format, come from the extended search form)
    search_params[:conditions].deep_merge!(
      normalize_extra_conditions(ActiveSupport::JSON.decode(params[:extra_conditions]))
    ) if params[:extra_conditions]

    search = config[:data_class_name].constantize.search(search_params)
  
    # applying scopes
    scopes.each do |s|
      if s.is_a?(Array)
        scope_name, *args = s
        search.send(scope_name, *args)
      else
        search.send(s)
      end
    end
  
    search
  end
end

#hide_column(params) ⇒ Object



49
50
51
52
53
54
# File 'lib/netzke/grid_panel_api.rb', line 49

def hide_column(params)
  raise "Called api_hide_column while not configured to do so" if ext_config[:enable_column_hide] == false
  column_at(params[:index].to_i)[:hidden] = params[:hidden].to_b
  save_columns!
  {}
end

#move_column(params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/netzke/grid_panel_api.rb', line 56

def move_column(params)
  raise "Called api_move_column while not configured to do so" if ext_config[:enable_column_move] == false
  column_to_move = columns.delete_at(params[:old_index].to_i)
  columns.insert(params[:new_index].to_i, column_to_move)
  save_columns!

  # reorder the columns on the client side (still not sure if it's not an overkill)
  # {:reorder_columns => columns.map(&:name)} # Well, I think it IS an overkill - commented out 
  # until proven to be necessary
  {}
end

#post_data(params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/netzke/grid_panel_api.rb', line 3

def post_data(params)
  success = true
  mod_records = {}
  [:create, :update].each do |operation|
    data = ActiveSupport::JSON.decode(params["#{operation}d_records"]) if params["#{operation}d_records"]
    if !data.nil? && !data.empty? # data may be nil for one of the operations
      mod_records[operation] = process_data(data, operation)
      mod_records[operation] = nil if mod_records[operation].empty?
    end
    break if !success
  end
  {
    :update_new_records => mod_records[:create], 
    :update_mod_records => mod_records[:update] || {},
    :feedback => @flash
  }
end

#resize_column(params) ⇒ Object



42
43
44
45
46
47
# File 'lib/netzke/grid_panel_api.rb', line 42

def resize_column(params)
  raise "Called api_resize_column while not configured to do so" if ext_config[:enable_column_resize] == false
  column_at(params[:index].to_i)[:width] = params[:size].to_i
  save_columns!
  {}
end