Class: RestaurantTagGroup

Inherits:
ApplicationRecord show all
Includes:
IdentityCache, SoftDelete
Defined in:
app/models/restaurant_tag_group.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SoftDelete

#soft_deleted?, #soft_destroy

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.sort_rank(from_tag_id, to_tag_id) ⇒ Object



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
# File 'app/models/restaurant_tag_group.rb', line 69

def self.sort_rank(from_tag_id, to_tag_id)
  RestaurantTagGroup.transaction do
    Restaurant.transaction do
      from_tag_id = find from_tag_id.to_i
      to_tag_id = find to_tag_id.to_i
      new_rank = to_tag_id.rank

      operator = from_tag_id.rank > to_tag_id.rank ? '<' : '>'
      limit_operator = from_tag_id.rank > to_tag_id.rank ? '>=' : '<='
      affected_restaurants = where("rank #{operator} ?", from_tag_id.rank).
        where("rank #{limit_operator} ?", to_tag_id.rank)
      inc_or_dec = operator == '>' ? -1 : 1
      affected_restaurants.lock.each do |restaurant|
        restaurant.rank = restaurant.rank + inc_or_dec
        restaurant.sneaky_save!
      end
      from_tag_id.rank = new_rank
      from_tag_id.sneaky_save!
      affected_restaurants = affected_restaurants.map do |restaurant|
        { id: restaurant.id, rank: restaurant.rank }
      end
      affected_restaurants.push(id: from_tag_id.id, rank: from_tag_id.rank)
      affected_restaurants
    end
  end
end

Instance Method Details

#localized_photoObject



59
60
61
# File 'app/models/restaurant_tag_group.rb', line 59

def localized_photo
  send(:"photo_#{default_photo_locale}")
end

#localized_photo_url(_version = nil) ⇒ Object



63
64
65
66
67
# File 'app/models/restaurant_tag_group.rb', line 63

def localized_photo_url(_version = nil)
  # ignore version parameter
  # we use imgproxy to resize the image
  send(:"photo_#{default_photo_locale}_url")
end