Module: Ditty::Helpers::Response

Defined in:
lib/ditty/helpers/response.rb

Instance Method Summary collapse

Instance Method Details

#actions(entity = nil) ⇒ Object



53
54
55
56
57
58
# File 'lib/ditty/helpers/response.rb', line 53

def actions(entity = nil)
  actions = {}
  actions["#{base_path}/#{entity.display_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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ditty/helpers/response.rb', line 40

def create_response(entity)
  respond_to do |format|
    format.html do
      flash[:success] = "#{heading} Created"
      redirect with_layout(flash[:redirect_to] || "#{base_path}/#{entity.display_id}")
    end
    format.json do
      content_type :json
      redirect "#{base_path}/#{entity.display_id}", 201
    end
  end
end

#delete_response(_entity) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ditty/helpers/response.rb', line 102

def delete_response(_entity)
  respond_to do |format|
    format.html do
      flash[:success] = "#{heading} Deleted"
      redirect with_layout(flash[:redirect_to] || back || base_path)
    end
    format.json do
      content_type :json
      redirect base_path.to_s, 204
    end
  end
end

#list_response(result, view: 'index') ⇒ Object



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
37
38
# File 'lib/ditty/helpers/response.rb', line 10

def list_response(result, view: 'index')
  respond_to do |format|
    format.html do
      actions = {}
      actions["#{base_path}/new"] = "New #{heading}" if policy(settings.model_class).create?
      haml :"#{view_location}/#{view}",
           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 { |e| permitted_response_attributes(e, :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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ditty/helpers/response.rb', line 60

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 permitted_response_attributes(entity, :for_json)
    end
    format.csv do
      CSV.generate do |csv|
        csv << entity.for_csv.keys
        csv << entity.for_csv.values
      end
    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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ditty/helpers/response.rb', line 88

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(flash[:redirect_to] || back || "#{base_path}/#{entity.display_id}")
    end
    format.json do
      content_type :json
      redirect "#{base_path}/#{entity.display_id}", 200, json(entity.for_json)
    end
  end
end