51 lines
1.4 KiB
Ruby
51 lines
1.4 KiB
Ruby
require 'yaml'
|
|
|
|
class DashboardController < ApplicationController
|
|
before_action :load_manifest
|
|
|
|
def index
|
|
# Renders the main page
|
|
respond_to do |format|
|
|
format.html
|
|
format.json { render json: @manifest }
|
|
end
|
|
end
|
|
|
|
# GET /dashboard/options?table=Joint&filter_col=joint_id&value=1
|
|
def options
|
|
table = params[:table].constantize
|
|
if params[:filter_col].present?
|
|
options = table.where(params[:filter_col] => params[:value]).pluck(params[:pluck_column])
|
|
else
|
|
puts table
|
|
options = table.distinct.pluck(params[:pluck_column])
|
|
end
|
|
render json: options
|
|
end
|
|
|
|
# GET /dashboard/data?joint=Acromioclavicular&sub_joint=X
|
|
def data
|
|
results = {}
|
|
|
|
# Use the data_sources from YAML to aggregate data
|
|
@manifest['data_sources'].each do |source|
|
|
table = source['table'].constantize
|
|
# Find the filter value from the params that matches the filter_column
|
|
# We assume the filter_column name in DB matches the filter ID in YAML
|
|
filter_val = params[source['filter_column']] || params['joint'] # Fallback to joint
|
|
|
|
record = table.find_by(source['filter_column'] => filter_val)
|
|
results[source['key']] = record ? record.attributes : {}
|
|
end
|
|
|
|
#debugger
|
|
render json: results
|
|
end
|
|
|
|
private
|
|
|
|
def load_manifest
|
|
@manifest = YAML.load_file(Rails.root.join('config', 'dashboard_manifest.yml'))
|
|
end
|
|
end
|