Class: SibuApidae::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/sibu_apidae.rb

Constant Summary collapse

APIDAE_OBJ_SOURCE =
'Fiche Apidae'

Class Method Summary collapse

Class Method Details

.is_valid?(conf) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/sibu_apidae.rb', line 71

def self.is_valid?(conf)
  conf && conf.key?(:obj_path) && conf.key?(:obj_sections) && conf.key?(:supported_types) && conf.key?(:page_external_id)
end

.update_pages(site_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sibu_apidae.rb', line 36

def self.update_pages(site_id)
  conf = Rails.application.config.sibu_apidae
  if is_valid?(conf)
    Sibu::Page.where(site_id: site_id).select(:id, :site_id, :name, :path, :metadata).to_a
        .select {|p| p.source == APIDAE_OBJ_SOURCE}.each do |p|
      obj = Apidae::Obj.find_by_apidae_id(p.external_id)
      if obj
        p.update(name: obj.title, title: obj.title, description: obj.short_desc,
                 path: conf[:obj_path].call(obj))
      else
        Rails.logger.info("Deleting page #{p.id} after object deletion (#{p.external_id})")
        p.destroy
      end
    end

    Apidae::Obj.all.each do |obj|
      if conf[:supported_types].include?(obj.apidae_type)
        obj_page = Sibu::Page.where(site_id: site_id).select(:id, :metadata).to_a
            .find {|p| p.source == APIDAE_OBJ_SOURCE && p.external_id == obj.apidae_id}
        unless obj_page
          p = Sibu::Page.new(name: obj.title, site_id: site_id, path: conf[:obj_path].call(obj), title: obj.title,
                             description: obj.short_desc, source: APIDAE_OBJ_SOURCE,
                             external_id: conf[:page_external_id].call(obj))
          p.sections = conf[:obj_sections].call(obj)
          p.save!
        end
      end
    end
  else
    Rails.logger.error "Please provide a valid configuration for the sibu_apidae gem " +
                           "(:page_external_id, :obj_path, :obj_sections and :supported_types are required)"
  end
  'SibuApidae pages update complete'
end