def get_roles()
roles = []
store = {}
ProjectContributor.find(:all, :conditions => ["contributor = ?", self.contributor]).each do |list|
proj = list.get_project
if(!proj.nil? && %[Bible Book Audio].include?(proj.data_type) && proj.print_status != "OP")
store[list.role] = (store.has_key?(list.role)) ? store[list.role] + 1 : 1
end
end
store.keys.sort.each do |k,v|
break unless("A01 A12 A23 A32 B01 B09".include?(k.to_s) )
roles << "#{select_lookup(k.to_s, CONTRIBUTOR_CHOICES)} [#{store[k]}]"
end
roles.join(", ").to_s
end
Refactorings
No refactoring yet !
Ben Marini
October 2, 2009, October 02, 2009 14:42, permalink
A little more code would help. All the classes involved and their relationships. I would consider caching the tallies.
I have a Contributor and ProjectContributor model in my Rails Project. As these are legacy databases, I need to process them. There has to be a more efficient way to get this data.
My approach is looping through the result set and sum the roles based on the hash key. After sorting the hash's key I transform the role value to the text version to create a comma separated list like the following:
By (author) [8], Foreword by [2], Contributions by [6]
FYI: list.get_project looks for the project from a non-primary key.