Skip to content

Instantly share code, notes, and snippets.

@akasper
Created December 9, 2010 22:04
Show Gist options
  • Save akasper/735411 to your computer and use it in GitHub Desktop.
Save akasper/735411 to your computer and use it in GitHub Desktop.
require 'fastercsv'
class Page < ActiveRecord::Base
CSV_COLUMN_NAMES = ["Name", "Link", "Annual Value", "Potential Value", "Realized", "Likes Count", "Fans", "Comments Count","Fan Post Count", "Brand Post Count", "Brand Post Max of 60", "id", "Mulipler", "Category"]
# Generate a CSV File
# Expects a sort by field nand desc / asc
# Page.genreate_csv(:sidx => blah, :sord => blah, :rows => blah, :categories => blah)
def self.generate_csv(options = {})
options.symbolize_keys!
sidx = options[:sidx] || 'pages.created_at'
sord = (options[:sord] || 'desc')
limit = options[:rows].blank? ? nil : options[:rows] # If not specified, limit is unlimited
categories = options[:categories] || []
categories = categories.collect {|x| x.to_i}
categories = Category.all.collect {|x| x.id} if categories.empty?
pages = Page.find(:all,
:include => [:current_evaluation, :category],
:order => "#{sidx} #{sord}",
:limit => limit,
:conditions => [" (pages.category_id in (?)) or pages.category_id is null ", categories ])
csv_string = FasterCSV.generate do |csv|
csv << CSV_COLUMN_NAMES
counter = 2
pages.each do |r|
recent = r.most_recent_finished_evaluation || Evaluation.new
a = []
a << r.name # Name
a << r.link # Link
a << "=IF(ISERROR(G#{counter}*K#{counter}*M#{counter}*5/1000*12),0,G#{counter}*K#{counter}*M#{counter}*5/1000*12)"
a << "=IF(ISERROR(G#{counter}*60*5/1000*3*12),0,G#{counter}*60*5/1000*3*12)" # Potential Value
a << "=IF(ISERROR(C#{counter}/D#{counter}),0,C#{counter}/D#{counter})" # Realized
a << [recent.likes_count, recent.fan_count,recent.comments_count, recent.fan_post_count, recent.brand_post_count]
a << "=IF(J#{counter}>60,60,IF(J#{counter}=0,1,J#{counter}))"
a << r.id
a << "=IF((((((H#{counter}*4)+F#{counter})/K#{counter})+I#{counter})/G#{counter}*1000)>3,3,IF((((((H#{counter}*4)+F#{counter})/K#{counter})+I#{counter})/G#{counter}*1000)<1,1,(((((H#{counter}*4)+F#{counter})/K#{counter})+I#{counter})/G#{counter}*1000)))"
a << (r.category.blank? ? '' : r.category.name)
counter +=1
csv << a.flatten.dup
end
end
return csv_string
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment