55502f40dc8b7c769880b10874abc9d0

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.

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 !

D85d44a0eca045f40e5a31449277c26c

Ben Marini

October 2, 2009, October 02, 2009 14:42, permalink

No rating. Login to rate!

A little more code would help. All the classes involved and their relationships. I would consider caching the tallies.

Your refactoring





Format Copy from initial code

or Cancel