diff --git a/app/assets/stylesheets/dmpopidor.scss b/app/assets/stylesheets/dmpopidor.scss index eb61a93..10a6e48 100644 --- a/app/assets/stylesheets/dmpopidor.scss +++ b/app/assets/stylesheets/dmpopidor.scss @@ -1235,22 +1235,27 @@ } .answer-save-zone { flex: 1; + } + + .answer-save-zone, .answer-run-zone { display: flex; flex-direction: column; padding: 5px; - .please-save-message { + .message-zone { text-align: center; font-weight: bold; color: $rust; visibility: hidden; } - .answer-save-button { + .btn { font-size: 20px; font-weight: 700; - - background-color: gray; - border-color: gray; + + &.answer-save-button { + background-color: gray; + border-color: gray; + } } &.unsaved { @@ -1261,7 +1266,7 @@ border-color: $rust; } - .please-save-message { + .message-zone { visibility: inherit; } } diff --git a/app/controllers/madmp_codebase_controller.rb b/app/controllers/madmp_codebase_controller.rb index c1db7bb..05fcfcd 100644 --- a/app/controllers/madmp_codebase_controller.rb +++ b/app/controllers/madmp_codebase_controller.rb @@ -14,6 +14,12 @@ "schema": {}, "dmp_id": @fragment.dmp_id }) + @fragment.save_codebase_fragment(response.data, @fragment.madmp_schema) + + # EXAMPLE DATA : CODEBASE NEEDS FIXES + # file_path = Rails.root.join("config/schemas/codebase_example_data.json") + # response = JSON.load(File.open(file_path)) + # @fragment.save_codebase_fragment(response, @fragment.madmp_schema) authorize @fragment render json: response.to_json diff --git a/app/models/concerns/codebase_fragment.rb b/app/models/concerns/codebase_fragment.rb new file mode 100644 index 0000000..884b613 --- /dev/null +++ b/app/models/concerns/codebase_fragment.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +module CodebaseFragment + + def save_codebase_fragment(codebase_data, schema) + fragmented_data = {} + codebase_data.each do |prop, content| + schema_prop = schema.schema["properties"][prop] + + next if schema_prop.nil? || schema_prop["type"].nil? + + if schema_prop["type"].eql?("object") && + schema_prop["schema_id"].present? + #################################### + # OBJECT FIELDS + #################################### + sub_data = content # TMP: for readability + next if content["action"].nil? + + sub_schema = MadmpSchema.find(schema_prop["schema_id"]) + + if sub_data["action"].eql?("create") + cb_fragment = MadmpFragment.new( + dmp_id: dmp.id, + parent_id: id, + madmp_schema_id: sub_schema.id, + additional_info: { property_name: prop } + ) + cb_fragment.classname = sub_schema.classname + cb_fragment.instantiate + cb_fragment.save_codebase_fragment(sub_data["data"], sub_schema) + elsif sub_data["action"].eql?("update") && sub_data["dbid"] + cb_fragment = MadmpFragment.find(sub_data["dbid"]) + cb_fragment.save_codebase_fragment(sub_data["data"], sub_schema) + end + elsif schema_prop["type"].eql?("array") && + schema_prop["items"]["schema_id"].present? + #################################### + # ARRAY FIELDS + #################################### + data_list = content # TMP: for readability + data_list.each do |cb_data| + next if cb_data["action"].nil? + + sub_schema = MadmpSchema.find(schema_prop["items"]["schema_id"]) + + if cb_data["action"].eql?("create") + cb_fragment = MadmpFragment.new( + dmp_id: dmp.id, + parent_id: id, + madmp_schema_id: sub_schema.id, + additional_info: { property_name: prop } + ) + cb_fragment.classname = sub_schema.classname + cb_fragment.instantiate + cb_fragment.save_codebase_fragment(cb_data["data"], sub_schema) + elsif cb_data["action"].eql?("update") && cb_data["dbid"] + cb_fragment = MadmpFragment.find(cb_data["dbid"]) + cb_fragment.save_codebase_fragment(cb_data["data"], sub_schema) + end + end + else + fragmented_data[prop] = content + end + end + update!( + data: data.merge(fragmented_data), + additional_info: additional_info.except!("custom_value") + ) + end + +end diff --git a/app/models/madmp_fragment.rb b/app/models/madmp_fragment.rb index 18ec796..6613c35 100644 --- a/app/models/madmp_fragment.rb +++ b/app/models/madmp_fragment.rb @@ -23,6 +23,7 @@ include ValidationMessages include DynamicFormHelper + include CodebaseFragment # ================ # = Associations = diff --git a/app/views/branded/madmp_fragments/_edit.html.erb b/app/views/branded/madmp_fragments/_edit.html.erb index 065c74b..816d519 100644 --- a/app/views/branded/madmp_fragments/_edit.html.erb +++ b/app/views/branded/madmp_fragments/_edit.html.erb @@ -52,7 +52,7 @@
<%= f.button(_('Save'), class: "btn btn-default answer-save-button", type: "submit", disabled: true) %> - <%= d_('dmpopidor', 'You have pending changes, please save')%> + <%= d_('dmpopidor', 'You have pending changes, please save')%>
diff --git a/app/views/branded/plans/_edit_details.html.erb b/app/views/branded/plans/_edit_details.html.erb index 31ad06d..89401db 100644 --- a/app/views/branded/plans/_edit_details.html.erb +++ b/app/views/branded/plans/_edit_details.html.erb @@ -56,7 +56,7 @@
<%= f.button(_('Save'), class: "btn btn-default answer-save-button", type: "submit") %> - <%= d_('dmpopidor', 'You have pending changes, please save')%> + <%= d_('dmpopidor', 'You have pending changes, please save')%>
@@ -108,7 +108,7 @@
<%= f.button(_('Save'), class: "btn btn-default answer-save-button", type: "submit") %> - <%= d_('dmpopidor', 'You have pending changes, please save')%> + <%= d_('dmpopidor', 'You have pending changes, please save')%>
diff --git a/app/views/branded/shared/dynamic_form/_codebase_run.html.erb b/app/views/branded/shared/dynamic_form/_codebase_run.html.erb index 6dd7252..bbad3d2 100644 --- a/app/views/branded/shared/dynamic_form/_codebase_run.html.erb +++ b/app/views/branded/shared/dynamic_form/_codebase_run.html.erb @@ -14,4 +14,6 @@ 'data-toggle' => "tooltip", 'data-placement' => 'top', 'data-original-title' => ttip -} %> \ No newline at end of file +} %> + + \ No newline at end of file