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

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

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

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

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

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

  router.put %r{/redirects/(\d+)} do |id|
    @redirect = 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 Redirect.update_order(params['redirect'])
        'Changed'
      else
        'Crumps! It broke'
      end
    end
  end

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

end