Class: Apidae::Obj

Inherits:
ActiveRecord::Base
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



8
9
10
# File 'app/models/apidae/obj.rb', line 8

def locale
  @locale
end

#obj_versionObject

Returns the value of attribute obj_version.



9
10
11
# File 'app/models/apidae/obj.rb', line 9

def obj_version
  @obj_version
end

#obj_versionsObject

Returns the value of attribute obj_versions.



10
11
12
# File 'app/models/apidae/obj.rb', line 10

def obj_versions
  @obj_versions
end

#skip_archivingObject

Returns the value of attribute skip_archiving.



11
12
13
# File 'app/models/apidae/obj.rb', line 11

def skip_archiving
  @skip_archiving
end

Class Method Details

.add_object(object_data, locales, versions) ⇒ Object



206
207
208
209
# File 'app/models/apidae/obj.rb', line 206

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_scopeObject



112
113
114
# File 'app/models/apidae/obj.rb', line 112

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)



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/models/apidae/obj.rb', line 232

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.attachments_data = ApidaeDataParser.parse_attachments_data(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.tags_data = ApidaeDataParser.parse_tags_data(object_data[:presentation], object_data[:criteresInternes], object_data[:liens])
  apidae_obj. = object_data[:metadonnees]
end

.update_object(apidae_obj, object_data, locales, versions) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/models/apidae/obj.rb', line 211

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



167
168
169
170
171
172
173
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
# File 'app/models/apidae/obj.rb', line 167

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_fieldsObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/apidae/obj.rb', line 153

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_textObject



257
258
259
260
261
262
263
# File 'app/models/apidae/obj.rb', line 257

def contact_text
  entries = []
  JSON.parse(contact_data).each_pair do |k, v|
    entries << "#{k}: #{v}"
  end
  entries.join("\n")
end

#dig(*keys) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/apidae/obj.rb', line 141

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



136
137
138
139
# File 'app/models/apidae/obj.rb', line 136

def in_locale(l)
  @locale = l
  self
end

#in_version(v) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/apidae/obj.rb', line 124

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_pictureObject



265
266
267
# File 'app/models/apidae/obj.rb', line 265

def main_picture
  pictures.blank? ? "/#{Rails.application.config.apidae_pictures_path}/default/logo.png" : pictures[0]["url"].gsub('http:', 'https:')
end

#root_objObject



116
117
118
# File 'app/models/apidae/obj.rb', line 116

def root_obj
  Obj.unscoped.where(id: root_obj_id).first
end

#versionsObject



120
121
122
# File 'app/models/apidae/obj.rb', line 120

def versions
  Obj.unscoped.where(root_obj_id: id)
end