Class: Spree::Transit::TripsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/transit/trips_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#collection_url, #current_vendor, #edit_object_url, #page, #per_page, #required_vendor_user!, #vendors

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/spree/transit/trips_controller.rb', line 24

def create
  @trip = model_class.new
  if @trip.update(trip_params(nested_stop_attributes))
    flash[:success] = flash_message_for(@trip, :successfully_created)
    redirect_to edit_transit_route_trip_path(@product, @product.trip.id)
  else
    flash[:error] = "create failed. Errors: #{@trip.errors.full_messages.join(', ')}"
    redirect_back(fallback_location: new_transit_route_trip_path(@product))
  end
end

#editObject



18
19
20
21
22
# File 'app/controllers/spree/transit/trips_controller.rb', line 18

def edit
  @trip_stops = @trip.trip_stops.sort_by(&:sequence)
  @boarding_stops = @trip.trip_stops.boarding.to_a.pluck(:stop_id)
  @drop_off_stops = @trip.trip_stops.drop_off.to_a.pluck(:stop_id)
end

#load_dataObject



6
7
8
9
10
11
12
# File 'app/controllers/spree/transit/trips_controller.rb', line 6

def load_data
  @product = Spree::Product.find_by(slug: params[:route_id])
  @vehicles = SpreeCmCommissioner::Vehicle.includes(:vehicle_type).order('cm_vehicle_types.name')
  @stops = Spree::Taxonomy.place.taxons.select(&:stop?).to_a
  @places = Spree::Taxonomy.place.taxons.select(&:location?).to_a
  @trip = SpreeCmCommissioner::Trip.find_by(id: params[:id]) if params[:id]
end

#location_after_saveObject



85
86
87
# File 'app/controllers/spree/transit/trips_controller.rb', line 85

def location_after_save
  transit_routes_path
end

#model_classObject



14
15
16
# File 'app/controllers/spree/transit/trips_controller.rb', line 14

def model_class
  SpreeCmCommissioner::Trip
end

#nested_stop_attributesObject



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
# File 'app/controllers/spree/transit/trips_controller.rb', line 44

def nested_stop_attributes
  cm_params = params.require('spree_cm_commissioner_trip')
  trip_stops = @trip.trip_stops.index_by(&:stop_id)

  boarding_points_attributes = (cm_params.delete('boarding_points') || []).compact_blank
                                                                          .map do |point_id|
    trip_stop = trip_stops.delete(point_id.to_i)
    { stop_id: point_id, stop_type: 'boarding', id: trip_stop.try(:id) }
  end

  drop_off_points_attributes = (cm_params.delete('drop_off_points') || []).compact_blank
                                                                          .map do |point_id|
    trip_stop = trip_stops.delete(point_id.to_i)
    { stop_id: point_id, stop_type: 'drop_off', id: trip_stop.try(:id) }
  end

  trip_stops.each_value do |trip_stop|
    next unless trip_stop.stop_id != @trip.origin_id && trip_stop.stop_id != @trip.destination_id

    boarding_points_attributes << { id: trip_stop.id,
                                    _destroy: '1'
                                  }
  end

  cm_params[:trip_stops_attributes] = boarding_points_attributes + drop_off_points_attributes
  cm_params
end

#object_nameObject



81
82
83
# File 'app/controllers/spree/transit/trips_controller.rb', line 81

def object_name
  'spree_cm_commissioner_trip'
end

#trip_params(params) ⇒ Object



72
73
74
75
76
77
78
79
# File 'app/controllers/spree/transit/trips_controller.rb', line 72

def trip_params(params)
  params.permit(
    :origin_id, :destination_id, :vehicle_id, :hours, :minutes, :seconds,
    'departure_time(1i)', 'departure_time(2i)', 'departure_time(3i)',
    'departure_time(4i)', 'departure_time(5i)', :product_id,
    trip_stops_attributes: i[stop_id stop_type _destroy id]
  )
end

#updateObject



35
36
37
38
39
40
41
42
# File 'app/controllers/spree/transit/trips_controller.rb', line 35

def update
  if @trip.update(trip_params(nested_stop_attributes))
    flash[:success] = flash_message_for(@trip, :successfully_updated)
  else
    flash[:error] = "update failed. Errors: #{@trip.errors.full_messages.join(', ')}"
  end
  redirect_back(fallback_location: edit_transit_route_trip_path(@product, @trip))
end