class AdvertiserFactory

Constants

FIELDS

Public Class Methods

update_fields(advertiser, advertiser_xml) click to toggle source
# File lib/helpers/advertiser_factory.rb, line 17
def self.update_fields(advertiser, advertiser_xml)
  #puts advertiser_xml.xpath(FIELDS[0][:xml]).to_s
  
  #Iterate over every field and attempt to insert
  FIELDS.each_with_index do |field, index|
    
    xml_field = field[:xml]
    db_field = field[:db]
    
    #Magic baby!
    unless db_field == 'nice_name'
      advertiser[db_field] = advertiser_xml.xpath(xml_field).to_s
    else
      advertiser[db_field] = (advertiser_xml.xpath(xml_field).to_s).gsub(/[^A-Za-z0-9]+/, '-').strip.gsub(/\ +/, '-').downcase
    end
    
  end

  #Update categories & Relationships
  advertiser_xml.xpath('./primary-category').children.each_with_index do |children, index|
    category_name = children.child.to_s
    category_nice_name = category_name.gsub(/[^A-Za-z0-9]+/, '-').strip.gsub(/\ +/, '-').downcase
    
    
    category = Category.find_or_create_by_nice_name(category_nice_name, :name => category_name)
    AdvertiserCategoryLink.find_or_create_by_advertiser_id_and_category_id(
      :advertiser_id => advertiser.id,
      :category_id => category.id)
  end
  
  advertiser.save
end