Class: SearchIcon

Inherits:
ApplicationRecord show all
Extended by:
Enumerize
Defined in:
app/models/search_icon.rb

Overview

Schema Information

Table name: search_icons

id                :bigint           not null, primary key
icon_type         :string(191)
image             :string(191)
is_active         :boolean          default(TRUE)
position          :integer
tag               :string(191)
title_cn          :string(191)
title_en          :string(191)
title_th          :string(191)
url               :text(65535)
created_at        :datetime         not null
updated_at        :datetime         not null
restaurant_tag_id :bigint

Indexes

index_search_icons_on_restaurant_tag_id  (restaurant_tag_id)

Constant Summary collapse

ICON_TYPE =
{
  tag_base: 'Tag Base',
  url_base: 'URL Base',
}.freeze
ICON_TYPE_TAG_BASE =
'tag_base'
ICON_TYPE_URL_BASE =
'url_base'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.iconic_typesObject



55
56
57
# File 'app/models/search_icon.rb', line 55

def self.iconic_types
  SearchIcon::ICON_TYPE.values
end

Instance Method Details

#allowed_to_edit_city?Boolean

Only allowed to edit city for these cases:

  • icon_type is URL_BASE

  • icon_type is TAG_BASE and restaurant_tag has no city_id (city_id is nil)

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'app/models/search_icon.rb', line 78

def allowed_to_edit_city?
  return true if icon_type == ICON_TYPE_URL_BASE || restaurant_tag.blank?

  return false if restaurant_tag&.city_id.present?

  true
end

#city_idsObject



67
68
69
70
71
72
73
# File 'app/models/search_icon.rb', line 67

def city_ids
  return [] if search_icons_cities.blank?

  return [] unless [ICON_TYPE_TAG_BASE, ICON_TYPE_URL_BASE].include?(icon_type)

  search_icons_cities.pluck(:city_id)
end

#total_citiesObject



59
60
61
62
63
64
65
# File 'app/models/search_icon.rb', line 59

def total_cities
  return 0 if search_icons_cities.blank?

  return 0 unless [ICON_TYPE_TAG_BASE, ICON_TYPE_URL_BASE].include?(icon_type)

  search_icons_cities.count
end