Module: RestAssured::RedirectRoutes

Included in:
Application
Defined in:
lib/rest-assured/routes/redirect.rb

Class Method Summary collapse

Class Method Details

.included(router) ⇒ Object



3
4
5
6
7
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
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
72
# File 'lib/rest-assured/routes/redirect.rb', line 3

def self.included(router)
  router.get '/redirects' do
    @redirects = Models::Redirect.ordered
    haml :'redirects/index'
  end

  router.get '/redirects/new' do
    @redirect = Models::Redirect.new
    haml :'redirects/new'
  end

  router.post /\/redirects(.json)?/ do |needs_json|
    @redirect = Models::Redirect.create(params['redirect'] || { :pattern => params['pattern'], :to => params['to'] })

    if needs_json
      if @redirect.errors.present?
        status 400
        body @redirect.errors.full_messages.join("\n")
      end
    else
      if @redirect.errors.blank?
        flash[:notice] = "Redirect created"
        redirect '/redirects'
      else
        flash.now[:error] = "Crumps! " + @redirect.errors.full_messages.join("; ")
        haml :'redirects/new'
      end
    end
  end

  router.get %r{/redirects/(\d+)/edit} do |id|
    @redirect = Models::Redirect.find(id)
    haml :'redirects/edit'
  end

  router.put %r{/redirects/(\d+)} do |id|
    @redirect = Models::Redirect.find(id)

    @redirect.update_attributes(params['redirect'])

    if @redirect.save
      flash[:notice] = 'Redirect updated'
      redirect '/redirects'
    else
      flash[:error] = 'Crumps! ' + @redirect.errors.full_messages.join("\n")
      haml :'redirects/edit'
    end
  end

  router.put '/redirects/reorder' do
    if params['redirect']
      if Models::Redirect.update_order(params['redirect'])
        'Changed'
      else
        'Crumps! It broke'
      end
    end
  end

  router.delete %r{/redirects/(\d+)} do |id|
    if Models::Redirect.destroy(id)
      flash[:notice] = 'Redirect deleted'
      redirect '/redirects'
    end
  end

  router.delete '/redirects/all' do
    status Models::Redirect.destroy_all ? 200 : 500
  end
end