Skip to content

Instantly share code, notes, and snippets.

@knowuh
Created September 13, 2011 21:44
Show Gist options
  • Save knowuh/1215256 to your computer and use it in GitHub Desktop.
Save knowuh/1215256 to your computer and use it in GitHub Desktop.
Report on embeddable usage
EmbeddableKlasses = [
Embeddable::ImageQuestion,
Embeddable::SoundGrapher
]
class Investigation < ActiveRecord::Base
EmbeddableKlasses.each do |klass|
eval "has_many :#{klass.name[/::(\w+)$/, 1].underscore.pluralize}, :class_name => '#{klass.name}',
:finder_sql => 'SELECT #{klass.table_name}.* FROM #{klass.table_name}
INNER JOIN page_elements ON #{klass.table_name}.id = page_elements.embeddable_id AND page_elements.embeddable_type = \"#{klass.to_s}\"
INNER JOIN pages ON page_elements.page_id = pages.id
INNER JOIN sections ON pages.section_id = sections.id
INNER JOIN activities ON sections.activity_id = activities.id
WHERE activities.investigation_id = \#\{id\}'"
end
end
@attribute_names = %w(
Xhtml
OpenResponse
MultipleChoice
DataTable
DrawingTool
DataCollector
LabBookSnapshot
InnerPage
ImageQuestion
MwModelerPage
NLogoModel
RawOtml
RangeQuestion
SoundGrapher
)
@totals = {}
def report(inv)
values = @attribute_names.sort.map do |attribute_name|
attribute = attribute_name.strip.underscore.pluralize
values = inv.send attribute
size = values.size
@totals[attribute] ||= 0
@totals[attribute] = @totals[attribute] + size
"#{size}"
end
author = inv.user.name || 'unknown'
created = inv.created_at || 'unknown'
published = inv.published? ? "public" : "private"
offerings = inv.offerings.size
"#{inv.id} - #{inv.name}, #{published}, #{offerings}, #{author}, #{created}, #{values.join(", ")}\n"
end
File.open("/tmp/investigation_report.csv","w") do |file|
header = "Investigation (id - name), published, offerings, author, created at, #{@attribute_names.sort.join(", ")}\n"
puts header
file.write(header)
Investigation.published.each do |inv|
data = report(inv)
file.write(data)
puts(data)
end
sorted_keys = @totals.keys.sort
total_line = "Totals:, (published), (offerings), (author), (created), #{sorted_keys.map { |k| "#{@totals[k]}" }.join(", ")}\n"
puts total_line
file.write(total_line)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment