Module: Ditty::Helpers::Response

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

Instance Method Summary collapse

Instance Method Details

#create_response(entity) ⇒ Object



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

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

#delete_response(_entity) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ditty/helpers/response.rb', line 68

def delete_response(_entity)
  respond_to do |format|
    format.html do
      flash[:success] = "#{heading} Deleted"
      redirect base_path.to_s
    end
    format.json do
      content_type :json
      headers 'Location' => "#{base_path}"
      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
# 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 }
    end
    format.json do
      # TODO: Add links defined by actions (New #{heading})
      json(
        'items' => result.all.map(&:for_json),
        'page' => (params['page'] || 1).to_i,
        'count' => result.count,
        'total' => result.pagination_record_count
      )
    end
  end
end

#read_response(entity) ⇒ Object



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

def read_response(entity)
  respond_to do |format|
    format.html do
      actions = {}
      actions["#{base_path}/#{entity.id}/edit"] = "Edit #{heading}" if policy(entity).update?
      title = heading(:read) + (entity.respond_to?(:name) ? ": #{entity.name}" : '')
      haml :"#{view_location}/display", locals: { entity: entity, title: title, actions: actions }
    end
    format.json do
      # TODO: Add links defined by actions (Edit #{heading})
      json entity.for_json
    end
  end
end

#update_response(entity) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ditty/helpers/response.rb', line 54

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 "#{base_path}/#{entity.id}"
    end
    format.json do
      headers 'Location' => "#{base_path}/#{entity.id}"
      json body entity.for_json
    end
  end
end