aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/referential_lines_controller.rb
blob: 37051faeb65076353f87aa882fa2bccf799efbcc (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class ReferentialLinesController < ChouetteController
  include ReferentialSupport
  include PolicyChecker

  defaults :resource_class => Chouette::Line, :collection_name => 'lines', :instance_name => 'line'
  respond_to :html
  respond_to :xml
  respond_to :json
  respond_to :kml, :only => :show
  respond_to :js, :only => :index

  belongs_to :referential

  def show
    @routes = resource.routes.order(:objectid)

    case sort_route_column
    when "stop_points", "journey_patterns"
      left_join = %Q{LEFT JOIN "#{sort_route_column}" ON "#{sort_route_column}"."route_id" = "routes"."id"}

      @routes = @routes.joins(left_join).group(:id).order("count(#{sort_route_column}.route_id) #{sort_route_direction}")
    else
      @routes = @routes.order("#{sort_route_column} #{sort_route_direction}")
    end

    @q = @routes.ransack(params[:q])
    @routes = @q.result

    @routes = @routes.paginate(page: params[:page], per_page: 10)

    @routes = RouteDecorator.decorate(
      @routes,
      context: {
        referential: referential,
        line: @line
      }
    )

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

  # overwrite inherited resources to use delete instead of destroy
  # foreign keys will propagate deletion)
  def destroy_resource(object)
    object.delete
  end

  def delete_all
    objects =
      get_collection_ivar || set_collection_ivar(end_of_association_chain.where(:id => params[:ids]))
    objects.each { |object| object.delete }
    respond_with(objects, :location => smart_collection_url)
  end

  def name_filter
    respond_to do |format|
      format.json { render :json => filtered_lines_maps}
    end
  end

  protected

  def build_resource
    super.tap do |resource|
      resource.line_referential = referential.line_referential
    end
  end

  def filtered_lines_maps
    filtered_lines.collect do |line|
      { :id => line.id, :name => (line.published_name ? line.published_name : line.name) }
    end
  end

  def filtered_lines
    referential.lines.by_text(params[:q])
  end

  def collection
    %w(network_id company_id group_of_lines_id comment_id transport_mode).each do |filter|
      if params[:q] && params[:q]["#{filter}_eq"] == '-1'
        params[:q]["#{filter}_eq"] = ''
        params[:q]["#{filter}_blank"] = '1'
      end
    end

    @q = referential.lines.search(params[:q])

    if sort_column && sort_direction
      @lines ||= @q.result(:distinct => true).order(sort_column + ' ' + sort_direction)
    else
      @lines ||= @q.result(:distinct => true).order(:number)
    end
    @lines = @lines.paginate(page: params[:page], per_page: 10).includes([:network, :company])

  end

  private

  def sort_column
    referential.lines.column_names.include?(params[:sort]) ? params[:sort] : 'number'
  end
  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  def sort_route_column
    (@line.routes.column_names + %w{stop_points journey_patterns}).include?(params[:sort]) ? params[:sort] : 'name'
  end
  def sort_route_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  def line_params
    params.require(:line).permit(
      :transport_mode,
      :transport_submode,
      :network_id,
      :company_id,
      :objectid,
      :object_version,
      :name, :number,
      :published_name,
      :transport_mode,
      :registration_number,
      :comment,
      :mobility_restricted_suitability,
      :int_user_needs,
      :flexible_service,
      :group_of_lines,
      :group_of_line_ids,
      :group_of_line_tokens,
      :url,
      :color,
      :text_color,
      :stable_id
      )
  end

end