Partly working
This commit is contained in:
@@ -1,25 +1,50 @@
|
||||
class DashboardController < ApplicationController
|
||||
def index
|
||||
joint_column = 'joint'
|
||||
@available_joints = Joint.distinct.pluck(joint_column)
|
||||
@available_data
|
||||
@records = filter_records
|
||||
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: @records }
|
||||
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 filter_records
|
||||
# Using .where(joint: params[:joint]) handles both present and nil values gracefully
|
||||
if params[:joint].present?
|
||||
Joint.where(joint: params[:joint])
|
||||
else
|
||||
# Return empty array if no joint selected to avoid loading thousands of records
|
||||
[]
|
||||
end
|
||||
def load_manifest
|
||||
@manifest = YAML.load_file(Rails.root.join('config', 'dashboard_manifest.yml'))
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user