cirandas.net

ref: master

plugins/custom_routes/controllers/custom_routes_plugin_admin_controller.rb


 1
 2
 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
class CustomRoutesPluginAdminController < AdminController

  before_filter :accept_only_post, :only => [:create, :update, :destroy]

  def index
    @routes = environment.custom_routes.all
  end

  def new
    @route = environment.custom_routes.new
  end

  def create
    params[:route][:enabled] ||= false
    @route = environment.custom_routes.new(params[:route])

    if @route.save
      redirect_to action: :index
    else
      session[:notice] = _('Could not save the route mapping.')
      render action: :new
    end
  end

  def edit
    @route = environment.custom_routes.find_by(id: params[:route_id])
    render_not_found unless @route
  end

  def update
    params[:route][:enabled] ||= false
    @route = environment.custom_routes.find_by(id: params[:route_id])
    return render_not_found unless @route

    if @route.update(params[:route])
      redirect_to action: :index
    else
      session[:notice] = _('Could not update the route mapping.')
      render action: :edit
    end
  end

  def destroy
    begin
      environment.custom_routes.destroy(params[:route_id])
      render :json => { msg: 'ok' }, status: 200
    rescue
      render :json => { msg: 'Could not remove this route mapping' },
             status: 400
    end
  end

end