Class: SpreeCmCommissioner::Places::FindWithRoute

Inherits:
Object
  • Object
show all
Defined in:
app/finders/spree_cm_commissioner/places/find_with_route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(place_type:, place_id: nil, keyword: nil) ⇒ FindWithRoute

Returns a new instance of FindWithRoute.



25
26
27
28
29
# File 'app/finders/spree_cm_commissioner/places/find_with_route.rb', line 25

def initialize(place_type:, place_id: nil, keyword: nil)
  @place_type = place_type
  @place_id = place_id
  @keyword = keyword
end

Instance Attribute Details

#keywordObject (readonly)

Returns the value of attribute keyword.



23
24
25
# File 'app/finders/spree_cm_commissioner/places/find_with_route.rb', line 23

def keyword
  @keyword
end

#place_idObject (readonly)

Returns the value of attribute place_id.



23
24
25
# File 'app/finders/spree_cm_commissioner/places/find_with_route.rb', line 23

def place_id
  @place_id
end

#place_typeObject (readonly)

Returns the value of attribute place_type.



23
24
25
# File 'app/finders/spree_cm_commissioner/places/find_with_route.rb', line 23

def place_type
  @place_type
end

Instance Method Details

#executeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/finders/spree_cm_commissioner/places/find_with_route.rb', line 31

def execute
  return SpreeCmCommissioner::Place.none if place_type.blank?

  result = if place_type == 'origin'
             scope = SpreeCmCommissioner::Place.with_routes_as_origin
             place_id.present? ? scope.where(cm_routes: { destination_place_id: place_id }) : scope
           else
             scope = SpreeCmCommissioner::Place.with_routes_as_destination
             place_id.present? ? scope.where(cm_routes: { origin_place_id: place_id }) : scope
           end

  return SpreeCmCommissioner::Place.none if place_id.present? && !SpreeCmCommissioner::Place.exists?(place_id)

  # Apply keyword filter if provided
  result = result.where('cm_places.name ILIKE ?', "%#{keyword}%") if keyword.present?
  result
end