Module: Sinatra::Hat::Actions

Included in:
Maker
Defined in:
lib/sinatras-hat/actions.rb

Overview

Contains all of the actions that Sinatra’s Hat supports. Each action states a name, a path, optionally, the HTTP verb, then a block which takes a request object, optionally loads data using the :finder or :record options, then responds, based on whether or not the action was a success

NOTE: only the :create action renders a different :failure

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(map) ⇒ Object



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sinatras-hat/actions.rb', line 11

def self.included(map)
  map.action :destroy, '/:id', :verb => :delete do |request|
    record = model.find(request.params) || request.not_found
    record.destroy
    responder.success(:destroy, request, record)
  end
  
  map.action :new, '/new' do |request|
    new_record = model.new(request.params)
    responder.success(:new, request, new_record)
  end
  
  map.action :update, '/:id', :verb => :put do |request|
    record = model.update(request.params) || request.not_found
    result = record.save ? :success : :failure
    responder.send(result, :update, request, record)
  end
  
  map.action :edit, '/:id/edit' do |request|
    record = model.find(request.params) || request.not_found
    responder.success(:edit, request, record)
  end

  map.action :show, '/:id' do |request|
    record = model.find(request.params) || request.not_found
    set_cache_headers(request, record) unless protected?(:show)
    responder.success(:show, request, record)
  end
  
  map.action :create, '/', :verb => :post do |request|
    record = model.new(request.params)
    result = record.save ? :success : :failure
    responder.send(result, :create, request, record)
  end

  map.action :index, '/' do |request|
    records = model.all(request.params)
    set_cache_headers(request, records) unless protected?(:index)
    responder.success(:index, request, records)
  end
  
  private
  
  def set_cache_headers(request, data)
    
    set_etag(request, data)
    set_last_modified(request, data)
  end
  
  def set_etag(request, data)
    record = model.find_last_modified(Array(data))
    return unless record.respond_to?(:updated_at)
    request.etag("#{record.id}-#{record.updated_at}-#{data.is_a?(Array)}")
  end
  
  def set_last_modified(request, data)
    record = model.find_last_modified(Array(data))
    return unless record.respond_to?(:updated_at)
    request.last_modified(record.updated_at)
  end
end

Instance Method Details

#set_cache_headers(request, data) ⇒ Object



54
55
56
57
58
# File 'lib/sinatras-hat/actions.rb', line 54

def set_cache_headers(request, data)
  
  set_etag(request, data)
  set_last_modified(request, data)
end

#set_etag(request, data) ⇒ Object



60
61
62
63
64
# File 'lib/sinatras-hat/actions.rb', line 60

def set_etag(request, data)
  record = model.find_last_modified(Array(data))
  return unless record.respond_to?(:updated_at)
  request.etag("#{record.id}-#{record.updated_at}-#{data.is_a?(Array)}")
end

#set_last_modified(request, data) ⇒ Object



66
67
68
69
70
# File 'lib/sinatras-hat/actions.rb', line 66

def set_last_modified(request, data)
  record = model.find_last_modified(Array(data))
  return unless record.respond_to?(:updated_at)
  request.last_modified(record.updated_at)
end