Skip to content

Instantly share code, notes, and snippets.

@jeremiaheb
Last active December 20, 2015 19:58
Show Gist options
  • Save jeremiaheb/6186725 to your computer and use it in GitHub Desktop.
Save jeremiaheb/6186725 to your computer and use it in GitHub Desktop.
Dashboard Tables
class Dashboard
def diver_list(current_diver)
if current_diver.role == "admin"
@boat_logs = BoatLog.all
@boatlog_divers = RepLog.group(:diver_id).count(:id)
@sample_divers = DiverSample.primary.group(:diver_id).count(:id)
@lpi_divers = BenthicCover.group(:diver_id).count(:id)
@demo_divers = CoralDemographic.group(:diver_id).count(:id)
hash_merge(@boatlog_divers, @sample_divers, @lpi_divers, @demo_divers).keys
else
@boat_logs = BoatLog.where( "boatlog_manager_id=?", current_diver.boatlog_manager_id )
@boatlog_divers = RepLog.joins(station_log: :boat_log).where("boat_logs.boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
@sample_divers = DiverSample.primary.joins(:sample).where("samples.boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
@lpi_divers = BenthicCover.where("boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
@demo_divers = CoralDemographic.where("boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
hash_merge(@boatlog_divers, @sample_divers, @lpi_divers, @demo_divers).keys
end
end
def hash_merge *hashes
hashes.inject :merge
end
def check_val (v)
v.nil? ? 0 : v
end
def divers(current_diver)
organized_hash = {}
diver_list(current_diver).each do |diver|
organized_hash[diver] = { "boat" => check_val(@boatlog_divers[diver]),
"sample" => check_val(@sample_divers[diver]),
"lpi" => check_val(@lpi_divers[diver]),
"demo" => check_val(@demo_divers[diver]) }
end
return organized_hash
end
end
@tdouce
Copy link

tdouce commented Aug 8, 2013

  1. Lines 21-39 should really be in app/models/dashboard.rb
  2. In app/models/diver.rb or app/models/user.rb I would make methods to determine the role for the current_diver, like:
def admin?
  current_diver.role == 'admin'
end

and etc.

  1. I'm not familiar with what this does:
def hash_merge *hashes
      hashes.inject :merge
 end

what exactly does that do?

  1. what is this used for?
 format.json { render json: @dashboard }
  1. Maybe return an array instead of a hash, b/c it would make iterating over the @data_by_divers easier, like:
def organize_diver_hashes

      divers = diver_list.each do |diver|
        organized_hash[diver] = { "boat" => check_val(@boatlog_divers[diver]), "sample" => check_val(@sample_divers[diver]), "lpi" => check_val(@lpi_divers[diver]), "demo" => check_val(@demo_divers[diver]) }
      end
      divers
    end

@tdouce
Copy link

tdouce commented Aug 8, 2013

That is pretty awesome. You've done a great job! The above is food for thought.

@tdouce
Copy link

tdouce commented Aug 9, 2013

Move lines 8-11 and 14-18 into the model as well

@tdouce
Copy link

tdouce commented Aug 9, 2013

You could do something like this:

def self.diver_list(current_diver)
  if current_diver.admin?
    boat_logs      = BoatLog.all
    boatlog_divers = RepLog.group(:diver_id).count(:id)
    sample_divers  = DiverSample.primary.group(:diver_id).count(:id)
    lpi_divers     = BenthicCover.group(:diver_id).count(:id)
    demo_divers    = CoralDemographic.group(:diver_id).count(:id)
    self.hash_merge(boatlog_divers, sample_divers, lpi_divers, demo_divers).keys
  else
    boat_logs      = BoatLog.where( "boatlog_manager_id=?", current_diver.boatlog_manager_id )
    boatlog_divers = RepLog.joins(station_log: :boat_log).where("boat_logs.boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
    sample_divers  = DiverSample.primary.joins(:sample).where("samples.boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
    lpi_divers     = BenthicCover.where("boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
    demo_divers    = CoralDemographic.where("boatlog_manager_id = ?", current_diver.boatlog_manager_id).group(:diver_id).count(:id)
    self.hash_merge(boatlog_divers, sample_divers, lpi_divers, demo_divers).keys
  end

@tdouce
Copy link

tdouce commented Aug 9, 2013

controller

 def show
   Dashboard.organize_diver_hashes(current_diver)
  end

model

def self.organize_diver_hashes(current_diver)
      organized_hash = {}
      diver_list(current_diver).each do |diver|
        organized_hash[diver] = { "boat" => check_val(@boatlog_divers[diver]), "sample" => check_val(@sample_divers[diver]), "lpi" => check_val(@lpi_divers[diver]), "demo" => check_val(@demo_divers[diver]) }
      end
      return organized_hash
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment