Module: Ditty::Helpers::Response
- Defined in:
- lib/ditty/helpers/response.rb
Instance Method Summary collapse
- #actions(entity = nil) ⇒ Object
- #create_response(entity) ⇒ Object
- #delete_response(_entity) ⇒ Object
- #list_response(result) ⇒ Object
- #read_response(entity) ⇒ Object
- #update_response(entity) ⇒ Object
Instance Method Details
#actions(entity = nil) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/ditty/helpers/response.rb', line 51 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 |
#create_response(entity) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ditty/helpers/response.rb', line 38 def create_response(entity) respond_to do |format| format.html do flash[:success] = "#{heading} Created" redirect with_layout("#{base_path}/#{entity.id}") end format.json do content_type :json redirect "#{base_path}/#{entity.id}", 201 end end end |
#delete_response(_entity) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ditty/helpers/response.rb', line 94 def delete_response(_entity) respond_to do |format| format.html do flash[:success] = "#{heading} Deleted" redirect with_layout(base_path.to_s) end format.json do content_type :json headers 'Location' => base_path.to_s status 204 end end end |
#list_response(result) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ditty/helpers/response.rb', line 8 def list_response(result) respond_to do |format| format.html do actions = {} actions["#{base_path}/new"] = "New #{heading}" if policy(settings.model_class).create? haml :"#{view_location}/index", locals: { list: result, title: heading(:list), actions: actions }, layout: layout end format.json do # TODO: Add links defined by actions (New #{heading}) total = result.respond_to?(:pagination_record_count) ? result.pagination_record_count : result.count json( 'items' => result.all.map(&:for_json), '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 |
#read_response(entity) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ditty/helpers/response.rb', line 58 def read_response(entity) actions = actions(entity) respond_to do |format| format.html do 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 |
#update_response(entity) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ditty/helpers/response.rb', line 80 def update_response(entity) respond_to do |format| format.html do # TODO: Ability to customize the return path and message? flash[:success] = "#{heading} Updated" redirect with_layout("#{base_path}/#{entity.id}") end format.json do headers 'Location' => "#{base_path}/#{entity.id}" json body entity.for_json end end end |