-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
398 additions
and
35 deletions.
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
app/controllers/crudify/api/v1/visualisations_controller.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
module CRUDify | ||
module Api | ||
module V1 | ||
class VisualisationsController < ApplicationController | ||
|
||
before_action :authenticate_token | ||
before_action :set_model_metadata | ||
|
||
def catch_all_collection_action | ||
action_name = params[:action_name].to_sym | ||
|
||
# Fetch model configuration | ||
config = CRUDify.configuration.crudify_visuals[@model_name] | ||
|
||
unless config | ||
render json: { error: "Visualisation for the Model #{@model_name} is not configured in CRUDify" }, status: :not_found | ||
return | ||
end | ||
|
||
# Check if the action exists in custom collection actions | ||
action = config.collection_end_points.find { |a| a[:name] == action_name } | ||
|
||
unless action | ||
render json: { error: "End Point #{action_name} is not defined for #{@model_name} Visualisation" }, status: :unprocessable_entity | ||
return | ||
end | ||
|
||
# Dynamically execute the action logic | ||
instance_exec(&action[:logic]) | ||
end | ||
|
||
def catch_all_member_action | ||
action_name = params[:action_name].to_sym | ||
|
||
# Fetch model configuration | ||
config = CRUDify.configuration.crudify_visuals[@model_name] | ||
|
||
unless config | ||
render json: { error: "Visualisation for the Model #{@model_name} is not configured in CRUDify" }, status: :not_found | ||
return | ||
end | ||
|
||
# Check if the action exists in custom member actions | ||
action = config.entity_end_points.find { |a| a[:action_name] == action_name } | ||
|
||
unless action | ||
render json: { error: "End Point #{action_name} is not defined for #{@model_name} Visualisation" }, status: :unprocessable_entity | ||
return | ||
end | ||
|
||
# Dynamically execute the action logic | ||
instance_exec(&action[:logic]) | ||
end | ||
|
||
private | ||
|
||
def set_model_metadata | ||
@model_name = params[:model_name] # Extract model_name from URL | ||
unless @model_name | ||
render json: { error: "Crudify not configured for the model: #{params[:model_name]}" }, status: :unprocessable_entity | ||
return | ||
end | ||
|
||
@model_config = CRUDify.configuration.crudify_models[@model_name] | ||
unless @model_config | ||
render json: { error: "Crudify not configured for the model: #{params[:model_name]}" }, status: :unprocessable_entity | ||
return | ||
end | ||
|
||
@model_class = @model_name.classify.constantize | ||
end | ||
|
||
def resource_params | ||
params.require(@model_class.name.underscore.to_sym).permit! | ||
end | ||
|
||
def scoped_collection | ||
# Get list columns and associations to include | ||
@list_columns = @model_config.get_list_columns | ||
@association_includes = @list_columns.map { |col| col[:options][:include] }.compact | ||
|
||
unless @list_columns | ||
render json: { error: "Crudify not configured for the model: #{@model_class.name}" }, status: :unprocessable_entity | ||
return | ||
end | ||
|
||
# Fetch pagination parameters | ||
@page = params[:page].to_i > 0 ? params[:page].to_i : 1 | ||
@per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : @model_config.get_records_per_page.first | ||
|
||
# Calculate offset | ||
@offset = (@page - 1) * @per_page | ||
|
||
# Fetch records with associations and apply pagination | ||
relation = @model_class.all | ||
relation = relation.includes(*@association_includes) if @association_includes.any? | ||
relation = relation.offset(@offset).limit(@per_page) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
module CRUDify | ||
module Configuration | ||
class MetricConfig | ||
attr_reader :name | ||
|
||
def initialize(name, options = {}) | ||
@name = name | ||
@visualisation = options[:visualisation] | ||
@title = options[:title] || name.to_s.titleize | ||
@caption = options[:caption] || "Caption for #{name}" | ||
@data = options[:data] | ||
@highlights = options[:highlights] || [] | ||
end | ||
|
||
# Getter and setter for visualisation | ||
def visualisation(value = nil) | ||
return @visualisation if value.nil? | ||
@visualisation = value | ||
end | ||
|
||
# Getter and setter for title | ||
def title(value = nil) | ||
return @title if value.nil? | ||
@title = value | ||
end | ||
|
||
# Getter and setter for caption | ||
def caption(value = nil) | ||
return @caption if value.nil? | ||
@caption = value | ||
end | ||
|
||
# Getter and setter for data | ||
def data(value = nil) | ||
return @data if value.nil? | ||
@data = value | ||
end | ||
|
||
|
||
# Add highlight configuration for a metric | ||
def highlight(title:, caption: nil, value:) | ||
@highlights << { title: title, caption: caption, value: value } | ||
end | ||
|
||
def highlights | ||
@highlights | ||
end | ||
|
||
def to_h | ||
{ | ||
name: name, | ||
visualisation: visualisation, | ||
title: title, | ||
caption: caption, | ||
data: data, | ||
highlights: highlights | ||
} | ||
end | ||
end | ||
end | ||
end | ||
|
Oops, something went wrong.