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