Skip to content

Commit

Permalink
feat: add MindMap, Node and Edges
Browse files Browse the repository at this point in the history
  • Loading branch information
jeferson-sb committed Sep 15, 2024
1 parent 8ee51d6 commit 44f14a0
Show file tree
Hide file tree
Showing 20 changed files with 371 additions and 167 deletions.
17 changes: 17 additions & 0 deletions api/app/controllers/mindmaps_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class MindmapsController < ApplicationController
before_action :authenticate_request, only: :create

def show
@mm = MindMap.find(params[:id])
end

def index
@mmaps = MindMap.all
end

def create
# TBD
end
end
11 changes: 11 additions & 0 deletions api/app/models/edge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Edge < ApplicationRecord
belongs_to :from_node, class_name: 'Node'
belongs_to :to_node, class_name: 'Node'

validates :from_node_id, presence: true
validates :to_node_id, presence: true

scope :between, ->(from, to) { where(from_node_id: from.id, to_node_id: to.id) }
end
20 changes: 20 additions & 0 deletions api/app/models/mind_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class MindMap < ApplicationRecord
belongs_to :category
has_many :nodes, foreign_key: 'graph_id'

scope :for_category, ->(category) { where(category_id: category.id) }

def add_node(node)
node.update!(graph_id: self.id)
end

def remove_node(node)
node.update!(graph_id: nil)
end

def connect_nodes(from, to)
Edge.create(from_node: from, to_node: to)
end
end
14 changes: 14 additions & 0 deletions api/app/models/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class Node < ApplicationRecord
belongs_to :nodeable, polymorphic: true
belongs_to :graph, class_name: 'MindMap', foreign_key: 'graph_id', optional: true

has_many :edges, dependent: :destroy
has_many :edges, foreign_key: 'to_node_id'

validates :nodeable_type, presence: true
validates :nodeable_id, presence: true

scope :related_to_exam, ->(exam) { where(nodeable_type: 'Exam', nodeable_id: exam.id) }
end
1 change: 1 addition & 0 deletions api/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class User < ApplicationRecord

has_many :answer
has_many :exams, through: :answer
has_many :mind_map
has_one :garden
end
8 changes: 8 additions & 0 deletions api/app/views/mindmaps/index.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

json.array! @mmaps do |map|
json.id map.id
json.name map.name
json.owner_id map.user_id
json.node_count map.node_ids.count
end
6 changes: 6 additions & 0 deletions api/app/views/mindmaps/show.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

json.message 'Your mind map'
json.name @mm.name
json.category @mm.category.title
json.nodes @mm.node_ids
1 change: 1 addition & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
post '/journal/surprise_question', to: 'gardens#evaluate_surprise_question'
end
resources :trees, only: %i[index show]
resources :mindmaps
end
end
13 changes: 13 additions & 0 deletions api/db/migrate/20240907184721_create_mind_maps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateMindMaps < ActiveRecord::Migration[7.0]
def change
create_table :mind_maps do |t|
t.string :name
t.references :category, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
14 changes: 14 additions & 0 deletions api/db/migrate/20240907184819_create_nodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CreateNodes < ActiveRecord::Migration[7.0]
def change
create_table :nodes do |t|
t.references :nodeable, polymorphic: true, null: false
t.integer :position

t.timestamps
end

add_index :nodes, %i[nodeable_type nodeable_id], name: 'index_nodes_on_nodeable_type_and_nodeable_id', unique: true
end
end
14 changes: 14 additions & 0 deletions api/db/migrate/20240907184914_create_edges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CreateEdges < ActiveRecord::Migration[7.0]
def change
create_table :edges do |t|
t.references :from_node, null: false, foreign_key: { to_table: :nodes }
t.references :to_node, null: false, foreign_key: { to_table: :nodes }

t.timestamps
end

add_index :edges, %i[from_node_id to_node_id], name: 'index_edges_on_from_node_id_and_to_node_id', unique: true
end
end
5 changes: 5 additions & 0 deletions api/db/migrate/20240915162642_add_graph_id_to_nodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddGraphIdToNodes < ActiveRecord::Migration[7.0]
def change
add_column :nodes, :graph_id, :integer
end
end
Loading

0 comments on commit 44f14a0

Please sign in to comment.