class TableSettings

Attributes

errors[R]
table_settings[RW]

Public Class Methods

new(model) click to toggle source

Inicializace

@param [Symbol] model

# 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

Public Instance Methods

add_action(name, label) { |action| ... } click to toggle source

Prida akci/tlacitko/link do tabulky

@params [Symbol] name - nazev akce (libovolny, mel by odpovidat akci) @params [String] label - nazev akce pro view

@return [TableSettings::Action]

# 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| ... } click to toggle source

Prida standardni sloupec do tabulky

@params [String] name - nazev sloupce v db @params [String] label - nazev sloupce pro zobrazeni @params [String] table - nazev db tabulky (nepovinne, default z konstruktoru)

@return [TableSettings::Column]

# 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| ... } click to toggle source

Prida custom sloupec do tabulky

@params [String] name - nazev sloupce v db @params [String] label - nazev sloupce pro view @params [String] column_method - metoda pro definováni sloupce @params [String] column_class - třída, ve které se volá column_method (nepovinne, defaultne vychozi model) @params [String] column_params - vlastni parametry pro column_method

@return [TableSettings::Column]

# 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_defaults() click to toggle source
# 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) click to toggle source

Adds or removes checkboxes from table

# File lib/table_settings.rb, line 153
def checkboxes(enabled = true)
  @settings[:checkboxes] = enabled
  self
end
construct_actions() click to toggle source
# 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_columns() click to toggle source
# File lib/table_settings.rb, line 239
def construct_columns
  @columns.each do |column|
    @settings[:columns] << column.column_hash
  end
end
content_id(string) click to toggle source

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

# File lib/table_settings.rb, line 221
def content_id(string)
  @settings[:content_id] = string
  self
end
csv_exclude_labels(exclude = true) click to toggle source

CSV export - exclude header row with column labels

# 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) click to toggle source

CSV export - exclude header row with column names

# 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) click to toggle source

CSV export - exclude row_id column

# 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') click to toggle source

CSV export - filename

# File lib/table_settings.rb, line 175
def csv_name(name = 'export.csv')
  @settings[:csv][:name] = name

  self
end
default_label(name) click to toggle source
# File lib/table_settings.rb, line 271
def default_label(name)
  @model.human_attribute_name(name)
end
filter_method(name) click to toggle source

Metoda, ze ktere se filtruje tabulka

# File lib/table_settings.rb, line 206
def filter_method(name)
  @settings[:filter_method] = name
  self
end
filter_path(path) click to toggle source
# File lib/table_settings.rb, line 199
def filter_path(path)
  @settings[:filter_path] = path

  self
end
form_id(id = "unique_form_id") click to toggle source
# File lib/table_settings.rb, line 130
def form_id(id = "unique_form_id")
  @settings[:form_id] = id

  self
end
has_columns?() click to toggle source
# File lib/table_settings.rb, line 315
def has_columns?
  !@columns.empty?
end
has_defaults?() click to toggle source
# 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?() click to toggle source
# File lib/table_settings.rb, line 280
def has_filter_path?
  @settings.has_key? :filter_path
end
has_form_id?() click to toggle source
# File lib/table_settings.rb, line 285
def has_form_id?
  @settings.has_key? :form_id
end
has_includes?() click to toggle source
# File lib/table_settings.rb, line 276
def has_includes?
  @settings.has_key? :includes
end
has_order_by?() click to toggle source
# File lib/table_settings.rb, line 301
def has_order_by?
  @settings[:default].has_key? :order_by
end
has_order_by_direction?() click to toggle source
# File lib/table_settings.rb, line 306
def has_order_by_direction?
  @settings[:default].has_key? :order_by_direction
end
has_page?() click to toggle source
# File lib/table_settings.rb, line 310
def has_page?
  @settings[:default].has_key? :page
end
has_row_id?() click to toggle source
# File lib/table_settings.rb, line 290
def has_row_id?
  @settings[:row].has_key? :id
end
hash() click to toggle source

Vysledny hash pro renderovani tabulky

@return [Hash]

# 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) click to toggle source
# File lib/table_settings.rb, line 233
def includes(options)
  @settings[:includes] = options

  self
end
on_method(name) click to toggle source

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

# 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) click to toggle source
# 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") click to toggle source
# File lib/table_settings.rb, line 158
def order_by_direction(direction = "asc")
  @settings[:default][:order_by_direction] = direction

  self
end
page(number = 1) click to toggle source
# File lib/table_settings.rb, line 164
def page(number = 1)
  @settings[:default][:page] = number

  self
end
per_page(number = 10) click to toggle source
# File lib/table_settings.rb, line 170
def per_page(number = 10)
  @settings[:default][:per_page] = number
end
refresh_settings() click to toggle source
# File lib/table_settings.rb, line 42
def refresh_settings
  @settings[:columns] = []
  construct_settings
end
row_id(row_name = "id", table_name = @default_table) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# 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) click to toggle source

Rendering template je udelano s RIGHT OUTER JOIN (nevim proc) Pokud to chci v renderovani template jako normalne (kdyz zadnou nezadam), musim nastavit toto

# File lib/table_settings.rb, line 229
def standard_query_processing(aBool = true)
  @settings[:standard_query_processing] = aBool
end
table_name_from_model(model) click to toggle source
# 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?() click to toggle source
# 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