module ActiveGrid class Grid attr_reader :columns, :name, :options, :partial, :scope, :view attr_accessor :block def initialize(view, name, scope, partial, options = {}) @columns = [] @view, @name, @scope, @partial, @options = view, name, scope, partial, options self.view._activegrid = self self.view.render(partial) end def header mode :header do view.content_tag(:tr, view.capture(self, nil, &block)) end end def filters mode :filter do view.content_tag(:tr, view.capture(self, nil, &block)) end end def body mode :body do models.map do |model| view.content_tag(:tr, view.capture(self, model, &block)) end.reduce(:<<) end end def column(name, options = {}, &block) column = Column.new(view, name, options, &block) case mode when :header columns << column view.content_tag(:th, column.header, column.tag_options) when :filter view.content_tag(:th, column.filter) when :body view.content_tag(:td, column.value) end end def mode(value = nil, &block) if value begin old_mode, @mode = @mode, value yield ensure @mode = old_mode end else @mode end end def models if paginate = options[:paginate] scope.limit(paginate) else scope end end def tag_options tag_options = {} tag_options["id"] = "#{name}_grid" tag_options["class"] = "activegrid" tag_options["data-activegrid"] = true tag_options["data-activegrid-name"] = name if options[:paginate] tag_options["data-activegrid-pages"] = (scope.count / options[:paginate].to_f).ceil tag_options["data-activegrid-paginate"] = options[:paginate] end tag_options end def to_html view.content_tag(:table, tag_options) do view.content_tag(:thead, header << filters) << view.content_tag(:tbody, body) end end end end