aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/routing_constraint_zones_controller.rb
blob: 886247e7901737248c932a5df2222a47b2fe55f9 (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
class RoutingConstraintZonesController < ChouetteController
  include ReferentialSupport
  include PolicyChecker

  defaults resource_class: Chouette::RoutingConstraintZone
  respond_to :html, :xml, :json

  # before_action :check_stoppoint_param, only: [:create, :update]

  belongs_to :referential do
    belongs_to :line, parent_class: Chouette::Line
  end

  def index
    index! do |format|
      format.html do
        @routing_constraint_zones = RoutingConstraintZoneDecorator.decorate(
          @routing_constraint_zones,
          context: {
            referential: referential,
            line: parent
          }
        )
      end
      format.json
    end
  end

  def show
    show! do |format|
      @routing_constraint_zone = @routing_constraint_zone.decorate(context: {
        referential: referential,
        line: parent
      })
    end
  end

  def new
    new! do |format|
      format.html
      format.js {
        # Get selected route in the form view
        @route = @line.routes.find params[:route_id] if params[:route_id]
        # Get the routing_constraint_zone, the main use is when validation failed
        routing_constraint_zone = @line.routing_constraint_zones.new(JSON(params[:routing_constraint_zone_json])) if params[:routing_constraint_zone_json]
        @routing_constraint_zone = routing_constraint_zone || build_resource
      }
    end
  end

  protected

  alias_method :routing_constraint_zone, :resource
  alias_method :line, :parent

  def collection
    @q = line.routing_constraint_zones.search(params[:q])

    @routing_constraint_zones ||= begin
      routing_constraint_zones = sort_collection
      routing_constraint_zones = routing_constraint_zones.paginate(
        page: params[:page],
        per_page: 10
      )
    end
  end

  private
  def sort_column
    (
      Chouette::RoutingConstraintZone.column_names +
      [
        'stop_points_count',
        'route'
      ]
    ).include?(params[:sort]) ? params[:sort] : 'name'
  end
  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  def sort_collection
    sort_by = sort_column

    if sort_by == 'stop_points_count'
      @q.result.order_by_stop_points_count(sort_direction)
    elsif sort_by == 'route'
      @q.result.order_by_route_name(sort_direction)
    else
      @q.result.order(sort_column + ' ' + sort_direction)
    end
  end

  def routing_constraint_zone_params
    params.require(:routing_constraint_zone).permit(
      :name,
      { stop_point_ids: [] },
      :line_id,
      :route_id,
      :objectid,
      :object_version,
    )
  end

  def check_stoppoint_param
    spArr = []
    if params.require(:routing_constraint_zone)[:stop_point_ids] and params.require(:routing_constraint_zone)[:stop_point_ids].length >= 2
      params.require(:routing_constraint_zone)[:stop_point_ids].each do |k,v|
        spArr << v
      end
      params.require(:routing_constraint_zone)[:stop_point_ids] = spArr
    else
      Rails.logger.error("Error: An ITL must have at least two stop points")
    end
  end

end