Module: Ditty::Helpers::Response

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

Instance Method Summary collapse

Instance Method Details

#create_response(entity) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ditty/helpers/response.rb', line 28

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ditty/helpers/response.rb', line 73

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ditty/helpers/response.rb', line 6

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
  end
end

#read_response(entity) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ditty/helpers/response.rb', line 41

def read_response(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
  end
end

#update_response(entity) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ditty/helpers/response.rb', line 59

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