Class: Fancygrid::Grid

Inherits:
Node
  • Object
show all
Defined in:
lib/fancygrid/grid.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #name, #parent, #resource_class, #root, #table_name

Instance Method Summary collapse

Methods inherited from Node

#attributes, #column, #columns, #columns_for, #name_chain, #root?

Constructor Details

#initialize(name, options = {}) ⇒ Grid

Initializes the fancygrid



61
62
63
64
65
# File 'lib/fancygrid/grid.rb', line 61

def initialize(name, options = {})
  self.options = Fancygrid.default_options.merge(options)
  super(nil, name, self.options)
  self.apply_options(self.options)
end

Instance Attribute Details

#ajax_typeObject

The request method for the ajax callback e.g. GET or POST.



26
27
28
# File 'lib/fancygrid/grid.rb', line 26

def ajax_type
  @ajax_type
end

#ajax_urlObject

Url for the ajax callback.



23
24
25
# File 'lib/fancygrid/grid.rb', line 23

def ajax_url
  @ajax_url
end

#componentsObject

Array of fancygrid component names that are going to be rendered.



29
30
31
# File 'lib/fancygrid/grid.rb', line 29

def components
  @components
end

#hide_searchObject

If true then hides the search bar, but keeps it enabled.



35
36
37
# File 'lib/fancygrid/grid.rb', line 35

def hide_search
  @hide_search
end

#leafsObject

Collection of all defined columns.



6
7
8
# File 'lib/fancygrid/grid.rb', line 6

def leafs
  @leafs
end

#optionsObject

Options that have been passed to initialize the fancygrid.



57
58
59
# File 'lib/fancygrid/grid.rb', line 57

def options
  @options
end

#ormObject

The class of the database connector implementation to be used.



12
13
14
# File 'lib/fancygrid/grid.rb', line 12

def orm
  @orm
end

#page_countObject

If pagination is enabled this value holds the total number of available data pages.



54
55
56
# File 'lib/fancygrid/grid.rb', line 54

def page_count
  @page_count
end

#paginateObject

Specifies whether pagination is enabled or not.



44
45
46
# File 'lib/fancygrid/grid.rb', line 44

def paginate
  @paginate
end

#per_page_valueObject

The default value for the per page drop down.



50
51
52
# File 'lib/fancygrid/grid.rb', line 50

def per_page_value
  @per_page_value
end

#per_page_valuesObject

Array of select options for per page drop down.



47
48
49
# File 'lib/fancygrid/grid.rb', line 47

def per_page_values
  @per_page_values
end

#record_countObject

Number of matching records. This is displayed in the pagination.



20
21
22
# File 'lib/fancygrid/grid.rb', line 20

def record_count
  @record_count
end

#recordsObject

The result of the database query. This is the data that is going to be rendered.



17
18
19
# File 'lib/fancygrid/grid.rb', line 17

def records
  @records
end

#search_operatorObject

The default search operator.



41
42
43
# File 'lib/fancygrid/grid.rb', line 41

def search_operator
  @search_operator
end

#search_operatorsObject

Array of enabled search operators.



38
39
40
# File 'lib/fancygrid/grid.rb', line 38

def search_operators
  @search_operators
end

#selectObject

Specifies whether sql select optimization should be performed



32
33
34
# File 'lib/fancygrid/grid.rb', line 32

def select
  @select
end

#view_stateObject

The grids view state.



9
10
11
# File 'lib/fancygrid/grid.rb', line 9

def view_state
  @view_state
end

Instance Method Details

#apply_options(options) ⇒ Object

Applies the given options and sets default values



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fancygrid/grid.rb', line 69

def apply_options(options)
  self.leafs            = []
  self.view_state       = Fancygrid::ViewState.new(options.fetch(:state_hash, {}))

  self.records          = []
  self.record_count     = 0
  self.orm              = options[:orm].classify.constantize
                 
  self.ajax_url         = nil
  self.ajax_type        = options[:ajax_type] 
  self.components       = options[:components]
  
  self.hide_search      = options[:hide_search]
  self.search_operators = options[:search_operators]
  self.search_operator  = options[:search_operator]
  
  self.per_page_values  = options[:per_page_values]
  self.per_page_value   = self.view_state.pagination_per_page(options[:per_page_value])
  self.paginate         = true
  self.select           = true
  self.page_count       = 0
end

#bottom_control?Boolean

Determines whether the bottom control bar component is enabled.

Returns:

  • (Boolean)


144
145
146
# File 'lib/fancygrid/grid.rb', line 144

def bottom_control?
  dynamic? && component?(:bottom_bar)
end

#collect_columnsObject

Reorganizes the defined columns and post fixes some options



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/fancygrid/grid.rb', line 225

def collect_columns
  leafs.clear
  super(leafs)
  leafs.each do |leaf|
    leaf.position = self.view_state.column_option(leaf, :position, leaf.position).to_i
    leaf.width    = self.view_state.column_option(leaf, :width,    leaf.width)
    leaf.visible  = self.view_state.column_option(leaf, :visible, leaf.visible).to_s == "true"
    
    leaf.search_operator = self.view_state.column_condition(leaf, :operator, leaf.search_operator)
    leaf.search_value    = self.view_state.column_condition(leaf, :value,    leaf.search_value)
  end
  leafs.sort! { |a, b| a.position <=> b.position }
end

#complex_search?Boolean

Determines whether the complex search component is enabled.

Returns:

  • (Boolean)


132
133
134
# File 'lib/fancygrid/grid.rb', line 132

def complex_search?
  dynamic? && component?(:search)
end

#component?(name) ⇒ Boolean

Determines whether the given component is enabled or not.

Returns:

  • (Boolean)


120
121
122
# File 'lib/fancygrid/grid.rb', line 120

def component?(name)
  self.components.include?(name)
end

#dump_recordsObject

Dumps the fetched records into an array of hashes that can be rendered as xml or cvs.



242
243
244
245
246
247
248
249
250
251
# File 'lib/fancygrid/grid.rb', line 242

def dump_records
  result = []
  self.records.each do |record|
    result << (dump = {})
    self.visible_columns.each do |col|
      dump[col.identifier] = col.fetch_value_and_format(record)
    end
  end
  return result
end

#dynamic?Boolean

Determines whether ajax callbacks are enabled or not

Returns:

  • (Boolean)


114
115
116
# File 'lib/fancygrid/grid.rb', line 114

def dynamic?
  self.ajax_url.present?
end

#find(&block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fancygrid/grid.rb', line 92

def find &block
  self.collect_columns

  query_options = {}
  query_options[:grid]       = self
  query_options[:conditions] = self.view_state.conditions
  query_options[:operator]   = self.view_state.operator
  
  if self.select
    query_options[:select]     = self.leafs.select { |leaf| leaf.selectable }.map{ |leaf| leaf.identifier }
  end

  if self.paginate && self.dynamic?
    query_options[:pagination] = self.view_state.pagination_options(1, self.per_page_value)
  end
  
  self.records, self.record_count = self.orm.new(query_options).execute(resource_class, &block)
  self.page_count = (self.record_count.to_f / self.per_page_value.to_f).ceil
end

#hidden_columnsObject

Collects and returns all columns that are marked to be hidden. The collection is sorted by the column position attribute.



168
169
170
171
172
173
174
# File 'lib/fancygrid/grid.rb', line 168

def hidden_columns
  @hidden_columns ||= leafs.select { |leaf| 
    leaf.hidden 
  }.sort { |a, b| 
    a.position <=> b.position 
  }
end

#js_optionsObject

Builds the javascript options for the javascript part of fancygrid.



212
213
214
215
216
217
218
219
220
221
# File 'lib/fancygrid/grid.rb', line 212

def js_options
  {
    :ajaxUrl => self.ajax_url,
    :ajaxType => self.ajax_type,
    :name => self.name,
    :page => self.view_state.pagination_page,
    :perPage => self.per_page_value,
    :searchVisible => !self.hide_search || !self.view_state.conditions.empty?
  }.to_json.gsub("<|>", "")
end

#operator_human_name(name) ⇒ Object

Gets the human readable name for given comparison operator.



196
197
198
199
200
201
# File 'lib/fancygrid/grid.rb', line 196

def operator_human_name(name)
  I18n.t(:"search.operator.#{name}", {
    :default => name.humanize, 
    :scope => Fancygrid.i18n_scope
  })
end

#search_filter(column, collection) ⇒ Object

Sets the search options for given column.



205
206
207
208
# File 'lib/fancygrid/grid.rb', line 205

def search_filter column, collection
  node = self.children.select { |leaf| leaf.respond_to?(:identifier) && leaf.identifier == column }.first
  node.search_options = Array(collection).map { |v| v.is_a?(Array) ? v : [v.to_s, v.to_s]}
end

#select_column_optionsObject

Generates options for dropdown selection to select a column.



178
179
180
181
182
183
184
# File 'lib/fancygrid/grid.rb', line 178

def select_column_options
  leafs.select { |leaf| 
    !leaf.hidden && leaf.searchable 
  }.map { |leaf| 
    [leaf.human_name, leaf.identifier] 
  }
end

#select_operator_optionsObject

Generates options for dropdown selection to select a comparison operator.



188
189
190
191
192
# File 'lib/fancygrid/grid.rb', line 188

def select_operator_options
  @select_operator_options ||= self.search_operators.map do |op|
    [ self.operator_human_name(op.to_s), op.to_s ] 
  end
end

#simple_search?Boolean

Determines whether the simple search component is enabled.

Returns:

  • (Boolean)


126
127
128
# File 'lib/fancygrid/grid.rb', line 126

def simple_search?
  dynamic? && component?(:search_bar)
end

#sort_window?Boolean

Determines whether the sort window component is enabled.

Returns:

  • (Boolean)


150
151
152
# File 'lib/fancygrid/grid.rb', line 150

def sort_window?
  dynamic? && component?(:sort_window)
end

#table_classObject



304
305
306
# File 'lib/fancygrid/grid.rb', line 304

def table_class
  @table_class
end

#table_class=(value) ⇒ Object



301
302
303
# File 'lib/fancygrid/grid.rb', line 301

def table_class=value
  @table_class = value
end

#table_idObject



311
312
313
# File 'lib/fancygrid/grid.rb', line 311

def table_id
  @table_id
end

#table_id=(value) ⇒ Object



308
309
310
# File 'lib/fancygrid/grid.rb', line 308

def table_id=value
  @table_id = value
end

#td_class(record = nil) ⇒ Object



280
281
282
283
284
285
286
287
# File 'lib/fancygrid/grid.rb', line 280

def td_class(record=nil)
  @td_class = Proc.new if block_given?
  if @td_class.is_a? Proc
    @td_class.call(record) if record
  else
    @td_class
  end
end

#td_class=(value) ⇒ Object



277
278
279
# File 'lib/fancygrid/grid.rb', line 277

def td_class=value
  @td_class = value
end

#td_id(record = nil) ⇒ Object



292
293
294
295
296
297
298
299
# File 'lib/fancygrid/grid.rb', line 292

def td_id(record=nil)
  @td_id = Proc.new if block_given?
  if @td_id.is_a? Proc
    @td_id.call(record) if record
  else
    @td_id
  end
end

#td_id=(value) ⇒ Object



289
290
291
# File 'lib/fancygrid/grid.rb', line 289

def td_id=value
  @td_id = value
end

#top_control?Boolean

Determines whether the top control bar component is enabled.

Returns:

  • (Boolean)


138
139
140
# File 'lib/fancygrid/grid.rb', line 138

def top_control?
  dynamic? && component?(:top_bar)
end

#tr_class(record = nil) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/fancygrid/grid.rb', line 256

def tr_class(record=nil)
  @tr_class = Proc.new if block_given?
  if @tr_class.is_a? Proc
    @tr_class.call(record) if record
  else
    @tr_class
  end
end

#tr_class=(value) ⇒ Object



253
254
255
# File 'lib/fancygrid/grid.rb', line 253

def tr_class=value
  @tr_class = value
end

#tr_id(record = nil) ⇒ Object



268
269
270
271
272
273
274
275
# File 'lib/fancygrid/grid.rb', line 268

def tr_id(record=nil)
  @tr_id = Proc.new if block_given?
  if @tr_id.is_a? Proc
    @tr_id.call(record) if record
  else
    @tr_id
  end
end

#tr_id=(value) ⇒ Object



265
266
267
# File 'lib/fancygrid/grid.rb', line 265

def tr_id=value
  @tr_id = value
end

#visible_columnsObject

Collects and returns all columns that are marked to be visible. The collection is sorted by the column position attribute.



157
158
159
160
161
162
163
# File 'lib/fancygrid/grid.rb', line 157

def visible_columns
  @visible_columns ||= leafs.select { |leaf| 
    leaf.visible 
  }.sort { |a, b| 
    a.position <=> b.position 
  }
end