Class: TableSettings

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

Defined Under Namespace

Classes: Action, Column, CustomColumn, StandardColumn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ TableSettings

Inicializace

Parameters:

  • model (Symbol)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/table_settings.rb', line 13

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.



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

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:



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/table_settings.rb', line 111

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:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/table_settings.rb', line 51

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:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/table_settings.rb', line 79

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



190
191
192
193
194
195
196
197
# File 'lib/table_settings.rb', line 190

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

end

#construct_actionsObject



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

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

#construct_columnsObject



167
168
169
170
171
# File 'lib/table_settings.rb', line 167

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

#default_label(name) ⇒ Object



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

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

#filter_path(path) ⇒ Object



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

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

  self
end

#form_id(id = "unique_form_id") ⇒ Object



125
126
127
128
129
# File 'lib/table_settings.rb', line 125

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

  self
end

#has_columns?Boolean

Returns:

  • (Boolean)


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

def has_columns?
  !@columns.empty?
end

#has_defaults?Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
# File 'lib/table_settings.rb', line 222

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)


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

def has_filter_path?
  @settings.has_key? :filter_path
end

#has_form_id?Boolean

Returns:

  • (Boolean)


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

def has_form_id?
  @settings.has_key? :form_id
end

#has_includes?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/table_settings.rb', line 204

def has_includes?
  @settings.has_key? :includes
end

#has_order_by?Boolean

Returns:

  • (Boolean)


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

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

#has_order_by_direction?Boolean

Returns:

  • (Boolean)


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

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

#has_page?Boolean

Returns:

  • (Boolean)


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

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

#has_row_id?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/table_settings.rb', line 218

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

#hashHash

Vysledny hash pro renderovani tabulky

Returns:

  • (Hash)


30
31
32
33
34
35
# File 'lib/table_settings.rb', line 30

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

  @settings
end

#includes(options) ⇒ Object



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

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

  self
end

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



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

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



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

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

  self
end

#page(number = 1) ⇒ Object



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

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

  self
end

#refresh_settingsObject



37
38
39
40
# File 'lib/table_settings.rb', line 37

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

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



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

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



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

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

#settings_ok?Boolean

Returns:

  • (Boolean)


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

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

#table_name_from_model(model) ⇒ Object



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

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)


255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/table_settings.rb', line 255

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