require 'table_settings/table_column.rb' require 'table_settings/table_action.rb' require 'table_settings/detail_table.rb' require 'table_settings/table_button.rb' class TableSettings attr_reader :errors ## # Inicializace # # @param [Symbol] model # 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 ## # Vysledny hash pro renderovani tabulky # # @return [Hash] 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 def refresh_settings @settings[:columns] = [] construct_settings end ## # 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] # 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 ## # 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] # 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 ## # 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] 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 def form_id(id = "unique_form_id") @settings[:form_id] = id self end def row_id(row_name = "id", table_name = @default_table) @settings[:row][:id] = table_name.to_s+"."+row_name.to_s self end def order_by(row_name = "id", table_name = @default_table) @settings[:default][:order_by] = table_name.to_s+"."+row_name.to_s self end def order_by_direction(direction = "asc") @settings[:default][:order_by_direction] = direction self end def page(number = 1) @settings[:default][:page] = number self end def filter_path(path) @settings[:filter_path] = path self end # Metoda, ze ktere se filtruje tabulka def filter_method(name) @settings[:filter_method] = name self end # Metoda, ze ktere se vykresluje tabulka (default: index) def on_method(name) @settings[:show_table_method] = name self end ## # ID tagu (napr.