Class: Apidae::Obj
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Apidae::Obj
- Includes:
- PgSearch::Model
- Defined in:
- app/models/apidae/obj.rb
Constant Summary collapse
- ACT =
'ACTIVITE'
- COS =
'COMMERCE_ET_SERVICE'
- DEG =
'DEGUSTATION'
- DOS =
'DOMAINE_SKIABLE'
- EQU =
'EQUIPEMENT'
- FEM =
'FETE_ET_MANIFESTATION'
- HCO =
'HEBERGEMENT_COLLECTIF'
- HLO =
'HEBERGEMENT_LOCATIF'
- HOT =
'HOTELLERIE'
- HPA =
'HOTELLERIE_PLEIN_AIR'
- PCU =
'PATRIMOINE_CULTUREL'
- PNA =
'PATRIMOINE_NATUREL'
- RES =
'RESTAURATION'
- SPA =
'SEJOUR_PACKAGE'
- STR =
'STRUCTURE'
- TER =
'TERRITOIRE'
- TYPES_DATA =
{ ACT => {node: :informationsActivite, subtype: :activiteType}, COS => {node: :informationsCommerceEtService, subtype: :commerceEtServiceType}, DEG => {node: :informationsDegustation, subtype: :statutsExploitant}, DOS => {node: :informationsDomaineSkiable, subtype: :domaineSkiableType}, EQU => {node: :informationsEquipement, subtype: :rubrique}, FEM => {node: :informationsFeteEtManifestation, subtype: :feteEtManifestationType}, HCO => {node: :informationsHebergementCollectif, subtype: :hebergementCollectifType}, HLO => {node: :informationsHebergementLocatif, subtype: :hebergementLocatifType}, HOT => {node: :informationsHotellerie, subtype: :hotellerieType}, HPA => {node: :informationsHotelleriePleinAir, subtype: :hotelleriePleinAirType}, PCU => {node: :informationsPatrimoineCulturel, subtype: :patrimoineCulturelType}, PNA => {node: :informationsPatrimoineNaturel, subtype: :patrimoineNaturelType}, RES => {node: :informationsRestauration, subtype: :restaurationType}, SPA => {node: :informationsSejourPackage, subtype: :sejourPackageType}, STR => {node: :informationsStructure, subtype: :structureType}, TER => {node: :informationsTerritoire, subtype: :territoireType} }
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#obj_version ⇒ Object
Returns the value of attribute obj_version.
-
#obj_versions ⇒ Object
Returns the value of attribute obj_versions.
-
#skip_archiving ⇒ Object
Returns the value of attribute skip_archiving.
Class Method Summary collapse
- .add_object(object_data, locales, versions) ⇒ Object
- .default_scope ⇒ Object
-
.populate_fields(apidae_obj, object_data, locales) ⇒ Object
Note : overrides existing fields (not a merge).
- .update_object(apidae_obj, object_data, locales, versions) ⇒ Object
Instance Method Summary collapse
- #archive_hash_value(prev_value, parent = nil) ⇒ Object
- #archive_updated_fields ⇒ Object
- #contact_text ⇒ Object
- #dig(*keys) ⇒ Object
- #in_locale(l) ⇒ Object
- #in_version(v) ⇒ Object
- #main_picture ⇒ Object
- #ref_search(ref_values, ref_list, label_key = :label) ⇒ Object
- #root_obj ⇒ Object
- #searchable_fields ⇒ Object
- #versions ⇒ Object
- #w_criteria ⇒ Object
- #w_short_desc ⇒ Object
- #w_title ⇒ Object
- #w_town ⇒ Object
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
13 14 15 |
# File 'app/models/apidae/obj.rb', line 13 def locale @locale end |
#obj_version ⇒ Object
Returns the value of attribute obj_version.
14 15 16 |
# File 'app/models/apidae/obj.rb', line 14 def obj_version @obj_version end |
#obj_versions ⇒ Object
Returns the value of attribute obj_versions.
15 16 17 |
# File 'app/models/apidae/obj.rb', line 15 def obj_versions @obj_versions end |
#skip_archiving ⇒ Object
Returns the value of attribute skip_archiving.
16 17 18 |
# File 'app/models/apidae/obj.rb', line 16 def skip_archiving @skip_archiving end |
Class Method Details
.add_object(object_data, locales, versions) ⇒ Object
242 243 244 245 |
# File 'app/models/apidae/obj.rb', line 242 def self.add_object(object_data, locales, versions) apidae_obj = Obj.new(apidae_id: object_data[:id], version: DEFAULT_VERSION) update_object(apidae_obj, object_data, locales, versions) end |
.default_scope ⇒ Object
118 119 120 |
# File 'app/models/apidae/obj.rb', line 118 def self.default_scope where(root_obj_id: nil) end |
.populate_fields(apidae_obj, object_data, locales) ⇒ Object
Note : overrides existing fields (not a merge)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'app/models/apidae/obj.rb', line 268 def self.populate_fields(apidae_obj, object_data, locales) type_fields = TYPES_DATA[object_data[:type]] apidae_obj.last_update = DateTime.parse(object_data[:gestion][:dateModification]) unless object_data[:gestion].blank? apidae_obj.owner_data = ApidaeDataParser.parse_owner_data(object_data[:gestion][:membreProprietaire], object_data[:enquete]) unless (object_data[:gestion].blank? && object_data[:enquete].blank?) apidae_obj.apidae_type = object_data[:type] apidae_obj.apidae_subtype = ApidaeDataParser.node_id(object_data[type_fields[:node]], type_fields[:subtype]) apidae_obj.title_data = ApidaeDataParser.parse_title(object_data, *locales) apidae_obj.description_data = ApidaeDataParser.parse_desc_data(object_data[:presentation], object_data[:donneesPrivees], object_data[:prestations], *locales) apidae_obj.contact_data = ApidaeDataParser.parse_contact_data(object_data[:informations], object_data[:contacts]) apidae_obj.location_data = ApidaeDataParser.parse_location_data(object_data[:localisation], object_data[type_fields[:node]], object_data[:territoires]) apidae_obj.town = ApidaeDataParser.parse_town(object_data[:localisation]) apidae_obj.openings_data = ApidaeDataParser.parse_openings(object_data[:ouverture], object_data[:datesOuverture], *locales) apidae_obj.rates_data = ApidaeDataParser.parse_rates(object_data[:descriptionTarif], *locales) apidae_obj.booking_data = ApidaeDataParser.parse_booking(object_data[:reservation], object_data[:visites], *locales) apidae_obj.type_data = ApidaeDataParser.parse_type_data(apidae_obj, object_data[type_fields[:node]], object_data[:prestations], object_data[:tourismeAffaires], *locales) apidae_obj.pictures_data = ApidaeDataParser.parse_pictures_data(object_data[:illustrations], *locales) apidae_obj. = ApidaeDataParser.(object_data[:multimedias], *locales) apidae_obj.entity_data = ApidaeDataParser.parse_entity_fields(object_data[:informations], object_data[type_fields[:node]], object_data[:informationsPrestataireActivites]) apidae_obj.service_data = ApidaeDataParser.parse_service_data(object_data[:prestations], object_data[type_fields[:node]]) apidae_obj. = ApidaeDataParser.(object_data[:presentation], object_data[:criteresInternes], object_data[:liens]) apidae_obj. = object_data[:metadonnees] end |
.update_object(apidae_obj, object_data, locales, versions) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'app/models/apidae/obj.rb', line 247 def self.update_object(apidae_obj, object_data, locales, versions) populate_fields(apidae_obj, object_data, locales) apidae_obj.save! unless versions.blank? || object_data[:aspects].blank? versions.select {|v| v != STANDARD_VERSION }.each do |version| version_data = object_data[:aspects].find {|a| a[:aspect] == version} if version_data version_data[:type] = apidae_obj.apidae_type version_obj = apidae_obj.in_version(version) || Obj.new(apidae_id: apidae_obj.apidae_id, root_obj_id: apidae_obj.id, version: version) version_obj.versioned_fields = ApidaeDataParser.parse_versioned_fields(version_data) populate_fields(version_obj, version_data, locales) version_obj.save! end end end apidae_obj end |
Instance Method Details
#archive_hash_value(prev_value, parent = nil) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'app/models/apidae/obj.rb', line 203 def archive_hash_value(prev_value, parent = nil) ts = Time.current.to_i ignored_sub_attributes = ['opening_days'] prev_value.each_pair do |sub_attr, prev_sub_val| if Apidae::ALL_LOCALES.include?(sub_attr) && parent new_val = in_locale(sub_attr).send(parent) elsif /^\d+$/.match?(sub_attr) && parent # Note : email / website / telephone etc... case - To be improved using changes method and a proper Hash/Array diff new_val = in_locale(Apidae::DEFAULT_LOCALE)[parent] else new_val = parent ? in_locale(Apidae::DEFAULT_LOCALE).dig(parent, sub_attr) : (respond_to?(sub_attr) ? in_locale(Apidae::DEFAULT_LOCALE).send(sub_attr) : in_locale(Apidae::DEFAULT_LOCALE)[sub_attr]) end unless ignored_sub_attributes.include?(sub_attr) || new_val == prev_sub_val if parent self.prev_data[parent][sub_attr] ||= {} else self.prev_data[sub_attr] ||= {} end if prev_sub_val.is_a?(Hash) && parent.nil? archive_hash_value(prev_sub_val, sub_attr) elsif prev_sub_val.is_a?(Array) && prev_sub_val.all? {|v| v.is_a?(Hash) && (v['id'] || v['identifiant'])} archived_val = {'prev' => prev_sub_val.map {|v| v['id'] || v['identifiant']}, 'ts' => ts} if parent self.prev_data[parent][sub_attr] = archived_val else self.prev_data[sub_attr] = archived_val end else archived_val = {'prev' => prev_sub_val, 'ts' => ts} if parent self.prev_data[parent][sub_attr] = archived_val else self.prev_data[sub_attr] = archived_val end end end end end |
#archive_updated_fields ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/models/apidae/obj.rb', line 189 def archive_updated_fields self.prev_data ||= {} ignored_root_attributes = ['prev_data', 'updated_at'] changed_attributes.each_pair do |attr, prev_value| unless ignored_root_attributes.include?(attr) || skip_archiving if prev_value.is_a?(Hash) archive_hash_value(prev_value) else self.prev_data[attr] = {'prev' => prev_value, 'ts' => Time.current.to_i} end end end end |
#contact_text ⇒ Object
293 294 295 296 297 298 299 |
# File 'app/models/apidae/obj.rb', line 293 def contact_text entries = [] JSON.parse(contact_data).each_pair do |k, v| entries << "#{k}: #{v}" end entries.join("\n") end |
#dig(*keys) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/models/apidae/obj.rb', line 177 def dig(*keys) root_key, *nested_keys = keys root_val = self.send(root_key) if root_val.blank? nested_keys.blank? ? root_val : nil elsif root_val.respond_to?(:dig) root_val.dig(*nested_keys) else raise ArgumentError.new('Cannot call dig with these args: ' + keys.to_s) end end |
#in_locale(l) ⇒ Object
142 143 144 145 |
# File 'app/models/apidae/obj.rb', line 142 def in_locale(l) @locale = l self end |
#in_version(v) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/apidae/obj.rb', line 130 def in_version(v) if v == DEFAULT_VERSION && root_obj_id.nil? @obj_version = DEFAULT_VERSION self else if @obj_versions[v].nil? @obj_versions[v] = versions.where(version: v).first end @obj_versions[v] end end |
#main_picture ⇒ Object
301 302 303 |
# File 'app/models/apidae/obj.rb', line 301 def main_picture pictures.blank? ? "/#{Rails.application.config.apidae_pictures_path}/default/logo.png" : pictures[0]["url"].gsub('http:', 'https:') end |
#ref_search(ref_values, ref_list, label_key = :label) ⇒ Object
168 169 170 |
# File 'app/models/apidae/obj.rb', line 168 def ref_search(ref_values, ref_list, label_key = :label) (ref_values || []).map {|r| ref_list.dig(r, label_key)} end |
#root_obj ⇒ Object
122 123 124 |
# File 'app/models/apidae/obj.rb', line 122 def root_obj Obj.unscoped.where(id: root_obj_id).first end |
#searchable_fields ⇒ Object
172 173 174 175 |
# File 'app/models/apidae/obj.rb', line 172 def searchable_fields {s_title: title, s_desc: short_desc, s_external_id: apidae_id, s_type: apidae_type, s_picture: main_picture, s_selections: selection_ids} end |
#versions ⇒ Object
126 127 128 |
# File 'app/models/apidae/obj.rb', line 126 def versions Obj.unscoped.where(root_obj_id: id) end |
#w_criteria ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'app/models/apidae/obj.rb', line 159 def w_criteria ([SUBTYPES.dig(apidae_subtype&.to_i, :label)] + ref_search(payment_methods, PAYMENT) + ref_search(categories, CATEGORIES) + ref_search(themes, THEMES) + ref_search(labels, LABELS) + ref_search(chains, CHAINS) + ref_search(classification, CLASSIFICATION) + ref_search(services, SERVICES) + ref_search(equipments, EQUIPMENTS) + ref_search(comfort, COMFORT) + ref_search(activities, ACTIVITIES) + ref_search(challenged, CHALLENGED) + ref_search(audience, AUDIENCE) + ref_search(promo, PROMO) + ref_search(environments, ENVIRONMENTS) + ref_search(products, PRODUCTS) + ref_search(internal, INTERNALS)).compact.uniq end |
#w_short_desc ⇒ Object
155 156 157 |
# File 'app/models/apidae/obj.rb', line 155 def w_short_desc "#{short_desc}§§§" end |
#w_title ⇒ Object
147 148 149 |
# File 'app/models/apidae/obj.rb', line 147 def w_title title end |
#w_town ⇒ Object
151 152 153 |
# File 'app/models/apidae/obj.rb', line 151 def w_town "#{town&.name}§§§" end |