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

Parameters:

  • model (Symbol)


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 => {}}
  @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)

Returns:



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)

Returns:



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)

Returns:



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



224
225
226
227
228
229
230
231
# File 'lib/table_settings.rb', line 224

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



149
150
151
152
# File 'lib/table_settings.rb', line 149

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

#construct_actionsObject



207
208
209
210
211
212
213
# File 'lib/table_settings.rb', line 207

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

#construct_columnsObject



201
202
203
204
205
# File 'lib/table_settings.rb', line 201

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”)



190
191
192
193
# File 'lib/table_settings.rb', line 190

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

#default_label(name) ⇒ Object



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

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

#filter_method(name) ⇒ Object

Metoda, ze ktere se filtruje tabulka



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

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

#filter_path(path) ⇒ Object



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

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

Returns:

  • (Boolean)


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

def has_columns?
  !@columns.empty?
end

#has_defaults?Boolean

Returns:

  • (Boolean)


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

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

Returns:

  • (Boolean)


242
243
244
# File 'lib/table_settings.rb', line 242

def has_filter_path?
  @settings.has_key? :filter_path
end

#has_form_id?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/table_settings.rb', line 247

def has_form_id?
  @settings.has_key? :form_id
end

#has_includes?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/table_settings.rb', line 238

def has_includes?
  @settings.has_key? :includes
end

#has_order_by?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/table_settings.rb', line 263

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

#has_order_by_direction?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/table_settings.rb', line 268

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

#has_page?Boolean

Returns:

  • (Boolean)


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

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

#has_row_id?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/table_settings.rb', line 252

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

#hashHash

Vysledny hash pro renderovani tabulky

Returns:

  • (Hash)


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



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

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

  self
end

#on_method(name) ⇒ Object

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



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

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

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



142
143
144
145
146
# File 'lib/table_settings.rb', line 142

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

  self
end

#order_by_direction(direction = "asc") ⇒ Object



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

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

  self
end

#page(number = 1) ⇒ Object



160
161
162
163
164
# File 'lib/table_settings.rb', line 160

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

  self
end

#per_page(number = 10) ⇒ Object



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

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



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

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

#settings_ok?Boolean

Returns:

  • (Boolean)


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

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

#table_name_from_model(model) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/table_settings.rb', line 216

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

#valid?Boolean

Returns:

  • (Boolean)


289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/table_settings.rb', line 289

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