def grid_html(grid, options, rendering, reuse_last_column_for_filter_buttons) #:nodoc:
      table_html_attrs, header_tr_html = options[:html], options[:header_tr_html]

      Wice::WgHash.add_or_append_class_value!(table_html_attrs, 'wice-grid', true)

      if Array === Defaults::DEFAULT_TABLE_CLASSES
        Defaults::DEFAULT_TABLE_CLASSES.each do |default_class|
          Wice::WgHash.add_or_append_class_value!(table_html_attrs, default_class, true)
        end
      end

      if options[:class]
        Wice::WgHash.add_or_append_class_value!(table_html_attrs, options[:class])
        options.delete(:class)
      end

      cycle_class = nil
      sorting_dependant_row_cycling = options[:sorting_dependant_row_cycling]

      grid.output_buffer = GridOutputBuffer.new

      # Ruby 1.9.x
      grid.output_buffer.force_encoding('UTF-8') if grid.output_buffer.respond_to?(:force_encoding)

      grid.output_buffer << %(<div class="wice-grid-container table-responsive" data-grid-name="#{grid.name}" id="#{grid.name}"><div id="#{grid.name}_title">)
      grid.output_buffer << content_tag(:h3, grid.saved_query.name) if grid.saved_query
      grid.output_buffer << "</div><table #{public_tag_options(table_html_attrs, true)}>"
      grid.output_buffer << "<caption>#{rendering.kaption}</caption>" if rendering.kaption
      grid.output_buffer << '<thead>'

      no_filters_at_all = (options[:show_filters] == :no || rendering.no_filter_needed?)

      if no_filters_at_all
        no_rightmost_column = no_filter_row = no_filters_at_all
      else
        no_rightmost_column = no_filter_row = (options[:show_filters] == :no || rendering.no_filter_needed_in_main_table?) ? true : false
      end

      no_rightmost_column = true if reuse_last_column_for_filter_buttons

      number_of_columns = rendering.number_of_columns(:in_html)
      number_of_columns -= 1 if no_rightmost_column

      number_of_columns_for_extra_rows = number_of_columns + 1

      pagination_panel_content_html = nil
      if options[:upper_pagination_panel]
        grid.output_buffer << rendering.pagination_panel(number_of_columns, options[:hide_csv_button]) do
          pagination_panel_content_html =
            pagination_panel_content(grid, options[:extra_request_parameters], options[:allow_showing_all_records], options[:pagination_theme])
          pagination_panel_content_html
        end
      end

      title_row_attrs = header_tr_html.clone
      Wice::WgHash.add_or_append_class_value!(title_row_attrs, 'wice-grid-title-row', true)

      grid.output_buffer << %(<tr #{public_tag_options(title_row_attrs, true)}>)

      filter_row_id = grid.name + '_filter_row'

      # first row of column labels with sorting links

      filter_shown = if options[:show_filters] == :when_filtered
        grid.filtering_on?
      elsif options[:show_filters] == :always
        true
      end

      rendering.each_column_aware_of_one_last_one(:in_html) do |column, last|
        column_name = column.name

        opts = column.html

        opts = opts ? opts.clone : {}

        Wice::WgHash.add_or_append_class_value!(opts, column.css_class)

        if column.attribute && column.ordering

          column.add_css_class('active-filter') if grid.filtered_by?(column)

          direction = 'asc'
          link_style = nil
          arrow_class = nil

          if grid.ordered_by?(column)
            column.add_css_class('sorted')
            Wice::WgHash.add_or_append_class_value!(opts, 'sorted')
            link_style = grid.order_direction

            case grid.order_direction
            when 'asc'
              direction = 'desc'
              arrow_class = 'down'
            when 'desc'
              direction = 'asc'
              arrow_class = 'up'
            end
          end

          col_link = link_to(
            (column_name +
              if arrow_class
                ' ' + content_tag(:i, '', class: "fa fa-arrow-#{arrow_class}")
              else
                ''
              end).html_safe,

            rendering.column_link(
              column,
              direction,
              params,
              options[:extra_request_parameters]
            ),
            class: link_style)

          grid.output_buffer << content_tag(:th, col_link, opts)

        else
          if reuse_last_column_for_filter_buttons && last
            grid.output_buffer << content_tag(:th,
                                              hide_show_icon(filter_row_id, grid, filter_shown, no_filter_row, options[:show_filters], rendering), opts
            )
          else
            grid.output_buffer << content_tag(:th, column_name, opts)
          end
        end
      end

      grid.output_buffer << content_tag(:th,
                                        hide_show_icon(filter_row_id, grid, filter_shown, no_filter_row, options[:show_filters], rendering)
      ) unless no_rightmost_column

      grid.output_buffer << '</tr>'
      # rendering first row end

      unless no_filters_at_all # there are filters, we don't know where, in the table or detached
        if no_filter_row # they are all detached
          rendering.each_column(:in_html) do |column|
            if column.filter_shown?
              filter_html_code = column.render_filter.html_safe
              grid.output_buffer.add_filter(column.detach_with_id, filter_html_code)
            end
          end

        else # some filters are present in the table

          filter_row_attrs = header_tr_html.clone
          Wice::WgHash.add_or_append_class_value!(filter_row_attrs, 'wg-filter-row', true)
          filter_row_attrs['id'] = filter_row_id

          grid.output_buffer << %(<tr #{public_tag_options(filter_row_attrs, true)} )
          grid.output_buffer << 'style="display:none"' unless filter_shown
          grid.output_buffer << '>'

          rendering.each_column_aware_of_one_last_one(:in_html) do |column, last|
            opts = column.html ? column.html.clone : {}
            Wice::WgHash.add_or_append_class_value!(opts, column.css_class)

            if column.filter_shown?

              filter_html_code = column.render_filter.html_safe
              if column.detach_with_id
                grid.output_buffer << content_tag(:th, '', opts)
                grid.output_buffer.add_filter(column.detach_with_id, filter_html_code)
              else
                grid.output_buffer << content_tag(:th, filter_html_code, opts)
              end
            else
              if reuse_last_column_for_filter_buttons && last
                grid.output_buffer << content_tag(:th,
                                                  reset_submit_buttons(options, grid, rendering),
                                                  Wice::WgHash.add_or_append_class_value!(opts, 'filter_icons')
                )
              else
                grid.output_buffer << content_tag(:th, '', opts)
              end
            end
          end
          unless no_rightmost_column
            grid.output_buffer << content_tag(:th, reset_submit_buttons(options, grid, rendering), class: 'filter_icons')
          end
          grid.output_buffer << '</tr>'
        end
      end

      grid.output_buffer << '</thead><tfoot>'
      grid.output_buffer << rendering.pagination_panel(number_of_columns, options[:hide_csv_button]) do
        if pagination_panel_content_html
          pagination_panel_content_html
        else
          pagination_panel_content_html =
            pagination_panel_content(grid, options[:extra_request_parameters], options[:allow_showing_all_records], options[:pagination_theme])
          pagination_panel_content_html
        end
      end

      grid.output_buffer << '</tfoot><tbody>'

      # rendering  rows
      cell_value_of_the_ordered_column = nil
      previous_cell_value_of_the_ordered_column = nil

      grid.each do |ar| # rows
        before_row_output = if rendering.before_row_handler
          call_block(rendering.before_row_handler, ar, number_of_columns_for_extra_rows)
        end

        after_row_output = if rendering.after_row_handler
          call_block(rendering.after_row_handler, ar, number_of_columns_for_extra_rows)
        end

        replace_row_output = if rendering.replace_row_handler
          call_block(rendering.replace_row_handler, ar, number_of_columns_for_extra_rows)
        end

        row_content = if replace_row_output
          no_rightmost_column = true
          replace_row_output
        else
          row_content, tmp_cell_value_of_the_ordered_column = get_row_content(rendering, ar, sorting_dependant_row_cycling)
          cell_value_of_the_ordered_column = tmp_cell_value_of_the_ordered_column if tmp_cell_value_of_the_ordered_column
          row_content
        end

        row_attributes = rendering.get_row_attributes(ar)

        if sorting_dependant_row_cycling
          cycle_class = cycle('odd', 'even', name: grid.name) if cell_value_of_the_ordered_column != previous_cell_value_of_the_ordered_column
          previous_cell_value_of_the_ordered_column = cell_value_of_the_ordered_column
        else
          cycle_class = cycle('odd', 'even', name: grid.name)
        end

        Wice::WgHash.add_or_append_class_value!(row_attributes, cycle_class)

        grid.output_buffer << before_row_output if before_row_output
        grid.output_buffer << "<tr #{public_tag_options(row_attributes)}>#{row_content}"
        grid.output_buffer << content_tag(:td, '') unless no_rightmost_column
        grid.output_buffer << '</tr>'
        grid.output_buffer << after_row_output if after_row_output
      end

      last_row_output = if rendering.last_row_handler
        call_block(rendering.last_row_handler, number_of_columns_for_extra_rows)
      end

      grid.output_buffer << last_row_output if last_row_output

      grid.output_buffer << '</tbody></table>'

      base_link_for_filter, base_link_for_show_all_records = rendering.base_link_for_filter(controller, options[:extra_request_parameters])

      link_for_export = rendering.link_for_export(controller, 'csv', options[:extra_request_parameters])

      parameter_name_for_query_loading = { grid.name => { q: '' } }.to_query
      parameter_name_for_focus = { grid.name => { foc: '' } }.to_query

      processor_initializer_arguments = [
        base_link_for_filter,
        base_link_for_show_all_records,
        link_for_export,
        parameter_name_for_query_loading,
        parameter_name_for_focus,
        Rails.env
      ]

      filter_declarations = if no_filters_at_all
        []
      else
        rendering.select_for(:in_html) do |vc|
          vc.attribute && vc.filter
        end.collect(&:yield_declaration)
      end

      wg_data = {
        'data-processor-initializer-arguments' => processor_initializer_arguments.to_json,
        'data-filter-declarations'             => filter_declarations.to_json,
        :class                                 => 'wg-data'
      }

      wg_data['data-foc'] = grid.status['foc'] if grid.status['foc']

      grid.output_buffer << content_tag(:div, '', wg_data)

      grid.output_buffer << '</div>'

      if Rails.env.development?
        grid.output_buffer << javascript_tag(%/ document.ready = function(){ \n/ +
          %$ if (typeof(WiceGridProcessor) == "undefined"){\n$ +
          %$   alert("wice_grid.js not loaded, WiceGrid cannot proceed!\\n" +\n$ +
          %(     "Make sure that you have loaded wice_grid.js.\\n" + ) +
          %(     "Add line //= require wice_grid.js " + ) +
          %$     "to app/assets/javascripts/application.js")\n$ +
          %( } ) +
          %$ } $)
      end

      grid.output_buffer
    end
def grid_html(grid, options, rendering, reuse_last_column_for_filter_buttons) #:nodoc:
      table_html_attrs, header_tr_html = options[:html], options[:header_tr_html]

      Wice::WgHash.add_or_append_class_value!(table_html_attrs, 'wice-grid', true)

      if Array === Defaults::DEFAULT_TABLE_CLASSES
        Defaults::DEFAULT_TABLE_CLASSES.each do |default_class|
          Wice::WgHash.add_or_append_class_value!(table_html_attrs, default_class, true)
        end
      end

      if options[:class]
        Wice::WgHash.add_or_append_class_value!(table_html_attrs, options[:class])
        options.delete(:class)
      end

      cycle_class = nil
      sorting_dependant_row_cycling = options[:sorting_dependant_row_cycling]

      grid.output_buffer = GridOutputBuffer.new

      # Ruby 1.9.x
      grid.output_buffer.force_encoding('UTF-8') if grid.output_buffer.respond_to?(:force_encoding)

      grid.output_buffer << %(<div class="wice-grid-container table-responsive" data-grid-name="#{grid.name}" id="#{grid.name}"><div id="#{grid.name}_title">)
      grid.output_buffer << content_tag(:h3, grid.saved_query.name) if grid.saved_query
      grid.output_buffer << "</div><table #{public_tag_options(table_html_attrs, true)}>"
      grid.output_buffer << "<caption>#{rendering.kaption}</caption>" if rendering.kaption
      grid.output_buffer << '<thead>'

      no_filters_at_all = (options[:show_filters] == :no || rendering.no_filter_needed?)

      if no_filters_at_all
        no_rightmost_column = no_filter_row = no_filters_at_all
      else
        no_rightmost_column = no_filter_row = (options[:show_filters] == :no || rendering.no_filter_needed_in_main_table?) ? true : false
      end

      no_rightmost_column = true if reuse_last_column_for_filter_buttons

      number_of_columns = rendering.number_of_columns(:in_html)
      number_of_columns -= 1 if no_rightmost_column

      number_of_columns_for_extra_rows = number_of_columns + 1

      pagination_panel_content_html = nil
      if options[:upper_pagination_panel]
        grid.output_buffer << rendering.pagination_panel(number_of_columns, options[:hide_csv_button]) do
          pagination_panel_content_html =
            pagination_panel_content(grid, options[:extra_request_parameters], options[:allow_showing_all_records], options[:pagination_theme])
          pagination_panel_content_html
        end
      end

      title_row_attrs = header_tr_html.clone
      Wice::WgHash.add_or_append_class_value!(title_row_attrs, 'wice-grid-title-row', true)

      grid.output_buffer << %(<tr #{public_tag_options(title_row_attrs, true)}>)

      filter_row_id = grid.name + '_filter_row'

      # first row of column labels with sorting links

      filter_shown = if options[:show_filters] == :when_filtered
        grid.filtering_on?
      elsif options[:show_filters] == :always
        true
      end

      rendering.each_column_aware_of_one_last_one(:in_html) do |column, last|
        column_name = column.name

        opts = column.html

        opts = opts ? opts.clone : {}

        Wice::WgHash.add_or_append_class_value!(opts, column.css_class)

        if column.attribute && column.ordering

          column.add_css_class('active-filter') if grid.filtered_by?(column)

          direction = 'asc'
          link_style = nil
          arrow_class = nil

          if grid.ordered_by?(column)
            column.add_css_class('sorted')
            Wice::WgHash.add_or_append_class_value!(opts, 'sorted')
            link_style = grid.order_direction

            case grid.order_direction
            when 'asc'
              direction = 'desc'
              arrow_class = 'down'
            when 'desc'
              direction = 'asc'
              arrow_class = 'up'
            end
          end

          col_link = link_to(
            (column_name +
              if arrow_class
                ' ' + content_tag(:i, '', class: "fa fa-arrow-#{arrow_class}")
              else
                ''
              end).html_safe,

            rendering.column_link(
              column,
              direction,
              params,
              options[:extra_request_parameters]
            ),
            class: link_style)

          grid.output_buffer << content_tag(:th, col_link, opts)

        else
          if reuse_last_column_for_filter_buttons && last
            grid.output_buffer << content_tag(:th,
                                              hide_show_icon(filter_row_id, grid, filter_shown, no_filter_row, options[:show_filters], rendering), opts
            )
          else
            grid.output_buffer << content_tag(:th, column_name, opts)
          end
        end
      end

      grid.output_buffer << content_tag(:th,
                                        hide_show_icon(filter_row_id, grid, filter_shown, no_filter_row, options[:show_filters], rendering)
      ) unless no_rightmost_column

      grid.output_buffer << '</tr>'
      # rendering first row end

      unless no_filters_at_all # there are filters, we don't know where, in the table or detached
        if no_filter_row # they are all detached
          rendering.each_column(:in_html) do |column|
            if column.filter_shown?
              filter_html_code = column.render_filter.html_safe
              grid.output_buffer.add_filter(column.detach_with_id, filter_html_code)
            end
          end

        else # some filters are present in the table

          filter_row_attrs = header_tr_html.clone
          Wice::WgHash.add_or_append_class_value!(filter_row_attrs, 'wg-filter-row', true)
          filter_row_attrs['id'] = filter_row_id

          grid.output_buffer << %(<tr #{public_tag_options(filter_row_attrs, true)} )
          grid.output_buffer << 'style="display:none"' unless filter_shown
          grid.output_buffer << '>'

          rendering.each_column_aware_of_one_last_one(:in_html) do |column, last|
            opts = column.html ? column.html.clone : {}
            Wice::WgHash.add_or_append_class_value!(opts, column.css_class)

            if column.filter_shown?

              filter_html_code = column.render_filter.html_safe
              if column.detach_with_id
                grid.output_buffer << content_tag(:th, '', opts)
                grid.output_buffer.add_filter(column.detach_with_id, filter_html_code)
              else
                grid.output_buffer << content_tag(:th, filter_html_code, opts)
              end
            else
              if reuse_last_column_for_filter_buttons && last
                grid.output_buffer << content_tag(:th,
                                                  reset_submit_buttons(options, grid, rendering),
                                                  Wice::WgHash.add_or_append_class_value!(opts, 'filter_icons')
                )
              else
                grid.output_buffer << content_tag(:th, '', opts)
              end
            end
          end
          unless no_rightmost_column
            grid.output_buffer << content_tag(:th, reset_submit_buttons(options, grid, rendering), class: 'filter_icons')
          end
          grid.output_buffer << '</tr>'
        end
      end

      grid.output_buffer << '</thead><tfoot>'
      grid.output_buffer << rendering.pagination_panel(number_of_columns, options[:hide_csv_button]) do
        if pagination_panel_content_html
          pagination_panel_content_html
        else
          pagination_panel_content_html =
            pagination_panel_content(grid, options[:extra_request_parameters], options[:allow_showing_all_records], options[:pagination_theme])
          pagination_panel_content_html
        end
      end

      grid.output_buffer << '</tfoot><tbody>'

      # rendering  rows
      cell_value_of_the_ordered_column = nil
      previous_cell_value_of_the_ordered_column = nil

      grid.each do |ar| # rows
        before_row_output = if rendering.before_row_handler
          call_block(rendering.before_row_handler, ar, number_of_columns_for_extra_rows)
        end

        after_row_output = if rendering.after_row_handler
          call_block(rendering.after_row_handler, ar, number_of_columns_for_extra_rows)
        end

        replace_row_output = if rendering.replace_row_handler
          call_block(rendering.replace_row_handler, ar, number_of_columns_for_extra_rows)
        end

        row_content = if replace_row_output
          no_rightmost_column = true
          replace_row_output
        else
          row_content, tmp_cell_value_of_the_ordered_column = get_row_content(rendering, ar, sorting_dependant_row_cycling)
          cell_value_of_the_ordered_column = tmp_cell_value_of_the_ordered_column if tmp_cell_value_of_the_ordered_column
          row_content
        end

        row_attributes = rendering.get_row_attributes(ar)

        if sorting_dependant_row_cycling
          cycle_class = cycle('odd', 'even', name: grid.name) if cell_value_of_the_ordered_column != previous_cell_value_of_the_ordered_column
          previous_cell_value_of_the_ordered_column = cell_value_of_the_ordered_column
        else
          cycle_class = cycle('odd', 'even', name: grid.name)
        end

        Wice::WgHash.add_or_append_class_value!(row_attributes, cycle_class)

        grid.output_buffer << before_row_output if before_row_output
        grid.output_buffer << "<tr #{public_tag_options(row_attributes)}>#{row_content}"
        grid.output_buffer << content_tag(:td, '') unless no_rightmost_column
        grid.output_buffer << '</tr>'
        grid.output_buffer << after_row_output if after_row_output
      end

      last_row_output = if rendering.last_row_handler
        call_block(rendering.last_row_handler, number_of_columns_for_extra_rows)
      end

      grid.output_buffer << last_row_output if last_row_output

      grid.output_buffer << '</tbody></table>'

      base_link_for_filter, base_link_for_show_all_records = rendering.base_link_for_filter(controller, options[:extra_request_parameters])

      link_for_export = rendering.link_for_export(controller, 'csv', options[:extra_request_parameters])

      parameter_name_for_query_loading = { grid.name => { q: '' } }.to_query
      parameter_name_for_focus = { grid.name => { foc: '' } }.to_query

      processor_initializer_arguments = [
        base_link_for_filter,
        base_link_for_show_all_records,
        link_for_export,
        parameter_name_for_query_loading,
        parameter_name_for_focus,
        Rails.env
      ]

      filter_declarations = if no_filters_at_all
        []
      else
        rendering.select_for(:in_html) do |vc|
          vc.attribute && vc.filter
        end.collect(&:yield_declaration)
      end

      wg_data = {
        'data-processor-initializer-arguments' => processor_initializer_arguments.to_json,
        'data-filter-declarations'             => filter_declarations.to_json,
        :class                                 => 'wg-data'
      }

      wg_data['data-foc'] = grid.status['foc'] if grid.status['foc']

      grid.output_buffer << content_tag(:div, '', wg_data)

      grid.output_buffer << '</div>'

      if Rails.env.development?
        grid.output_buffer << javascript_tag(%/ document.ready = function(){ \n/ +
          %$ if (typeof(WiceGridProcessor) == "undefined"){\n$ +
          %$   alert("wice_grid.js not loaded, WiceGrid cannot proceed!\\n" +\n$ +
          %(     "Make sure that you have loaded wice_grid.js.\\n" + ) +
          %(     "Add line //= require wice_grid.js " + ) +
          %$     "to app/assets/javascripts/application.js")\n$ +
          %( } ) +
          %$ } $)
      end

      grid.output_buffer
    end
the longest method? :(