lib/sibu_apidae.rb in sibu_apidae-0.2.0 vs lib/sibu_apidae.rb in sibu_apidae-0.3.0

- old
+ new

@@ -28,26 +28,48 @@ Apidae::Obj.find_by_apidae_id(obj_id) || Apidae::Obj.first end end class Helper - def self.import_pages(site_id) + + APIDAE_OBJ_SOURCE = 'Fiche Apidae' + + def self.update_pages(site_id) conf = Rails.application.config.sibu_apidae - if conf && conf.key?(:obj_path) && conf.key?(:obj_sections) && conf.key?(:supported_types) + 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_path = Rails.application.config.sibu_apidae[:obj_path].call(obj) - obj_page = Sibu::Page.where(site_id: site_id, path: obj_path).first + 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: obj_path, title: obj.title, - description: obj.short_desc) - p.sections = Rails.application.config.sibu_apidae[:obj_sections].call(obj) + 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 - puts "Please provide a valid configuration for the sibu_apidae gem (obj_path and obj_sections procs are required)" + 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 + + def self.is_valid?(conf) + conf && conf.key?(:obj_path) && conf.key?(:obj_sections) && conf.key?(:supported_types) && conf.key?(:page_external_id) end end end