Class: Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tag
- Defined in:
- app/models/tag.rb
Overview
The Tag model.
Class Method Summary (collapse)
-
+ (Object) create_with_data(text, lang_code, purchase_id, gender, hardware_id, hardware_type, ble_major, ble_minor, name, parent_id, latitude, longitude, customer)
Create a new tag with the given data.
-
+ (Object) my_top10_chart_data(current_customer)
Returns the top 10 scanned tags.
-
+ (Object) show_by_ble_hardware_id(hardware_id, hardware_type, major, minor)
Show all tags by bluetooth le hardware id.
-
+ (Object) show_by_hardware_id(hardware_id, hardware_type)
Show all tags by hardware id and hardware type.
Instance Method Summary (collapse)
-
- (Object) active_date_to_string
Returns the active date as a string.
-
- (Object) active_time_to_string
Returns the active time as a string.
-
- (Object) add_hardware_id_to_tag(hardware_id, hardware_type, ble_major, ble_minor, latitude, longitude)
Add a hardware to a tag.
-
- (Object) add_location_to_hardware(hardware_id, latitude, longitude)
Add a location to a hardware.
-
- (Object) current_date_active
Checks if the current tag is active.
-
- (Object) current_time_active
Returns true if the active_time is currently active, else it returns false.
-
- (Object) currently_active
Returns true if the tag is currently active, else it returns false.
-
- (Object) display_name
Show a display name.
-
- (Object) get_name_translation(lang_code)
Returns the translated name, or the original text if the language is the current.
-
- (Object) set_original_text(text, lang_code)
Set the original text for the tag.
Class Method Details
+ (Object) create_with_data(text, lang_code, purchase_id, gender, hardware_id, hardware_type, ble_major, ble_minor, name, parent_id, latitude, longitude, customer)
Create a new tag with the given data.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/models/tag.rb', line 69 def self.create_with_data(text, lang_code, purchase_id, gender, hardware_id, hardware_type, ble_major, ble_minor, name, parent_id, latitude, longitude, customer) #logger.info 'DBG: create new tag with text: ' + text + ' and lang: ' + lang_code + ' and purchase id: ' + purchase_id + 'and hardware id: ' + hardware_id # check if the language code exists unless TranslationsHelper.is_valid_language lang_code return 400, nil, 'Translation language is not supported', 'TranslationError' end # check if enough purchases are available for the given id purchase_count = Purchase.check_purchases(purchase_id) hardware = nil unless hardware_id.nil? if HardwaresHelper.validate_hardware_type(hardware_type) old_hardware = nil #remove hardware from other tags first if hardware_type == HardwaresHelper::TYPE_BLE unless ble_major.nil? and ble_minor.nil? old_hardware = Hardware.where('identifier = ? AND hardware_type = ? AND major = ? AND minor = ?', hardware_id, hardware_type, ble_major, ble_minor) end else old_hardware = Hardware.where('identifier = ? AND hardware_type = ?', hardware_id, hardware_type) end unless old_hardware.nil? old_hardware.destroy_all end hardware = Hardware.create(identifier: hardware_id, hardware_type: hardware_type) unless hardware.nil? # add major and minor id on bluetooth beacons if hardware_type == HardwaresHelper::TYPE_BLE unless ble_major.nil? and ble_minor.nil? hardware.major = ble_major hardware.minor = ble_minor end end # add hardware location unless latitude.nil? and longitude.nil? location = HardwareLocation.create(latitude: latitude, longitude: longitude) unless location.nil? hardware.hardware_locations << location end end end end end # check if gender value is f or m. If not set default: m unless gender == 'm' or gender == 'f' gender = 'm' end if purchase_count < MAX_TAGS_PER_PURCHASE_ID tag = Tag.create( customer: customer, text_record_attributes: { original_text_attributes: { :text => text, :lang_code => lang_code }, gender: gender, purchase_attributes: { :purchase_id => purchase_id } } ) if tag.nil? or tag.id.nil? return 400, nil, 'Error while creating now tag', 'CreationError' else # if there is already some hardware info save it to the current tag unless hardware.nil? tag.hardwares << hardware tag.save end # if there is a name add it as original name to the current tag unless name.nil? original_text = NameTranslation.create(:text => name, :lang_code => lang_code) tag.original_name = original_text tag.save end if parent_id.nil? tag.parent_id = parent_id end return 200, tag, '', '' end else return 400, nil, 'You are out of purchases', 'OutOfPurchases' end end |
+ (Object) my_top10_chart_data(current_customer)
Returns the top 10 scanned tags.
424 425 426 |
# File 'app/models/tag.rb', line 424 def self.my_top10_chart_data(current_customer) Tag.my_top_10_scanned(current_customer).map { |tag| [tag.display_name, tag.scan_count] } end |
+ (Object) show_by_ble_hardware_id(hardware_id, hardware_type, major, minor)
Show all tags by bluetooth le hardware id.
255 256 257 258 259 260 261 262 263 |
# File 'app/models/tag.rb', line 255 def self.show_by_ble_hardware_id(hardware_id, hardware_type, major, minor) unless hardware_id.nil? and hardware_type.nil? and major.nil? and minor.nil? Tag.joins(:hardwares).find_by('hardwares.identifier' => hardware_id, 'hardwares.hardware_type' => hardware_type, 'hardwares.major' => major, 'hardwares.minor' => minor, active: true) end end |
+ (Object) show_by_hardware_id(hardware_id, hardware_type)
Show all tags by hardware id and hardware type.
240 241 242 243 244 245 246 |
# File 'app/models/tag.rb', line 240 def self.show_by_hardware_id(hardware_id, hardware_type) unless hardware_id.nil? and hardware_type.nil? Tag.joins(:hardwares).find_by('hardwares.identifier' => hardware_id, 'hardwares.hardware_type' => hardware_type, active: true) end end |
Instance Method Details
- (Object) active_date_to_string
Returns the active date as a string.
410 411 412 413 414 415 416 417 418 |
# File 'app/models/tag.rb', line 410 def active_date_to_string unless self.active_start_date.nil? and self.active_stop_date.nil? dateFormat = "%d.%m.%Y" return self.active_start_date.strftime(dateFormat) + " - " + self.active_stop_date.strftime(dateFormat) end return "" end |
- (Object) active_time_to_string
Returns the active time as a string.
399 400 401 402 403 404 405 406 |
# File 'app/models/tag.rb', line 399 def active_time_to_string unless self.active_start_time.nil? and self.active_stop_time.nil? timeFormat = "%H:%M" return self.active_start_time.utc.strftime(timeFormat) + " - " + self.active_stop_time.utc.strftime(timeFormat) end return "" end |
- (Object) add_hardware_id_to_tag(hardware_id, hardware_type, ble_major, ble_minor, latitude, longitude)
Add a hardware to a tag.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'app/models/tag.rb', line 174 def add_hardware_id_to_tag(hardware_id, hardware_type, ble_major, ble_minor, latitude, longitude) if hardware_id.nil? return :status => 400, :json => { :error => { :message => 'Error hardware id is missing', :code => 'UpdatedError' }} else if HardwaresHelper.validate_hardware_type(hardware_type) old_hardware = nil #remove hardware from other tags first if hardware_type == HardwaresHelper::TYPE_BLE unless ble_major.nil? and ble_minor.nil? old_hardware = Hardware.where('identifier = ? AND hardware_type = ? AND major = ? AND minor = ?', hardware_id, hardware_type, ble_major, ble_minor) end else old_hardware = Hardware.where('identifier = ? AND hardware_type = ?', hardware_id, hardware_type) end unless old_hardware.nil? old_hardware.destroy_all end hardware = Hardware.create(identifier: hardware_id, hardware_type: hardware_type) unless hardware.nil? # add major and minor id on bluetooth beacons if hardware_type == HardwaresHelper::TYPE_BLE unless ble_major.nil? and ble_minor.nil? hardware.major = ble_major hardware.minor = ble_minor end end # add hardware location unless latitude.nil? and longitude.nil? location = HardwareLocation.create(latitude: latitude, longitude: longitude) unless location.nil? hardware.hardware_locations << location end end end self.hardwares << hardware self.save else return 400, nil, 'Error while adding hardware', 'AddingHardwareError' end return 200, self, '', '' end end |
- (Object) add_location_to_hardware(hardware_id, latitude, longitude)
Add a location to a hardware.
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/models/tag.rb', line 271 def add_location_to_hardware(hardware_id, latitude, longitude) unless hardware_id.nil? and latitude.nil? and longitude.nil? tag = Tag.joins(:hardwares).find_by('hardwares.identifier' => hardware_id) location = HardwareLocation.create(latitude: latitude, longitude: longitude) tag.hardwares.each do |item| if item.identifier == hardware_id item.hardware_locations << location end end end end |
- (Object) current_date_active
Checks if the current tag is active.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'app/models/tag.rb', line 380 def current_date_active if self.active_start_date.nil? or self.active_stop_date.nil? return false end # get the current time current_time = Time.now if self.active_start_date <= current_time and self.active_stop_date >= current_time return true else return false end return false end |
- (Object) current_time_active
Returns true if the active_time is currently active, else it returns false.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'app/models/tag.rb', line 360 def current_time_active if self.active_start_time.nil? or self.active_stop_time.nil? return false end # get the current time current_time = Time.now.utc # check if current time is in range if self.active_start_time.utc.strftime( "%H%M%S%N" ) <= current_time.utc.strftime( "%H%M%S%N" ) and self.active_stop_time.utc.strftime( "%H%M%S%N" ) >= current_time.utc.strftime( "%H%M%S%N" ) return true else return false end return false end |
- (Object) currently_active
Returns true if the tag is currently active, else it returns false.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'app/models/tag.rb', line 335 def currently_active # check if tag is active if self.active == false return false end # check if time based activation is active if self.active_time unless current_time_active return false end end # check if date based activation is action if self.active_date unless current_date_active return false end end return true end |
- (Object) display_name
Show a display name.
-
if available show original text
-
if available show old name
-
show tag id
45 46 47 48 49 50 51 |
# File 'app/models/tag.rb', line 45 def display_name if original_name.nil? id else original_name.text.nil? ? id: original_name.text end end |
- (Object) get_name_translation(lang_code)
Returns the translated name, or the original text if the language is the current.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'app/models/tag.rb', line 289 def get_name_translation(lang_code) #format language code to a valid bing translate format lang_code_cut = TranslationsHelper.cut_country(lang_code) if lang_code_cut.nil? return nil end # check if this is a valid language code unless TranslationsHelper.is_valid_language(lang_code_cut) return nil end # check if original text is in the new language unless original_name.nil? lang_code_original_cut = TranslationsHelper.cut_country(original_name.lang_code) if original_name.lang_code == lang_code return original_name.text elsif original_name.lang_code == lang_code_cut add_name_translation(original_name.text, lang_code) return original_name.text elsif lang_code_original_cut == lang_code_cut add_name_translation(original_name.text, lang_code) return original_name.text end end # check if translation is already available, if not translate it via bing trans = name_translations.find_by_lang_code(lang_code) if trans.nil? trans_cut = name_translations.find_by_lang_code(lang_code_cut) # check if there is a translation only with the language code, without country code if trans_cut.nil? return translate_name_into_lang_code(lang_code) else add_name_translation(trans_cut.text, lang_code) return trans_cut.text end else trans.text end end |
- (Object) set_original_text(text, lang_code)
Set the original text for the tag.
231 232 233 |
# File 'app/models/tag.rb', line 231 def set_original_text(text, lang_code) text_record.set_original_text(text, lang_code) end |