From 71fd6f2a244566f97af7982cb170abdba4d12283 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Fri, 21 Dec 2018 01:12:43 +0100 Subject: [PATCH] Add CSV download for ordergroups --- .../admin/ordergroups_controller.rb | 6 ++- app/models/ordergroup.rb | 6 +++ app/views/admin/ordergroups/index.html.haml | 5 ++- lib/ordergroups_csv.rb | 41 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/ordergroups_csv.rb diff --git a/app/controllers/admin/ordergroups_controller.rb b/app/controllers/admin/ordergroups_controller.rb index 358dc0c6..7bac6b8e 100644 --- a/app/controllers/admin/ordergroups_controller.rb +++ b/app/controllers/admin/ordergroups_controller.rb @@ -1,10 +1,14 @@ # encoding: utf-8 class Admin::OrdergroupsController < Admin::BaseController inherit_resources - + def index @ordergroups = Ordergroup.undeleted.order('name ASC') + if request.format.csv? + send_data OrdergroupsCsv.new(@ordergroups).to_csv, filename: 'ordergroups.csv', type: 'text/csv' + end + # if somebody uses the search field: unless params[:query].blank? @ordergroups = @ordergroups.where('name LIKE ?', "%#{params[:query]}%") diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index d61df2d5..1f98f8cd 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -39,6 +39,12 @@ class Ordergroup < Group .group('groups.id') end + def self.custom_fields + fields = FoodsoftConfig[:custom_fields] && FoodsoftConfig[:custom_fields][:ordergroup] + return [] unless fields + fields.map(&:deep_symbolize_keys) + end + def last_user_activity last_active_user = users.order('users.last_activity DESC').first if last_active_user diff --git a/app/views/admin/ordergroups/index.html.haml b/app/views/admin/ordergroups/index.html.haml index f3c2133d..1a3b7f51 100644 --- a/app/views/admin/ordergroups/index.html.haml +++ b/app/views/admin/ordergroups/index.html.haml @@ -2,6 +2,9 @@ - content_for :actionbar do = link_to t('.new_ordergroup'), new_admin_ordergroup_path, class: 'btn btn-primary' + = link_to url_for(search: params[:q], format: :csv), class: 'btn' do + = glyph :download + CSV - content_for :sidebar do %p= t('.first_paragraph', url: link_to(t('.new_ordergroups'), new_admin_ordergroup_path)).html_safe @@ -12,4 +15,4 @@ = text_field_tag :query, params[:query], class: 'input-medium search-query', placeholder: t('admin.search_placeholder') #ordergroups - = render "ordergroups" \ No newline at end of file + = render "ordergroups" diff --git a/lib/ordergroups_csv.rb b/lib/ordergroups_csv.rb new file mode 100644 index 00000000..c41d2e83 --- /dev/null +++ b/lib/ordergroups_csv.rb @@ -0,0 +1,41 @@ +class OrdergroupsCsv < RenderCSV + include ApplicationHelper + + def header + row = [ + Ordergroup.human_attribute_name(:id), + Ordergroup.human_attribute_name(:name), + Ordergroup.human_attribute_name(:description), + Ordergroup.human_attribute_name(:account_balance), + Ordergroup.human_attribute_name(:created_on), + Ordergroup.human_attribute_name(:contact_person), + Ordergroup.human_attribute_name(:contact_phone), + Ordergroup.human_attribute_name(:contact_address), + Ordergroup.human_attribute_name(:break_start), + Ordergroup.human_attribute_name(:break_end), + Ordergroup.human_attribute_name(:last_user_activity), + Ordergroup.human_attribute_name(:last_order), + ] + row + Ordergroup.custom_fields.map { |f| f[:label] } + end + + def data + @object.each do |o| + row = [ + o.id, + o.name, + o.description, + o.account_balance, + o.created_on, + o.contact_person, + o.contact_phone, + o.contact_address, + o.break_start, + o.break_end, + o.last_user_activity, + o.last_order.try(:starts), + ] + yield row + Ordergroup.custom_fields.map { |f| o.settings.custom_fields[f[:name]] } + end + end +end