lib/ditty/helpers/response.rb in ditty-0.7.2 vs lib/ditty/helpers/response.rb in ditty-0.8.0
- old
+ new
@@ -1,7 +1,9 @@
# frozen_string_literal: true
+require 'csv'
+
module Ditty
module Helpers
module Response
def list_response(result)
respond_to do |format|
@@ -20,10 +22,18 @@
'page' => (params['page'] || 1).to_i,
'count' => result.count,
'total' => total
)
end
+ format.csv do
+ CSV.generate do |csv|
+ csv << result.first.for_csv.keys
+ result.all.each do |r|
+ csv << r.for_csv.values
+ end
+ end
+ end
end
end
def create_response(entity)
respond_to do |format|
@@ -36,23 +46,34 @@
redirect "#{base_path}/#{entity.id}", 201
end
end
end
+ def actions(entity = nil)
+ actions = {}
+ actions["#{base_path}/#{entity.id}/edit"] = "Edit #{heading}" if entity && policy(entity).update?
+ actions["#{base_path}/new"] = "New #{heading}" if policy(settings.model_class).create?
+ actions
+ end
+
def read_response(entity)
+ actions = actions(entity)
respond_to do |format|
format.html do
- actions = {}
- actions["#{base_path}/#{entity.id}/edit"] = "Edit #{heading}" if policy(entity).update?
- actions["#{base_path}/new"] = "New #{heading}" if policy(entity).create?
title = heading(:read) + (entity.respond_to?(:name) ? ": #{entity.name}" : '')
haml :"#{view_location}/display",
locals: { entity: entity, title: title, actions: actions },
layout: layout
end
format.json do
# TODO: Add links defined by actions (Edit #{heading})
json entity.for_json
+ end
+ format.csv do
+ CSV.generate do |csv|
+ csv << entity.for_csv.keys
+ csv << entity.for_csv.values
+ end
end
end
end
def update_response(entity)