Class: TableSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/table_settings.rb,
lib/table_settings/detail_table.rb,
lib/table_settings/table_action.rb,
lib/table_settings/table_button.rb,
lib/table_settings/table_column.rb

Defined Under Namespace

Classes: Action, Button, Buttons, Column, CustomColumn, DetailTable, Row, StandardColumn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ TableSettings

Inicializace



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/table_settings.rb', line 16

def initialize(model)
  @settings = {:columns => [], :row => {}, :default => {}, :csv => {}}
  @model = model
  @default_table = table_name_from_model(model)

  @column_index = 0
  @columns = []
  @actions = []
  @errors = {}

  add_defaults
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#table_settingsObject

Returns the value of attribute table_settings.



3
4
5
# File 'lib/table_settings/table_column.rb', line 3

def table_settings
  @table_settings
end

Instance Method Details

#add_action(name, label) {|action| ... } ⇒ TableSettings::Action

Prida akci/tlacitko/link do tabulky

Yields:

  • (action)


116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/table_settings.rb', line 116

def add_action(name, label)
  action = ::TableSettings::Action.new(self)

  action.name = name
  action.label(label)

  yield(action) if block_given?

  @actions << action

  action
end

#add_column(name, label = nil, table = @default_table) {|column| ... } ⇒ TableSettings::Column

Prida standardni sloupec do tabulky

Yields:

  • (column)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/table_settings.rb', line 56

def add_column(name,
    label = nil,
    table = @default_table)

  column = ::TableSettings::StandardColumn.new(self, @column_index)

  label = default_label(name) if label.nil?
  column.params(name, label, table)

  yield(column) if block_given?

  @columns << column
  @column_index += 1

  column
end

#add_custom_column(name, label, column_method, column_class = nil, column_params = nil) {|column| ... } ⇒ TableSettings::Column

Prida custom sloupec do tabulky

Yields:

  • (column)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/table_settings.rb', line 84

def add_custom_column(name,
    label,
    column_method,
    column_class = nil,
    column_params = nil

)
  column = ::TableSettings::CustomColumn.new(self, @column_index)


  label = default_label(name) if label.nil?
  column.params(name,
                label,
                column_method,
                column_class,
                column_params
  )
  yield(column) if block_given?

  @columns << column
  @column_index += 1

  column
end

#add_defaultsObject



262
263
264
265
266
267
268
269
# File 'lib/table_settings.rb', line 262

def add_defaults
  form_id(@default_table+"_form_id")
  row_id()
  order_by()
  order_by_direction()
  page()

end

#checkboxes(enabled = true) ⇒ Object

Adds or removes checkboxes from table



153
154
155
156
# File 'lib/table_settings.rb', line 153

def checkboxes(enabled = true)
  @settings[:checkboxes] = enabled
  self
end

#construct_actionsObject



245
246
247
248
249
250
251
# File 'lib/table_settings.rb', line 245

def construct_actions
  actions = {}
  @actions.each do |action|
    actions[action.name] = action.action_hash
  end
  @settings[:row][:functions] = actions
end

#construct_columnsObject



239
240
241
242
243
# File 'lib/table_settings.rb', line 239

def construct_columns
  @columns.each do |column|
    @settings[:columns] << column.column_hash
  end
end

#content_id(string) ⇒ Object

ID tagu (napr. <div>), do ktereho se ma prekreslit cela tabulka (napr. pro tlacitko “Smazat filtr”)



221
222
223
224
# File 'lib/table_settings.rb', line 221

def content_id(string)
  @settings[:content_id] = string
  self
end

#csv_exclude_labels(exclude = true) ⇒ Object

CSV export - exclude header row with column labels



188
189
190
191
# File 'lib/table_settings.rb', line 188

def csv_exclude_labels(exclude = true)
  @settings[:csv][:exclude_labels] = exclude
  self
end

#csv_exclude_names(exclude = true) ⇒ Object

CSV export - exclude header row with column names



182
183
184
185
# File 'lib/table_settings.rb', line 182

def csv_exclude_names(exclude = true)
  @settings[:csv][:exclude_names] = exclude
  self
end

#csv_exclude_row_id(exclude = true) ⇒ Object

CSV export - exclude row_id column



194
195
196
197
# File 'lib/table_settings.rb', line 194

def csv_exclude_row_id(exclude = true)
  @settings[:csv][:exclude_row_id] = exclude
  self
end

#csv_name(name = 'export.csv') ⇒ Object

CSV export - filename



175
176
177
178
179
# File 'lib/table_settings.rb', line 175

def csv_name(name = 'export.csv')
  @settings[:csv][:name] = name

  self
end

#default_label(name) ⇒ Object



271
272
273
# File 'lib/table_settings.rb', line 271

def default_label(name)
  @model.human_attribute_name(name)
end

#filter_method(name) ⇒ Object

Metoda, ze ktere se filtruje tabulka



206
207
208
209
# File 'lib/table_settings.rb', line 206

def filter_method(name)
  @settings[:filter_method] = name
  self
end

#filter_path(path) ⇒ Object



199
200
201
202
203
# File 'lib/table_settings.rb', line 199

def filter_path(path)
  @settings[:filter_path] = path

  self
end

#form_id(id = "unique_form_id") ⇒ Object



130
131
132
133
134
# File 'lib/table_settings.rb', line 130

def form_id(id = "unique_form_id")
  @settings[:form_id] = id

  self
end

#has_columns?Boolean



315
316
317
# File 'lib/table_settings.rb', line 315

def has_columns?
  !@columns.empty?
end

#has_defaults?Boolean



294
295
296
297
298
299
# File 'lib/table_settings.rb', line 294

def has_defaults?
  default = @settings[:default]
  default.has_key? :order_by
  default.has_key? :order_by_direction
  default.has_key? :page
end

#has_filter_path?Boolean



280
281
282
# File 'lib/table_settings.rb', line 280

def has_filter_path?
  @settings.has_key? :filter_path
end

#has_form_id?Boolean



285
286
287
# File 'lib/table_settings.rb', line 285

def has_form_id?
  @settings.has_key? :form_id
end

#has_includes?Boolean



276
277
278
# File 'lib/table_settings.rb', line 276

def has_includes?
  @settings.has_key? :includes
end

#has_order_by?Boolean



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

def has_order_by?
  @settings[:default].has_key? :order_by
end

#has_order_by_direction?Boolean



306
307
308
# File 'lib/table_settings.rb', line 306

def has_order_by_direction?
  @settings[:default].has_key? :order_by_direction
end

#has_page?Boolean



310
311
312
# File 'lib/table_settings.rb', line 310

def has_page?
  @settings[:default].has_key? :page
end

#has_row_id?Boolean



290
291
292
# File 'lib/table_settings.rb', line 290

def has_row_id?
  @settings[:row].has_key? :id
end

#hashHash

Vysledny hash pro renderovani tabulky



33
34
35
36
37
38
39
40
# File 'lib/table_settings.rb', line 33

def hash
  construct_columns if @settings[:columns].empty?
  construct_actions if @settings[:row][:functions].blank?

  @settings[:row].delete(:functions) if @settings[:row][:functions].empty?

  @settings
end

#includes(options) ⇒ Object



233
234
235
236
237
# File 'lib/table_settings.rb', line 233

def includes(options)
  @settings[:includes] = options

  self
end

#on_method(name) ⇒ Object

Metoda, ze ktere se vykresluje tabulka (default: index)



212
213
214
215
# File 'lib/table_settings.rb', line 212

def on_method(name)
  @settings[:show_table_method] = name
  self
end

#order_by(row_name = "id", table_name = @default_table, use_table_name = true) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/table_settings.rb', line 142

def order_by(row_name = "id", table_name = @default_table, use_table_name = true)
  if use_table_name && !table_name.nil?
    @settings[:default][:order_by] = table_name.to_s+"."+row_name.to_s
  else
    @settings[:default][:order_by] = row_name.to_s
    order_by_direction("")
  end
  self
end

#order_by_direction(direction = "asc") ⇒ Object



158
159
160
161
162
# File 'lib/table_settings.rb', line 158

def order_by_direction(direction = "asc")
  @settings[:default][:order_by_direction] = direction

  self
end

#page(number = 1) ⇒ Object



164
165
166
167
168
# File 'lib/table_settings.rb', line 164

def page(number = 1)
  @settings[:default][:page] = number

  self
end

#per_page(number = 10) ⇒ Object



170
171
172
# File 'lib/table_settings.rb', line 170

def per_page(number = 10)
  @settings[:default][:per_page] = number
end

#refresh_settingsObject



42
43
44
45
# File 'lib/table_settings.rb', line 42

def refresh_settings
  @settings[:columns] = []
  construct_settings
end

#row_id(row_name = "id", table_name = @default_table) ⇒ Object



136
137
138
139
140
# File 'lib/table_settings.rb', line 136

def row_id(row_name = "id", table_name = @default_table)
  @settings[:row][:id] = table_name.to_s+"."+row_name.to_s

  self
end

#set_error(type) ⇒ Object



323
324
325
# File 'lib/table_settings.rb', line 323

def set_error(type)
  @errors[type.to_sym] = [type.to_s + " " + I18n.t("errors.messages.blank")]
end

#settings_ok?Boolean



319
320
321
# File 'lib/table_settings.rb', line 319

def settings_ok?
  has_filter_path? && has_form_id? && has_row_id? && has_defaults? && has_columns?
end

#standard_query_processing(aBool = true) ⇒ Object

Rendering template je udelano s RIGHT OUTER JOIN (nevim proc)

Pokud to chci v renderovani template jako normalne (kdyz zadnou nezadam), musim nastavit toto


229
230
231
# File 'lib/table_settings.rb', line 229

def standard_query_processing(aBool = true)
  @settings[:standard_query_processing] = aBool
end

#table_name_from_model(model) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/table_settings.rb', line 254

def table_name_from_model(model)
  if model.kind_of?(ActiveRecord::Relation)
    model.klass.table_name
  else
    model.table_name
  end
end

#valid?Boolean



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/table_settings.rb', line 327

def valid?
  filled = nil
  filled = set_error(:filter_path) unless has_filter_path?
  filled = set_error(:form_id) unless has_form_id?
  filled = set_error(:row_id) unless has_row_id?
  filled = set_error(:order_by) unless has_order_by?
  filled = set_error(:order_by_direction) unless has_order_by_direction?
  filled = set_error(:page) unless has_page?
  filled = set_error(:columns) unless has_columns?

  filled.nil?
end