-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FlowchartNode model and controller (#24)
* [WIP]: show, update, and swap functions * [WIP]: basic delete functionality and return json objects * add soft delete in node, run rubocop * flowchart node model tests * Finish testing for show and update * Updates to swap/delete and unit tests * linting * [WIP] FlowchartNode create controller method, some unit tests * Tests for flowchartnode create controller method * linting * Fix duplicate primary key issue in tests * Add comment around primary key sequence correction * remove as_json from render in flowchartnode controller * JSON parse body in flowchart node update * Add validation and testing for flowchart node create * Remove rescue blocks from flowchart node controller * remove request body access * add delete method on flowchart node model * Add swap method to flowchart model * lint
- Loading branch information
1 parent
43afebb
commit 4f5d996
Showing
5 changed files
with
610 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
class FlowchartNodeController < ApplicationController | ||
def create | ||
prev_id = params[:prev_id] | ||
is_child = params[:is_child] | ||
|
||
prev_node = FlowchartNode.find(prev_id) | ||
|
||
new_node = FlowchartNode.new(params[:node].permit(:text, :header, :button_text, :next_question)) | ||
new_node.is_root = false | ||
new_node.flowchart_id = prev_node.flowchart_id | ||
|
||
ActiveRecord::Base.transaction do | ||
new_node.save! | ||
if is_child == 'true' | ||
new_node.child_id = prev_node.child_id | ||
prev_node.child_id = new_node.id | ||
else | ||
new_node.sibling_id = prev_node.sibling_id | ||
prev_node.sibling_id = new_node.id | ||
end | ||
new_node.save! | ||
prev_node.save! | ||
end | ||
render json: new_node | ||
end | ||
|
||
def show | ||
node = FlowchartNode.find(params[:id]) | ||
render json: node | ||
end | ||
|
||
def update | ||
flowchart_node = FlowchartNode.find(params[:id]) | ||
flowchart_node.update!(params[:node].permit(:text, :header, :button_text, :next_question)) | ||
render json: flowchart_node | ||
end | ||
|
||
def swap | ||
node_a = FlowchartNode.find(params[:id_a]) | ||
res = node_a&.swap(params[:id_b]) | ||
|
||
render json: { new_a: res[:new_a], new_b: res[:new_b] } | ||
end | ||
|
||
def delete | ||
delete_node = FlowchartNode.find(params[:id]) | ||
delete_node&.delete | ||
render json: delete_node | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
class FlowchartNode < ApplicationRecord | ||
belongs_to :flowchart | ||
validates :text, presence: true | ||
validates :header, presence: true | ||
validates :button_text, exclusion: { in: [''] } | ||
validates :next_question, exclusion: { in: [''] } | ||
validates :child_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true | ||
validates :sibling_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true | ||
validates :is_root, null: false | ||
validates :flowchart_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, null: false | ||
validates :deleted, inclusion: { in: [true, false] } | ||
|
||
def swap(swap_id) | ||
node_b = FlowchartNode.find(swap_id) | ||
|
||
self.text, node_b.text = node_b.text, text | ||
self.header, node_b.header = node_b.header, header | ||
self.button_text, node_b.button_text = node_b.button_text, button_text | ||
self.next_question, node_b.next_question = node_b.next_question, next_question | ||
self.is_root, node_b.is_root = node_b.is_root, is_root | ||
self.child_id, node_b.child_id = node_b.child_id, child_id | ||
ActiveRecord::Base.transaction do | ||
save! | ||
node_b.save! | ||
end | ||
|
||
{ new_a: self, new_b: node_b } | ||
end | ||
|
||
def delete | ||
self.deleted = true | ||
|
||
parent_node = FlowchartNode.find_by(child_id: id) | ||
left_node = FlowchartNode.find_by(sibling_id: id) | ||
child_node = FlowchartNode.find_by(id: child_id) | ||
|
||
if !parent_node && !left_node | ||
save! | ||
elsif !parent_node | ||
left_node.sibling_id = sibling_id | ||
ActiveRecord::Base.transaction do | ||
left_node.save! | ||
save! | ||
end | ||
else | ||
parent_node.child_id = sibling_id | ||
ActiveRecord::Base.transaction do | ||
parent_node.save! | ||
save! | ||
end | ||
end | ||
child_node&.child_delete | ||
self | ||
end | ||
|
||
def child_delete | ||
self.deleted = true | ||
save! | ||
sibling = FlowchartNode.find_by(id: sibling_id) | ||
child = FlowchartNode.find_by(id: child_id) | ||
sibling&.child_delete | ||
child&.child_delete | ||
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
Oops, something went wrong.