lib/wice/wice_grid_controller.rb
# +export_grid_if_requested+ is a controller method which should be called at the end of each action containing grids with enabled
# CSV export.
#
# CSV export will only work if each WiceGrid helper is placed in a partial of its own (requiring it from the master template
# of course for the usual flow).
# +export_grid_if_requested+ intercepts CSV export requests and evaluates the corresponding partial with the required  grid helper.
# By default for each grid +export_grid_if_requested+ will look for a partial
# whose name follows the following pattern:
#
#     _GRID_NAME_grid.html.erb
#
# For example, a grid named +orders+ is supposed to be found in a template called <tt>_orders_grid.html.erb</tt>,
# Remember that the default name of grids is +grid+.
#
# This convention can be easily overridden by supplying a hash parameter to +export_grid_if_requested+ where each key is the name of
# a grid, and the value is the name of the template (like it is specified for +render+, i.e. without '_' and extensions):
#
#   export_grid_if_requested('grid' => 'orders', 'grid2' => 'invoices')
#
# If the request is not a CSV export request, the method does nothing and returns +false+, if it is a CSV export request,
# the method returns +true+.
#
# If the action has no explicit +render+ call, it's OK to just place +export_grid_if_requested+ as the last line of the action. Otherwise,
# to avoid double rendering, use the return value of the method to conditionally call your +render+ :
#
#    export_grid_if_requested || render(action: 'index')
#
# It's also possible to supply a block which will be called if no CSV export is requested:
#
#    export_grid_if_requested do
#     render(action: 'index')
#    end
+export_grid_if_requested+ is a controller method which should be called at the end of each action containing grids with enabled
CSV export.

CSV export will only work if each WiceGrid helper is placed in a partial of its own (requiring it from the master template
of course for the usual flow).
+export_grid_if_requested+ intercepts CSV export requests and evaluates the corresponding partial with the required  grid helper.
By default for each grid +export_grid_if_requested+ will look for a partial
whose name follows the following pattern.



For example, a grid named +orders+ is supposed to be found in a template called _orders_grid.html.erb,
Remember that the default name of grids is +grid+.

This convention can be easily overridden by supplying a hash parameter to +export_grid_if_requested+ where each key is the name of
a grid, and the value is the name of the template (like it is specified for +render+, i.e. without '_' and extensions).



If the request is not a CSV export request, the method does nothing and returns +false+, if it is a CSV export request,
the method returns +true+.

If the action has no explicit +render+ call, it's OK to just place +export_grid_if_requested+ as the last line of the action. Otherwise,
to avoid double rendering, use the return value of the method to conditionally call your +render+ .



It's also possible to supply a block which will be called if no CSV export is requested.

export_grid_if_requested do
render(action: 'index')
end
+ export_grid_if_requested + is a controller method which should be called at the end of each action containing grids with enabled CSV export . CSV export will only work if each WiceGrid helper is placed in a partial of its own ( requiring it from the master template of course for the usual flow ) . + export_grid_if_requested + intercepts CSV export requests and evaluates the corresponding partial with the required grid helper . By default for each grid + export_grid_if_requested + will look for a partial whose name follows the following pattern . For example a grid named + orders + is supposed to be found in a template called _orders_grid . html . erb Remember that the default name of grids is + grid + . This convention can be easily overridden by supplying a hash parameter to + export_grid_if_requested + where each key is the name of a grid and the value is the name of the template ( like it is specified for + render + i . e . without ' _ ' and extensions ) . If the request is not a CSV export request the method does nothing and returns + false + if it is a CSV export request the method returns + true + . If the action has no explicit + render + call it ' s OK to just place + export_grid_if_requested + as the last line of the action . Otherwise to avoid double rendering use the return value of the method to conditionally call your + render + . It ' s also possible to supply a block which will be called if no CSV export is requested . export_grid_if_requested do render ( action : ' index ' ) end
def export_grid_if_requested(opts = {})
      grid = self.wice_grid_instances.detect(&:output_csv?)
      if grid
        template_name = opts[grid.name] || opts[grid.name.intern]
        template_name ||= grid.name + '_grid'
        temp_filename = render_to_string(partial: template_name)
        temp_filename = temp_filename.strip
        filename = (grid.csv_file_name || grid.name) + '.csv'
        grid.csv_tempfile.close
        send_file temp_filename, filename: filename, type: "text/csv; charset=#{get_output_encoding grid.csv_encoding}"
        grid.csv_tempfile = nil
        true
      else
        yield if block_given?
        false
      end
    end
def export_grid_if_requested ( opts = { } ) grid = self . wice_grid_instances . detect ( & :output_csv? ) if grid template_name = opts [ grid . name ] || opts [ grid . name . intern ] template_name ||= grid . name + '_grid' temp_filename = render_to_string ( partial : template_name ) temp_filename = temp_filename . strip filename = ( grid . csv_file_name || grid . name ) + '.csv' grid . csv_tempfile . close send_file temp_filename , filename : filename , type : "text/csv; charset=#{get_output_encoding grid.csv_encoding}" grid . csv_tempfile = nil true else yield if block_given? false end end
+export_grid_if_requested+ is a controller method which should be called at the end of each action containing grids with enabled
CSV export.
+ export_grid_if_requested + is a controller method which should be called at the end of each action containing grids with enabled CSV export .
opts