aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/routes_controller.rb
blob: 852477c5c4605df342112f0a9b7498e57df40bc4 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class RoutesController < ChouetteController
  include PolicyChecker
  defaults :resource_class => Chouette::Route

  respond_to :html, :xml, :json
  respond_to :kml, :only => :show
  respond_to :js, :only => :show

  belongs_to :referential do
    belongs_to :line, :parent_class => Chouette::Line, :optional => true, :polymorphic => true
  end
  before_action :define_candidate_opposite_routes, only: [:new, :edit]

  def index
    index! do |format|
      format.html { redirect_to referential_line_path(@referential, @line) }
    end
  end

  def edit_boarding_alighting
    @route = route
    build_breadcrumb :edit
  end

  def save_boarding_alighting
    @route = route
    if @route.update_attributes!(route_params)
      redirect_to referential_line_route_path(@referential, @line, @route)
    else
      render "edit_boarding_alighting"
    end
  end

  def show
    @map = RouteMap.new(route).with_helpers(self)

    @route_sp = route.stop_points
    if sort_sp_column && sort_sp_direction
      @route_sp = @route_sp.order(sort_sp_column + ' ' + sort_sp_direction)
    else
      @route_sp = @route_sp.order(:position)
    end

    show! do
      @route = @route.decorate(context: {
        referential: @referential,
        line: @line
      })

      @route_sp = ModelDecorator.decorate(
        @route_sp,
        with: StopPointDecorator
      )

      build_breadcrumb :show
    end
  end

  def destroy
    destroy! do |success, failure|
      success.html { redirect_to referential_line_path(@referential,@line) }
    end
  end

  def create
    create! do |success, failure|
      success.html { redirect_to referential_line_path(@referential,@line) }
      failure.html { flash[:alert] = route.errors[:flash]; render :action => :new }
    end
  end

  def duplicate
    @route = Chouette::Route.find(params[:id])
    @line = @route.line
    build_breadcrumb(:edit)
  end

  protected

  alias_method :route, :resource

  def collection
    @q = parent.routes.search(params[:q])
    @routes ||=
      begin
        routes = @q.result(:distinct => true).order(:name)
        routes = routes.paginate(:page => params[:page]) if @per_page.present?
        routes
      end
  end

  def define_candidate_opposite_routes
    scope = if params[:id]
      parent.routes.where(opposite_route: [nil, resource]).where('id <> ?', resource.id)
    else
      parent.routes.where(opposite_route: nil)
    end
    @forward  = scope.where(wayback: :straight_forward)
    @backward = scope.where(wayback: :backward)
  end

  private

  def sort_sp_column
    route.stop_points.column_names.include?(params[:sort]) ? params[:sort] : 'position'
  end
  def sort_sp_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  def route_params
    params.require(:route).permit(
      :line_id,
      :objectid,
      :object_version,
      :creator_id,
      :name,
      :comment,
      :opposite_route_id,
      :published_name,
      :number,
      :direction,
      :wayback,
      stop_points_attributes: [:id, :_destroy, :position, :stop_area_id, :for_boarding, :for_alighting]
    )
  end

end