Module: Zena::Use::Grid::ControllerMethods

Includes:
Common
Included in:
NodesController
Defined in:
lib/zena/use/grid.rb

Overview

Common

Instance Method Summary collapse

Methods included from Common

#get_table_from_json

Instance Method Details

#cell_editObject

Get cell text



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zena/use/grid.rb', line 27

def cell_edit
  # get table
  table = get_table_from_json(@node.prop[params[:attr]])
  # get row/cell
  table_data = table[1]

  if row = table_data[params[:row].to_i]
    if cell = row[params[:cell].to_i]
      render :text => cell
    else
      ' '
    end
  else
    ' '
  end
end

#cell_updateObject

Ajax table editor



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zena/use/grid.rb', line 45

def cell_update
  # FIXME: SECURITY: how to make sure we only access authorized keys for tables ?
  # get table
  table = get_table_from_json(@node.prop[params[:attr]])

  # get row/cell
  table_data = table[1]

  if row = table_data[params[:row].to_i]
    if cell = row[params[:cell].to_i]
      if cell != params[:value]
        row[params[:cell].to_i] = params[:value]
        @node.update_attributes(params[:attr] => table.to_json)
      end
    else
      @node.errors.add(params[:attr], 'Cell outside of table range.')
    end
  else
    @node.errors.add(params[:attr], 'Row outside of table range.')
  end

  respond_to do |format|
    format.html { render :inline => @node.errors.empty? ? "<%= zazen(params[:value], :no_p => true) %>" : error_messages_for(:node, :object => @node) }
  end
rescue JSON::ParserError
  render :inline => _('could not save value (bad attribute)')
end

#table_updateObject

Ajax table add row/column



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/zena/use/grid.rb', line 74

def table_update
  # get table
  @table = get_table_from_json(@node.prop[params[:attr]])
  # get row/cell
  table_data = @table[1]

  if params[:add] == 'row'
    table_data << table_data[0].map { ' ' }
  elsif params[:add] == 'column'
    table_data.each do |row|
      row << ' '
    end
  elsif params[:remove] == 'row' && table_data.size > 2
    table_data.pop
  elsif params[:remove] == 'column' && table_data[0].size > 1
    table_data.each do |row|
      row.pop
    end
  else
    # reorder ...
  end

  @node.update_attributes(params[:attr] => @table.to_json)
rescue JSON::ParserError
  render :inline => _('could not save value (bad attribute)')
end