Class: DataGrid::DataGridLogic

Inherits:
Object
  • Object
show all
Defined in:
lib/data_grid/data_grid_logic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ DataGridLogic

Initialize



8
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/data_grid/data_grid_logic.rb', line 8

def initialize(attrs = {})
  # Internal copy od request params
  self.params = attrs[:params].except(:action).except(:controller).except(:utf8)

  # Display rows per page
  self.per_page = (attrs[:per_page] || DataGrid.per_page).to_i

  # Current page
  self.page = (attrs[:page] || 1).to_i 
  self.page = 1 if self.page <= 0
  self.page = 1 if self.page >= 10000000

  # DataGrid name. Has to be set if two or more grids on page.
  self.name = attrs[:name] || ''

  # Sort by this field
  self.sort = attrs[:sort]

  # Sort direction - (asc, desc)
  self.sort_direction = attrs[:sort] || DataGrid.sort_direction

  # Internal array of columns
  self.columns = attrs[:columns] || []

  # Internal representation of prepared data to display
  self.out_data = []

  # Name of state saver method - (cookies)
  self.state_saver = attrs[:state_saver]

  # Show grid footer?
  self.show_footer = attrs[:show_footer] || DataGrid.show_footer

  # Internal representation of hidden rows - row below each row
  self.hidden_row = attrs[:hidden_row]

  # Internal representation of prepared hidden rows
  self.out_hidden_rows = []

  # Initial data grid sorting (ie. 'age ASC')
  self.initial_sort = attrs[:initial_sort]

  # SQL which counts rows 
  self.count_statement = attrs[:count_statement] || nil

  # Extra where statement
  self.extra_orm_options = attrs[:extra_orm_options] || nil

  # Name of exported filename
  self.export_filename = attrs[:export_filename] || DataGrid.export_filename

  # Name of export method - (csv)
  self.export_enabled = attrs[:export_enabled]
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def columns
  @columns
end

#count_statementObject

Returns the value of attribute count_statement.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def count_statement
  @count_statement
end

#data_classObject

Returns the value of attribute data_class.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def data_class
  @data_class
end

#export_enabledObject

Returns the value of attribute export_enabled.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def export_enabled
  @export_enabled
end

#export_filenameObject

Returns the value of attribute export_filename.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def export_filename
  @export_filename
end

#extra_orm_optionsObject

Returns the value of attribute extra_orm_options.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def extra_orm_options
  @extra_orm_options
end

#global_summariesObject

Returns the value of attribute global_summaries.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def global_summaries
  @global_summaries
end

#hidden_rowObject

Returns the value of attribute hidden_row.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def hidden_row
  @hidden_row
end

#in_dataObject

Returns the value of attribute in_data.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def in_data
  @in_data
end

#initial_sortObject

Returns the value of attribute initial_sort.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def initial_sort
  @initial_sort
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def name
  @name
end

#out_dataObject

Returns the value of attribute out_data.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def out_data
  @out_data
end

#out_hidden_rowsObject

Returns the value of attribute out_hidden_rows.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def out_hidden_rows
  @out_hidden_rows
end

#pageObject

Returns the value of attribute page.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def page
  @page
end

#pagesObject

Returns the value of attribute pages.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def pages
  @pages
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def params
  @params
end

#per_pageObject

Returns the value of attribute per_page.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def per_page
  @per_page
end

#row_stylesObject

Returns the value of attribute row_styles.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def row_styles
  @row_styles
end

Returns the value of attribute show_footer.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def show_footer
  @show_footer
end

#sortObject

Returns the value of attribute sort.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def sort
  @sort
end

#sort_directionObject

Returns the value of attribute sort_direction.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def sort_direction
  @sort_direction
end

#state_saverObject

Returns the value of attribute state_saver.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def state_saver
  @state_saver
end

#summariesObject

Returns the value of attribute summaries.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def summaries
  @summaries
end

#totalObject

Returns the value of attribute total.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def total
  @total
end

#view_contextObject

Returns the value of attribute view_context.



4
5
6
# File 'lib/data_grid/data_grid_logic.rb', line 4

def view_context
  @view_context
end

Instance Method Details

#add_column(column_field, column_attrs = {}) ⇒ Object

Add new column



64
65
66
# File 'lib/data_grid/data_grid_logic.rb', line 64

def add_column(column_field, column_attrs = {})
  self.columns << Column.new(column_field, column_attrs)
end

#data(_data) ⇒ Object Also known as: data=

Assign data



69
70
71
72
73
74
# File 'lib/data_grid/data_grid_logic.rb', line 69

def data(_data)
  self.in_data = _data
  self.data_class = _data
  self.data_class = _data.class
  self.data_class = _data if _data.class != Array
end

#entries_fromObject

Used in view to show entries from



118
119
120
121
# File 'lib/data_grid/data_grid_logic.rb', line 118

def entries_from
  ef = (self.page-1)*self.per_page
  ef.zero? ? 1 : ef
end

#entries_toObject

Used in view to show entries to



124
125
126
127
128
129
130
# File 'lib/data_grid/data_grid_logic.rb', line 124

def entries_to
  if (self.page)*self.per_page > total
    total
  else
    (self.page)*self.per_page
  end
end

#filters?Boolean

Any column has filters?

Returns:

  • (Boolean)


82
83
84
# File 'lib/data_grid/data_grid_logic.rb', line 82

def filters?
  !self.columns.select{|c| !c.filter.nil?}.empty?
end

#footer?Boolean

Display footer?

Returns:

  • (Boolean)


97
98
99
# File 'lib/data_grid/data_grid_logic.rb', line 97

def footer?
  self.show_footer
end

#get_params_from_requestObject

Get params from request



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/data_grid/data_grid_logic.rb', line 102

def get_params_from_request
  self.per_page = (params["per_page_#{self.name}"] || self.per_page).to_i
  self.page = (params["page_#{self.name}"] || self.page).to_i
  self.sort = params["sort_#{self.name}"] ? params["sort_#{self.name}"].split('_').pop.to_i : self.sort
  self.sort_direction = params["sort_direction_#{self.name}"] || self.sort_direction

  self.columns.each_with_index do |col, col_index|
    col.filter_value = params["filter_#{self.name}_#{col_index}"]
    if params["filter_#{self.name}_#{col_index}_from"] or params["filter_#{self.name}_#{col_index}_to"]
      col.filter_value = params["filter_#{self.name}_#{col_index}_from"].to_s + DataGrid.range_separator +
        params["filter_#{self.name}_#{col_index}_to"].to_s
    end
  end
end

#global_summary?Boolean

Any column has global summary?

Returns:

  • (Boolean)


92
93
94
# File 'lib/data_grid/data_grid_logic.rb', line 92

def global_summary?
  !self.columns.select{|c| !c.global_summary.nil?}.empty?
end

#prepare_dataObject

Prepare data, do sorting, filtering, paging



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/data_grid/data_grid_logic.rb', line 144

def prepare_data
  data_source = nil
  if self.data_class == Array
    require 'data_grid/data_source_array'
    data_source = DataGrid::DataSourceArray.new
  else
    require 'data_grid/data_source_orm'
    data_source = DataGrid::DataSourceORM.new
  end

  data_source.data_grid = self
  data_source.prepare_data
end

#row_stylerObject

Row styler is a column, which is hidden and describes style of the row



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/data_grid/data_grid_logic.rb', line 160

def row_styler
  styler_column = self.columns.select{|col| col.title == :row_styler}

  if styler_column and !styler_column.empty?
    styler_column_index = self.columns.index(styler_column.first)

    self.row_styles = []
    self.out_data.each do |row|
      self.row_styles << row[styler_column_index]
    end

    self.columns.delete_at(styler_column_index)
  end
end

#sorting?Boolean

Sorting comes from URL?

Returns:

  • (Boolean)


77
78
79
# File 'lib/data_grid/data_grid_logic.rb', line 77

def sorting?
  !self.sort.nil?
end

#summary?Boolean

Any column has summary?

Returns:

  • (Boolean)


87
88
89
# File 'lib/data_grid/data_grid_logic.rb', line 87

def summary?
  !self.columns.select{|c| !c.summary.nil?}.empty?
end