Last active
December 20, 2015 19:58
-
-
Save jeremiaheb/6186725 to your computer and use it in GitHub Desktop.
Dashboard Tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
That is pretty awesome. You've done a great job! The above is food for thought.
Move lines 8-11 and 14-18 into the model as well
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
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
and etc.
what exactly does that do?
@data_by_divers
easier, like: