lib/active_admin/views/components/table_for.rb in activeadmin-0.4.4 vs lib/active_admin/views/components/table_for.rb in activeadmin-0.5.0.pre

- old
+ new

@@ -15,15 +15,15 @@ build_table super(options) end def column(*args, &block) - options = default_options.merge(args.last.is_a?(::Hash) ? args.pop : {}) + options = default_options.merge(args.extract_options!) title = args[0] data = args[1] || args[0] - col = Column.new(title, data, options, &block) + col = Column.new(title, data, @resource_class, options, &block) @columns << col # Build our header item within @header_row do build_table_header(col) @@ -70,13 +70,13 @@ def build_sortable_header_for(title, sort_key) classes = Arbre::HTML::ClassList.new(["sortable"]) if current_sort[0] == sort_key classes << "sorted-#{current_sort[1]}" end - + header_class = title.downcase.underscore - + classes << header_class th :class => classes do link_to(title, params.merge(:order => "#{sort_key}_#{order_for_sort_key(sort_key)}").except(:page)) end @@ -129,21 +129,27 @@ class Column attr_accessor :title, :data def initialize(*args, &block) - @options = default_options.merge(args.last.is_a?(::Hash) ? args.pop : {}) + @options = args.extract_options! + @title = pretty_title args[0] @data = args[1] || args[0] @data = block if block + @resource_class = args[2] end def sortable? if @data.is_a?(Proc) [String, Symbol].include?(@options[:sortable].class) - else + elsif @options.has_key?(:sortable) @options[:sortable] + elsif @data.respond_to?(:to_sym) && @resource_class + !@resource_class.reflect_on_association(@data.to_sym) + else + true end end # # Returns the key to be used for sorting this column @@ -162,36 +168,30 @@ # # column('Username', :sortable => 'login'){ @user.pretty_name } # # => Sort key will be 'login' # def sort_key - if @options[:sortable] == true || @options[:sortable] == false + # If boolean or nil, use the default sort key. + if @options[:sortable] == true || @options[:sortable] == false || @options[:sortable].nil? @data.to_s else @options[:sortable].to_s end end private def pretty_title(raw) if raw.is_a?(Symbol) - if @options[:i18n] && @options[:i18n].respond_to?(:human_attribute_name) && human_name = @options[:i18n].human_attribute_name(raw) - raw = human_name + if @options[:i18n] && @options[:i18n].respond_to?(:human_attribute_name) + raw = @options[:i18n].human_attribute_name(raw, :default => raw.to_s.titleize) + else + raw.to_s.titleize end - - raw.to_s.titleize else raw end end - - def default_options - { - :sortable => true - } - end - end end end end