Class: HhPackage::RestaurantPackageManager

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/hh_package/restaurant_package_manager.rb

Overview

Responsible to manage Restaurant's Package data

Class Method Summary collapse

Class Method Details

.add_package(restaurant_id, package) ⇒ Object

Add relation



20
21
22
23
24
25
26
27
28
# File 'app/my_lib/hh_package/restaurant_package_manager.rb', line 20

def add_package(restaurant_id, package)
  rp = RestaurantPackage.new(restaurant_id: restaurant_id, package: package, deleted_at: nil)
  status = rp.save
  Restaurant.find(restaurant_id).touch if status
  {
    success: status,
    errors: rp.errors
  }
end

.destroy_package(restaurant_id, package) ⇒ Object

Delete relation



31
32
33
34
35
36
37
38
39
40
# File 'app/my_lib/hh_package/restaurant_package_manager.rb', line 31

def destroy_package(restaurant_id, package)
  RestaurantPackage.transaction do
    RestaurantPackage.where(restaurant_id: restaurant_id, package: package).each do |rp|
      rp.update_attributes! deleted_at: Time.zone.now
    end
    Restaurant.find(restaurant_id).touch
    return { success: true }
  end
  { success: false }
end

.where_by(restaurant_id: nil, package: nil) ⇒ Object

Parameters:



9
10
11
12
13
14
15
16
17
# File 'app/my_lib/hh_package/restaurant_package_manager.rb', line 9

def where_by(restaurant_id: nil, package: nil)
  if !restaurant_id.nil? && package.nil?
    HhPackage::RestaurantPackage.includes(package: [:agendas, :translations]).where(restaurant_id: restaurant_id, deleted_at: nil)
  elsif !package.nil? && restaurant_id.nil?
    HhPackage::RestaurantPackage.includes(package: [:agendas, :translations]).where(package: package, deleted_at: nil)
  else
    raise NotImplementedError, 'not supported yet'
  end
end