lib/sibu_apidae.rb in sibu_apidae-0.3.1 vs lib/sibu_apidae.rb in sibu_apidae-0.3.2

- old
+ new

@@ -31,42 +31,50 @@ class Helper APIDAE_OBJ_SOURCE = 'Fiche Apidae' - def self.update_pages(site_id) + def self.update_pages(site_id, obj_apidae_id = nil) 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| + apidae_pages = Sibu::Page.where(site_id: site_id).where("metadata LIKE '%\"source\":\"#{APIDAE_OBJ_SOURCE}\"%'") + .select(:id, :site_id, :metadata).to_a + apidae_pages = apidae_pages.select {|p| p.external_id == obj_apidae_id} unless obj_apidae_id.blank? + apidae_pages.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 + Rails.logger.info("Completed sibu pages update/delete for site #{site_id}") - Apidae::Obj.all.each do |obj| + apidae_pages = Sibu::Page.where(site_id: site_id).where("metadata LIKE '%\"source\":\"#{APIDAE_OBJ_SOURCE}\"%'") + .select(:id, :site_id, :metadata).to_a + apidae_pages = apidae_pages.select {|p| p.external_id == obj_apidae_id} unless obj_apidae_id.blank? + + objs = obj_apidae_id.blank? ? Apidae::Obj.all : Apidae::Obj.where(apidae_id: obj_apidae_id) + objs.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} + obj_page = apidae_pages.find {|p| 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, site_id) p.save! end end end + Rails.logger.info("Completed sibu pages creation for site #{site_id}") 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' + "SibuApidae pages update complete for site #{site_id}" 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