diff --git a/app/assets/stylesheets/dmpopidor.scss b/app/assets/stylesheets/dmpopidor.scss index cd48ab0..b950f50 100644 --- a/app/assets/stylesheets/dmpopidor.scss +++ b/app/assets/stylesheets/dmpopidor.scss @@ -1002,7 +1002,7 @@ /* * STRUCTURED ANSWERS */ - .structured-answer { + .madmp-fragment { .dynamic-field { display: flex; align-items: center; diff --git a/app/controllers/madmp_fragments_controller.rb b/app/controllers/madmp_fragments_controller.rb new file mode 100644 index 0000000..07323f1 --- /dev/null +++ b/app/controllers/madmp_fragments_controller.rb @@ -0,0 +1,175 @@ +# frozen_string_literal: true + +class MadmpFragmentsController < ApplicationController + + after_action :verify_authorized + + # Instanciates a new structured answer/fragment + # def new + # @fragment = MadmpFragment.new + # @fragment.madmp_schema = MadmpSchema.find(params[:schema_id]) + # authorize @fragment + # render layout: false + # end + + # def edit + # @fragment = MadmpFragment.find(params[:id]) + # authorize @fragment + # render layout: false + # end + + # def create + # form_data = permitted_params.select { |k, v| schema_params(flat = true).include?(k) } + # @fragment = MadmpFragment.create( + # madmp_schema: MadmpSchema.find(permitted_params[:schema_id]), + # data: data_reformater(json_schema, form_data) + # ) + # authorize @fragment + # render json: { id: @fragment.id } + # end + + def update + @fragment = MadmpFragment.find(params[:id]) + form_data = permitted_params.select { |k, v| schema_params(flat = true).include?(k) } + @fragment.update(data: data_reformater(json_schema, form_data)) + authorize @fragment + render json: { id: @fragment.id } + end + + def create_or_update + p_params = permitted_params() + classname = params[:classname] + schema = MadmpSchema.find_by(classname: classname) + data = schema_params(schema) + + + # rubocop:disable BlockLength + MadmpFragment.transaction do + if p_params[:id].empty? + @fragment = MadmpFragment.new( + dmp_id: p_params[:dmp_id], + parent_id: p_params[:parent_id], + madmp_schema: schema, + data: data + ) + @fragment.classname = classname + authorize @fragment + @fragment.save! + else + @fragment = MadmpFragment.find_by!({ + id: p_params[:id], + dmp_id: p_params[:dmp_id] + }) + authorize @fragment + @fragment.update( + data: data + ) + end + end + + if @fragment.present? + obj_list = MadmpFragment.where( + dmp_id: @fragment.dmp_id, + parent_id: @fragment.parent_id, + classname: classname + ) + render json: { + "fragment_id" => @fragment.parent_id, + "classname" => classname, + "html" => render_to_string(partial: 'shared/dynamic_form/linked_fragment/list', locals: { + parent_id: @fragment.parent_id, + obj_list: obj_list, + classname: classname + }) + } + end + end + + + + def new_edit_linked_fragment + @classname = params[:classname] + @parent_fragment = MadmpFragment.find(params[:parent_id]) + @schema = MadmpSchema.find_by(classname: @classname) + @fragment = nil + if params[:fragment_id] + @fragment = MadmpFragment.find(params[:fragment_id]) + else + @fragment = MadmpFragment.new( + dmp_id: @parent_fragment.dmp_id, + parent_id: @parent_fragment.id + ) + end + authorize @fragment + respond_to do |format| + format.html + format.js { render :partial => "shared/dynamic_form/linked_fragment" } + end + end + + def destroy + @fragment = MadmpFragment.find(params[:id]) + classname = @fragment.classname + parent_id = @fragment.parent_id + dmp_id = @fragment.dmp_id + + authorize @fragment + if @fragment.destroy + obj_list = MadmpFragment.where( + dmp_id: dmp_id, + parent_id: parent_id, + classname: classname + ) + + render json: { + "fragment_id" => parent_id, + "classname" => classname, + "html" => render_to_string(partial: 'shared/dynamic_form/linked_fragment/list', locals: { + parent_id: @fragment.parent_id, + obj_list: obj_list, + classname: classname + }) + } + end + end + + # Gets fragment from a given id + def get_fragment + @fragment = MadmpFragment.find(params[:id]) + authorize @fragment + + if @fragment.present? + render json: @fragment.data + end + end + + private + + def json_schema + MadmpSchema.find(params['madmp_fragment']['schema_id']).schema + end + + # Get the parameters conresponding to the schema + def schema_params(schema, flat = false) + s_params = schema.generate_strong_params(flat) + params.require(:madmp_fragment).permit(s_params) + end + + def permitted_params + permit_arr = [:id, :dmp_id, :parent_id, :schema_id] + params.require(:madmp_fragment).permit(permit_arr) + end + + def funding_params + params.require(:madmp_fragment) + .permit(:fundingStatus, + funder: [:name, :dataPolicyUrl, funderId: [:value, :idType]], + grantId: [:value, :idType]) + end + + def partner_params + params.require(:madmp_fragment) + .permit(:name, :dataPolicyUrl, + orgId: [:value, :idType]) + end +end \ No newline at end of file diff --git a/app/controllers/paginable/madmp_schemas_controller.rb b/app/controllers/paginable/madmp_schemas_controller.rb new file mode 100644 index 0000000..b91a9ef --- /dev/null +++ b/app/controllers/paginable/madmp_schemas_controller.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class Paginable::MadmpSchemasController < ApplicationController + + include Paginable + + # /paginable/madmp_schemas/index/:page + def index + authorize(MadmpSchema) + paginable_renderise( + partial: "index", + scope: MadmpSchema.all, + query_params: { sort_field: "madmp_schemas.name", sort_direction: :asc }) + end + +end diff --git a/app/controllers/paginable/structured_data_schemas_controller.rb b/app/controllers/paginable/structured_data_schemas_controller.rb deleted file mode 100644 index c6b420f..0000000 --- a/app/controllers/paginable/structured_data_schemas_controller.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -class Paginable::StructuredDataSchemasController < ApplicationController - - include Paginable - - # /paginable/structured_data_schemas/index/:page - def index - authorize(StructuredDataSchema) - paginable_renderise( - partial: "index", - scope: StructuredDataSchema.all, - query_params: { sort_field: "structured_data_schemas.name", sort_direction: :asc }) - end - -end diff --git a/app/controllers/structured_answers_controller.rb b/app/controllers/structured_answers_controller.rb deleted file mode 100644 index 86afeac..0000000 --- a/app/controllers/structured_answers_controller.rb +++ /dev/null @@ -1,175 +0,0 @@ -# frozen_string_literal: true - -class StructuredAnswersController < ApplicationController - - after_action :verify_authorized - - # Instanciates a new structured answer/fragment - # def new - # @fragment = StructuredAnswer.new - # @fragment.structured_data_schema = StructuredDataSchema.find(params[:schema_id]) - # authorize @fragment - # render layout: false - # end - - # def edit - # @fragment = StructuredAnswer.find(params[:id]) - # authorize @fragment - # render layout: false - # end - - # def create - # form_data = permitted_params.select { |k, v| schema_params(flat = true).include?(k) } - # @fragment = StructuredAnswer.create( - # structured_data_schema: StructuredDataSchema.find(permitted_params[:schema_id]), - # data: data_reformater(json_schema, form_data) - # ) - # authorize @fragment - # render json: { id: @fragment.id } - # end - - def update - @fragment = StructuredAnswer.find(params[:id]) - form_data = permitted_params.select { |k, v| schema_params(flat = true).include?(k) } - @fragment.update(data: data_reformater(json_schema, form_data)) - authorize @fragment - render json: { id: @fragment.id } - end - - def create_or_update - p_params = permitted_params() - classname = params[:classname] - schema = StructuredDataSchema.find_by(classname: classname) - data = schema_params(schema) - - - # rubocop:disable BlockLength - StructuredAnswer.transaction do - if p_params[:id].empty? - @fragment = StructuredAnswer.new( - dmp_id: p_params[:dmp_id], - parent_id: p_params[:parent_id], - structured_data_schema: schema, - data: data - ) - @fragment.classname = classname - authorize @fragment - @fragment.save! - else - @fragment = StructuredAnswer.find_by!({ - id: p_params[:id], - dmp_id: p_params[:dmp_id] - }) - authorize @fragment - @fragment.update( - data: data - ) - end - end - - if @fragment.present? - obj_list = StructuredAnswer.where( - dmp_id: @fragment.dmp_id, - parent_id: @fragment.parent_id, - classname: classname - ) - render json: { - "fragment_id" => @fragment.parent_id, - "classname" => classname, - "html" => render_to_string(partial: 'shared/dynamic_form/linked_fragment/list', locals: { - parent_id: @fragment.parent_id, - obj_list: obj_list, - classname: classname - }) - } - end - end - - - - def new_edit_linked_fragment - @classname = params[:classname] - @parent_fragment = StructuredAnswer.find(params[:parent_id]) - @schema = StructuredDataSchema.find_by(classname: @classname) - @fragment = nil - if params[:fragment_id] - @fragment = StructuredAnswer.find(params[:fragment_id]) - else - @fragment = StructuredAnswer.new( - dmp_id: @parent_fragment.dmp_id, - parent_id: @parent_fragment.id - ) - end - authorize @fragment - respond_to do |format| - format.html - format.js { render :partial => "shared/dynamic_form/linked_fragment" } - end - end - - def destroy - @fragment = StructuredAnswer.find(params[:id]) - classname = @fragment.classname - parent_id = @fragment.parent_id - dmp_id = @fragment.dmp_id - - authorize @fragment - if @fragment.destroy - obj_list = StructuredAnswer.where( - dmp_id: dmp_id, - parent_id: parent_id, - classname: classname - ) - - render json: { - "fragment_id" => parent_id, - "classname" => classname, - "html" => render_to_string(partial: 'shared/dynamic_form/linked_fragment/list', locals: { - parent_id: @fragment.parent_id, - obj_list: obj_list, - classname: classname - }) - } - end - end - - # Gets fragment from a given id - def get_fragment - @fragment = StructuredAnswer.find(params[:id]) - authorize @fragment - - if @fragment.present? - render json: @fragment.data - end - end - - private - - def json_schema - StructuredDataSchema.find(params['structured_answer']['schema_id']).schema - end - - # Get the parameters conresponding to the schema - def schema_params(schema, flat = false) - s_params = schema.generate_strong_params(flat) - params.require(:structured_answer).permit(s_params) - end - - def permitted_params - permit_arr = [:id, :dmp_id, :parent_id, :schema_id] - params.require(:structured_answer).permit(permit_arr) - end - - def funding_params - params.require(:structured_answer) - .permit(:fundingStatus, - funder: [:name, :dataPolicyUrl, funderId: [:value, :idType]], - grantId: [:value, :idType]) - end - - def partner_params - params.require(:structured_answer) - .permit(:name, :dataPolicyUrl, - orgId: [:value, :idType]) - end -end \ No newline at end of file diff --git a/app/controllers/super_admin/madmp_schemas_controller.rb b/app/controllers/super_admin/madmp_schemas_controller.rb new file mode 100644 index 0000000..a9a8dcc --- /dev/null +++ b/app/controllers/super_admin/madmp_schemas_controller.rb @@ -0,0 +1,65 @@ +module SuperAdmin + class MadmpSchemasController < ApplicationController + + # GET /madmp_schemas + def index + authorize(MadmpSchema) + render(:index, locals: { madmp_schemas: MadmpSchema.all.page(1) }) + end + + def new + authorize(MadmpSchema) + @schema = MadmpSchema.new + end + + def create + authorize(MadmpSchema) + @schema = MadmpSchema.new(permitted_params) + if @schema.save + flash.now[:notice] = success_message(@schema, _("created")) + render :edit + else + flash.now[:alert] = failure_message(@schema, _("create")) + render :new + end + end + + def edit + authorize(MadmpSchema) + @schema = MadmpSchema.find(params[:id]) + end + + + def update + authorize(MadmpSchema) + @schema = MadmpSchema.find(params[:id]) + if @schema.update_attributes(permitted_params) + flash.now[:notice] = success_message(@schema, _("updated")) + else + flash.now[:alert] = failure_message(@schema, _("update")) + end + render :edit + end + + def destroy + authorize(MadmpSchema) + @schema = MadmpSchema.find(params[:id]) + if @schema.destroy + msg = success_message(@schema, _("deleted")) + redirect_to super_admin_madmp_schemas_path, notice: msg + else + flash.now[:alert] = failure_message(@schema, _("delete")) + redner :edit + end + end + + + # Private instance methods + private + + def permitted_params + params.require(:madmp_schema).permit(:label, :name, :version, :classname, :schema) + end + + end +end \ No newline at end of file diff --git a/app/controllers/super_admin/structured_data_schemas_controller.rb b/app/controllers/super_admin/structured_data_schemas_controller.rb deleted file mode 100644 index c87cce3..0000000 --- a/app/controllers/super_admin/structured_data_schemas_controller.rb +++ /dev/null @@ -1,65 +0,0 @@ -module SuperAdmin - class StructuredDataSchemasController < ApplicationController - - # GET /structured_data_schemas - def index - authorize(StructuredDataSchema) - render(:index, locals: { structured_data_schemas: StructuredDataSchema.all.page(1) }) - end - - def new - authorize(StructuredDataSchema) - @schema = StructuredDataSchema.new - end - - def create - authorize(StructuredDataSchema) - @schema = StructuredDataSchema.new(permitted_params) - if @schema.save - flash.now[:notice] = success_message(@schema, _("created")) - render :edit - else - flash.now[:alert] = failure_message(@schema, _("create")) - render :new - end - end - - def edit - authorize(StructuredDataSchema) - @schema = StructuredDataSchema.find(params[:id]) - end - - - def update - authorize(StructuredDataSchema) - @schema = StructuredDataSchema.find(params[:id]) - if @schema.update_attributes(permitted_params) - flash.now[:notice] = success_message(@schema, _("updated")) - else - flash.now[:alert] = failure_message(@schema, _("update")) - end - render :edit - end - - def destroy - authorize(StructuredDataSchema) - @schema = StructuredDataSchema.find(params[:id]) - if @schema.destroy - msg = success_message(@schema, _("deleted")) - redirect_to super_admin_structured_data_schemas_path, notice: msg - else - flash.now[:alert] = failure_message(@schema, _("delete")) - redner :edit - end - end - - - # Private instance methods - private - - def permitted_params - params.require(:structured_data_schema).permit(:label, :name, :version, :classname, :schema) - end - - end -end \ No newline at end of file diff --git a/app/javascript/views/shared/dynamic_form.js b/app/javascript/views/shared/dynamic_form.js index 3b1656d..d45ab53 100644 --- a/app/javascript/views/shared/dynamic_form.js +++ b/app/javascript/views/shared/dynamic_form.js @@ -1,4 +1,4 @@ -$(document).on('click', '.structured-answer .actions .add-record', (e) => { +$(document).on('click', '.madmp-fragment .actions .add-record', (e) => { const currentField = $(e.target.closest('.dynamic-field')); const clonedField = currentField.clone(true, true); @@ -8,7 +8,7 @@ currentField.after(clonedField); }); -$(document).on('click', '.structured-answer .actions .remove-record', (e) => { +$(document).on('click', '.madmp-fragment .actions .remove-record', (e) => { const currentField = $(e.target.closest('.dynamic-field')); currentField.remove(); }); diff --git a/app/models/answer.rb b/app/models/answer.rb index 6b1a534..083e638 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -46,7 +46,7 @@ has_many :notes - has_one :structured_answer + has_one :madmp_fragment, class_name: "MadmpFragment" # =============== diff --git a/app/models/fragment/cost.rb b/app/models/fragment/cost.rb index 0726c35..43bab11 100644 --- a/app/models/fragment/cost.rb +++ b/app/models/fragment/cost.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Cost < StructuredAnswer +class Fragment::Cost < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/data_collection.rb b/app/models/fragment/data_collection.rb index c3e29b2..29fe9d5 100644 --- a/app/models/fragment/data_collection.rb +++ b/app/models/fragment/data_collection.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::DataCollection < StructuredAnswer +class Fragment::DataCollection < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/data_quality.rb b/app/models/fragment/data_quality.rb index 5fba0d5..3a6d562 100644 --- a/app/models/fragment/data_quality.rb +++ b/app/models/fragment/data_quality.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::DataQuality < StructuredAnswer +class Fragment::DataQuality < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/distribution.rb b/app/models/fragment/distribution.rb index 0fce395..0bf922f 100644 --- a/app/models/fragment/distribution.rb +++ b/app/models/fragment/distribution.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Distribution < StructuredAnswer +class Fragment::Distribution < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/dmp.rb b/app/models/fragment/dmp.rb index 715bb53..aa17fb8 100644 --- a/app/models/fragment/dmp.rb +++ b/app/models/fragment/dmp.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Dmp < StructuredAnswer +class Fragment::Dmp < MadmpFragment def meta Fragment::Meta.where(dmp_id: id).first diff --git a/app/models/fragment/documentation.rb b/app/models/fragment/documentation.rb index 9dd4edf..9738ea3 100644 --- a/app/models/fragment/documentation.rb +++ b/app/models/fragment/documentation.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Documentation < StructuredAnswer +class Fragment::Documentation < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/ethical_issue.rb b/app/models/fragment/ethical_issue.rb index bb37b9d..1480498 100644 --- a/app/models/fragment/ethical_issue.rb +++ b/app/models/fragment/ethical_issue.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::EthicalIssue < StructuredAnswer +class Fragment::EthicalIssue < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/funder.rb b/app/models/fragment/funder.rb index d7cd244..a19d0a4 100644 --- a/app/models/fragment/funder.rb +++ b/app/models/fragment/funder.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Funder < StructuredAnswer +class Fragment::Funder < MadmpFragment def fundings Fragment::Funding.where("(data->>'funder'->>'dbId')::int = ?", id) diff --git a/app/models/fragment/funding.rb b/app/models/fragment/funding.rb index 3273199..df5d41e 100644 --- a/app/models/fragment/funding.rb +++ b/app/models/fragment/funding.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Funding < StructuredAnswer +class Fragment::Funding < MadmpFragment def project Fragment::Project.where(id: data['project']['dbId']).first diff --git a/app/models/fragment/legal_issue.rb b/app/models/fragment/legal_issue.rb index fe326d2..3bc9648 100644 --- a/app/models/fragment/legal_issue.rb +++ b/app/models/fragment/legal_issue.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::LegalIssue < StructuredAnswer +class Fragment::LegalIssue < MadmpFragment def legal_advisor Fragment::Person.where(id: data['legal_advisor']['dbId']).first diff --git a/app/models/fragment/meta.rb b/app/models/fragment/meta.rb index f5658fb..9729ac1 100644 --- a/app/models/fragment/meta.rb +++ b/app/models/fragment/meta.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Meta < StructuredAnswer +class Fragment::Meta < MadmpFragment def contact contact = nil diff --git a/app/models/fragment/metadata_format.rb b/app/models/fragment/metadata_format.rb index 68c9bfd..9994278 100644 --- a/app/models/fragment/metadata_format.rb +++ b/app/models/fragment/metadata_format.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::MetadataFormat < StructuredAnswer +class Fragment::MetadataFormat < MadmpFragment def documentation Fragment::Documentation.where("(data->>'metadata_format'->>'dbId')::int = ?", id) diff --git a/app/models/fragment/partner.rb b/app/models/fragment/partner.rb index e772fa4..ef14870 100644 --- a/app/models/fragment/partner.rb +++ b/app/models/fragment/partner.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Partner < StructuredAnswer +class Fragment::Partner < MadmpFragment def project Fragment::Project.where(id: data['project']['dbId']).first diff --git a/app/models/fragment/person.rb b/app/models/fragment/person.rb index 331c01b..bf98275 100644 --- a/app/models/fragment/person.rb +++ b/app/models/fragment/person.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Person < StructuredAnswer +class Fragment::Person < MadmpFragment def documentations diff --git a/app/models/fragment/personal_data_issue.rb b/app/models/fragment/personal_data_issue.rb index 7e9da94..9705bfb 100644 --- a/app/models/fragment/personal_data_issue.rb +++ b/app/models/fragment/personal_data_issue.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::PersonalDataIssue < StructuredAnswer +class Fragment::PersonalDataIssue < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/preservation_issue.rb b/app/models/fragment/preservation_issue.rb index 5273cfb..3218e88 100644 --- a/app/models/fragment/preservation_issue.rb +++ b/app/models/fragment/preservation_issue.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::PreservationIssue < StructuredAnswer +class Fragment::PreservationIssue < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/project.rb b/app/models/fragment/project.rb index 5d68a18..bdea74b 100644 --- a/app/models/fragment/project.rb +++ b/app/models/fragment/project.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Project < StructuredAnswer +class Fragment::Project < MadmpFragment def principalInvestigator principalInvestigator = nil diff --git a/app/models/fragment/research_output.rb b/app/models/fragment/research_output.rb index d1b979e..d4e20f6 100644 --- a/app/models/fragment/research_output.rb +++ b/app/models/fragment/research_output.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::ResearchOutput < StructuredAnswer +class Fragment::ResearchOutput < MadmpFragment def contact Fragment::Person.where(id: data['contact']['dbId']).first diff --git a/app/models/fragment/reuse_data.rb b/app/models/fragment/reuse_data.rb index 52db319..0dfc123 100644 --- a/app/models/fragment/reuse_data.rb +++ b/app/models/fragment/reuse_data.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::ReuseData < StructuredAnswer +class Fragment::ReuseData < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/sharing.rb b/app/models/fragment/sharing.rb index 74347a5..0f22d4a 100644 --- a/app/models/fragment/sharing.rb +++ b/app/models/fragment/sharing.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::Sharing < StructuredAnswer +class Fragment::Sharing < MadmpFragment def research_output self.parent diff --git a/app/models/fragment/staff_member.rb b/app/models/fragment/staff_member.rb index dd34360..149a0fd 100644 --- a/app/models/fragment/staff_member.rb +++ b/app/models/fragment/staff_member.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::StaffMember < StructuredAnswer +class Fragment::StaffMember < MadmpFragment def agent Fragment::Person.where(id: data['agent']['dbId']) diff --git a/app/models/fragment/technical_resource.rb b/app/models/fragment/technical_resource.rb index 2a46fa3..da1e683 100644 --- a/app/models/fragment/technical_resource.rb +++ b/app/models/fragment/technical_resource.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::TechnicalResource < StructuredAnswer +class Fragment::TechnicalResource < MadmpFragment def technical_resource_usages Fragment::TechnicalResourceUsage.where("(data->>'technical_resource'->>'dbId')::int = ?", id) diff --git a/app/models/fragment/technical_resource_usage.rb b/app/models/fragment/technical_resource_usage.rb index 8a73366..6896f82 100644 --- a/app/models/fragment/technical_resource_usage.rb +++ b/app/models/fragment/technical_resource_usage.rb @@ -1,11 +1,11 @@ # == Schema Information # -# Table name: structured_answers +# Table name: madmp_fragments # # id :integer not null, primary key # data :json # answer_id :integer -# structured_data_schema_id :integer +# madmp_schema_id :integer # created_at :datetime not null # updated_at :datetime not null # classname :string @@ -14,11 +14,11 @@ # # Indexes # -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) # -class Fragment::TechnicalResourceUsage < StructuredAnswer +class Fragment::TechnicalResourceUsage < MadmpFragment def research_output self.parent diff --git a/app/models/madmp_fragment.rb b/app/models/madmp_fragment.rb new file mode 100644 index 0000000..9d9e5d2 --- /dev/null +++ b/app/models/madmp_fragment.rb @@ -0,0 +1,156 @@ +# == Schema Information +# +# Table name: madmp_fragments +# +# id :integer not null, primary key +# data :json +# answer_id :integer +# madmp_schema_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# classname :string +# dmp_id :integer +# parent_id :integer +# +# Indexes +# +# index_madmp_fragments_on_answer_id (answer_id) +# index_madmp_fragments_on_madmp_schema_id (madmp_schema_id) +# +require 'jsonpath' + +class MadmpFragment < ActiveRecord::Base + + include ValidationMessages + include DynamicFormHelper + + # ================ + # = Associations = + # ================ + + belongs_to :answer + belongs_to :madmp_schema, class_name: "MadmpSchema" + belongs_to :dmp, class_name: "Fragment::Dmp", foreign_key: "dmp_id" + has_many :children, class_name: "MadmpFragment", foreign_key: "parent_id" + belongs_to :parent, class_name: "MadmpFragment", foreign_key: "parent_id" + + # =============== + # = Validations = + # =============== + + #validates :madmp_schema, presence: { message: PRESENCE_MESSAGE } + + # ================ + # = Single Table Inheritence = + # ================ + self.inheritance_column = :classname + scope :costs, -> { where(classname: 'cost') } + scope :data_collections, -> { where(classname: 'data_collection') } + scope :data_qualities, -> { where(classname: 'data_quality') } + scope :distributions, -> { where(classname: 'distribution') } + scope :dmps, -> { where(classname: 'dmp') } + scope :documentations, -> { where(classname: 'documentation') } + scope :ethical_issues, -> { where(classname: 'ethical_issue') } + scope :funders, -> { where(classname: 'funder') } + scope :fundings, -> { where(classname: 'funding') } + scope :metas, -> { where(classname: 'meta') } + scope :metadata_formats, -> { where(classname: 'metadata_format') } + scope :partners, -> { where(classname: 'partner') } + scope :persons, -> { where(classname: 'person') } + scope :personal_data_issues, -> { where(classname: 'personal_data_issue') } + scope :preservation_issues, -> { where(classname: 'preservation_issue') } + scope :projects, -> { where(classname: 'project') } + scope :research_outputs, -> { where(classname: 'research_output') } + scope :reuse_datas, -> { where(classname: 'reuse_data') } + scope :sharings, -> { where(classname: 'sharing') } + scope :staff_members, -> { where(classname: 'staff_member') } + scope :technical_resource_usages, -> { where(classname: 'technical_resource_usage') } + scope :technical_resources, -> { where(classname: 'technical_resource') } + + + # ============= + # = Callbacks = + # ============= + + after_create :update_parent_references + after_destroy :update_parent_references + + # ================= + # = Class methods = + # ================= + + def plan + plan = nil + if self.answer.nil? + self.dmp.plan + else + plan = self.answer.plan + end + end + + # Returns the schema associated to the JSON fragment + def json_schema + self.madmp_schema.schema + end + + # Returns a human readable version of the structured answer + def to_s + displayable = "" + if json_schema["to_string"] + json_schema["to_string"].each do |pattern| + # if it's a JsonPath pattern + if pattern.first == "$" + displayable += JsonPath.on(self.data, pattern).first + else + displayable += pattern + end + end + else + displayable = self.data.to_s + end + displayable + end + + # This method generates references to the child fragments in the parent fragment + # it updates the json "data" field in the database + # it groups the children fragment by classname and extracts the list of ids + # to create the json structure needed to update the "data" field + # this method should be called when creating or deleting a child fragment + def update_parent_references + unless self.parent.nil? + # Get each fragment grouped by its classname + classified_children = parent.children.group_by(&:classname) + parent_data = self.parent.data + + classified_children.each do |classname, children| + if children.count >= 2 + # if there is more than 1 child, should pluralize the classname + parent_data[classname.pluralize(children.count)] = children.map { |c| { "dbId" => c.id } } + parent_data.delete(classname) if parent_data[classname] + else + parent_data[classname] = { "dbId" => children.first.id } + parent_data.delete(classname.pluralize(2)) if parent_data[classname.pluralize(2)] + end + end + self.parent.update(data: parent_data) + end + end + + # Saves (and creates, if needed) the structured answer ("fragment") + def self.save_madmp_fragment(answer, data, schema, parent_id = nil) + # Extract the form data corresponding to the schema of the structured question + s_answer = MadmpFragment.find_or_initialize_by(answer_id: answer.id) do |sa| + sa.answer = answer + sa.madmp_schema = schema + sa.classname = schema.classname + sa.dmp_id = answer.plan.json_fragment().id + sa.parent_id = parent_id + end + s_answer.assign_attributes(data: data) + s_answer.save + end + + def self.find_sti_class(type_name) + self + end +end diff --git a/app/models/madmp_schema.rb b/app/models/madmp_schema.rb new file mode 100644 index 0000000..f28e752 --- /dev/null +++ b/app/models/madmp_schema.rb @@ -0,0 +1,69 @@ +# == Schema Information +# +# Table name: madmp_schemas +# +# id :integer not null, primary key +# label :string +# name :string +# version :integer +# schema :json +# org_id :integer +# classname :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_madmp_schemas_on_org_id (org_id) +# + +class MadmpSchema < ActiveRecord::Base + include ValidationMessages + + + belongs_to :org + has_many :madmp_fragments + has_many :questions + + delegate :costs, + :dmps, + :funders, + :metas, + :partners, + :persons, + :projects, + :research_outputs, to: :madmp_fragments + + + validates :name, presence: { message: PRESENCE_MESSAGE }, + uniqueness: { message: UNIQUENESS_MESSAGE } + + #validates :schema, presence: { message: PRESENCE_MESSAGE }, + # json: true + + + # ================= + # = Class Methods = + # ================= + + def detailed_name + label + " ( " + name + "_V" + version.to_s + " )" + end + + def generate_strong_params(flat = false) + parameters = Array.new + self.schema['properties'].each do |key, prop| + if prop['type'] == "object" + sub_schema = MadmpSchema.find_by(classname: prop['classname']) + parameters.append(key => sub_schema.generate_strong_params(false)) + elsif prop['type'] == "array" && !flat + parameters.append({key => []}) + # parameters.append(key) + else + parameters.append(key) + end + end + parameters + end + +end diff --git a/app/models/org.rb b/app/models/org.rb index 61750ce..6c3a324 100644 --- a/app/models/org.rb +++ b/app/models/org.rb @@ -80,7 +80,7 @@ has_many :departments - has_many :structured_data_schemas + has_many :madmp_schemas # =============== # = Validations = diff --git a/app/models/question.rb b/app/models/question.rb index 5c0df26..44b31b6 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -14,11 +14,11 @@ # option_comment_display :boolean default("true") # modifiable :boolean # versionable_id :string(36) -# structured_data_schema_id :integer +# madmp_schema_id :integer # # Indexes # -# index_questions_on_structured_data_schema_id (structured_data_schema_id) +# index_questions_on_madmp_schema_id (madmp_schema_id) # index_questions_on_versionable_id (versionable_id) # questions_question_format_id_idx (question_format_id) # questions_section_id_idx (section_id) @@ -62,7 +62,7 @@ has_one :template, through: :section - belongs_to :structured_data_schema + belongs_to :madmp_schema, class_name: "MadmpSchema" # =============== # = Validations = diff --git a/app/models/structured_answer.rb b/app/models/structured_answer.rb deleted file mode 100644 index 853e073..0000000 --- a/app/models/structured_answer.rb +++ /dev/null @@ -1,156 +0,0 @@ -# == Schema Information -# -# Table name: structured_answers -# -# id :integer not null, primary key -# data :json -# answer_id :integer -# structured_data_schema_id :integer -# created_at :datetime not null -# updated_at :datetime not null -# classname :string -# dmp_id :integer -# parent_id :integer -# -# Indexes -# -# index_structured_answers_on_answer_id (answer_id) -# index_structured_answers_on_structured_data_schema_id (structured_data_schema_id) -# -require 'jsonpath' - -class StructuredAnswer < ActiveRecord::Base - - include ValidationMessages - include DynamicFormHelper - - # ================ - # = Associations = - # ================ - - belongs_to :answer - belongs_to :structured_data_schema - belongs_to :dmp, class_name: "Fragment::Dmp", foreign_key: "dmp_id" - has_many :children, class_name: "StructuredAnswer", foreign_key: "parent_id" - belongs_to :parent, class_name: "StructuredAnswer", foreign_key: "parent_id" - - # =============== - # = Validations = - # =============== - - #validates :structured_data_schema, presence: { message: PRESENCE_MESSAGE } - - # ================ - # = Single Table Inheritence = - # ================ - self.inheritance_column = :classname - scope :costs, -> { where(classname: 'cost') } - scope :data_collections, -> { where(classname: 'data_collection') } - scope :data_qualities, -> { where(classname: 'data_quality') } - scope :distributions, -> { where(classname: 'distribution') } - scope :dmps, -> { where(classname: 'dmp') } - scope :documentations, -> { where(classname: 'documentation') } - scope :ethical_issues, -> { where(classname: 'ethical_issue') } - scope :funders, -> { where(classname: 'funder') } - scope :fundings, -> { where(classname: 'funding') } - scope :metas, -> { where(classname: 'meta') } - scope :metadata_formats, -> { where(classname: 'metadata_format') } - scope :partners, -> { where(classname: 'partner') } - scope :persons, -> { where(classname: 'person') } - scope :personal_data_issues, -> { where(classname: 'personal_data_issue') } - scope :preservation_issues, -> { where(classname: 'preservation_issue') } - scope :projects, -> { where(classname: 'project') } - scope :research_outputs, -> { where(classname: 'research_output') } - scope :reuse_datas, -> { where(classname: 'reuse_data') } - scope :sharings, -> { where(classname: 'sharing') } - scope :staff_members, -> { where(classname: 'staff_member') } - scope :technical_resource_usages, -> { where(classname: 'technical_resource_usage') } - scope :technical_resources, -> { where(classname: 'technical_resource') } - - - # ============= - # = Callbacks = - # ============= - - after_create :update_parent_references - after_destroy :update_parent_references - - # ================= - # = Class methods = - # ================= - - def plan - plan = nil - if self.answer.nil? - self.dmp.plan - else - plan = self.answer.plan - end - end - - # Returns the schema associated to the JSON fragment - def json_schema - self.structured_data_schema.schema - end - - # Returns a human readable version of the structured answer - def to_s - displayable = "" - if json_schema["to_string"] - json_schema["to_string"].each do |pattern| - # if it's a JsonPath pattern - if pattern.first == "$" - displayable += JsonPath.on(self.data, pattern).first - else - displayable += pattern - end - end - else - displayable = self.data.to_s - end - displayable - end - - # This method generates references to the child fragments in the parent fragment - # it updates the json "data" field in the database - # it groups the children fragment by classname and extracts the list of ids - # to create the json structure needed to update the "data" field - # this method should be called when creating or deleting a child fragment - def update_parent_references - unless self.parent.nil? - # Get each fragment grouped by its classname - classified_children = parent.children.group_by(&:classname) - parent_data = self.parent.data - - classified_children.each do |classname, children| - if children.count >= 2 - # if there is more than 1 child, should pluralize the classname - parent_data[classname.pluralize(children.count)] = children.map { |c| { "dbId" => c.id } } - parent_data.delete(classname) if parent_data[classname] - else - parent_data[classname] = { "dbId" => children.first.id } - parent_data.delete(classname.pluralize(2)) if parent_data[classname.pluralize(2)] - end - end - self.parent.update(data: parent_data) - end - end - - # Saves (and creates, if needed) the structured answer ("fragment") - def self.save_structured_answer(answer, data, schema, parent_id = nil) - # Extract the form data corresponding to the schema of the structured question - s_answer = StructuredAnswer.find_or_initialize_by(answer_id: answer.id) do |sa| - sa.answer = answer - sa.structured_data_schema = schema - sa.classname = schema.classname - sa.dmp_id = answer.plan.json_fragment().id - sa.parent_id = parent_id - end - s_answer.assign_attributes(data: data) - s_answer.save - end - - def self.find_sti_class(type_name) - self - end -end diff --git a/app/models/structured_data_schema.rb b/app/models/structured_data_schema.rb deleted file mode 100644 index 04b2de7..0000000 --- a/app/models/structured_data_schema.rb +++ /dev/null @@ -1,68 +0,0 @@ -# == Schema Information -# -# Table name: structured_data_schemas -# -# id :integer not null, primary key -# label :string -# name :string -# version :integer -# schema :json -# org_id :integer -# classname :string -# created_at :datetime not null -# updated_at :datetime not null -# -# Indexes -# -# index_structured_data_schemas_on_org_id (org_id) -# - -class StructuredDataSchema < ActiveRecord::Base - include ValidationMessages - - belongs_to :org - has_many :structured_answers - has_many :questions - - delegate :costs, - :dmps, - :funders, - :metas, - :partners, - :persons, - :projects, - :research_outputs, to: :structured_answers - - - validates :name, presence: { message: PRESENCE_MESSAGE }, - uniqueness: { message: UNIQUENESS_MESSAGE } - - #validates :schema, presence: { message: PRESENCE_MESSAGE }, - # json: true - - - # ================= - # = Class Methods = - # ================= - - def detailed_name - label + " ( " + name + "_V" + version.to_s + " )" - end - - def generate_strong_params(flat = false) - parameters = Array.new - self.schema['properties'].each do |key, prop| - if prop['type'] == "object" - sub_schema = StructuredDataSchema.find_by(classname: prop['classname']) - parameters.append(key => sub_schema.generate_strong_params(false)) - elsif prop['type'] == "array" && !flat - parameters.append({key => []}) - # parameters.append(key) - else - parameters.append(key) - end - end - parameters - end - -end diff --git a/app/policies/madmp_fragment_policy.rb b/app/policies/madmp_fragment_policy.rb new file mode 100644 index 0000000..3b3e9f7 --- /dev/null +++ b/app/policies/madmp_fragment_policy.rb @@ -0,0 +1,19 @@ +class MadmpFragmentPolicy < ApplicationPolicy + def initialize(user, fragment) + raise Pundit::NotAuthorizedError, _("must be logged in") unless user + @user = user + @fragment = fragment + end + def create_or_update? + @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner + end + def destroy? + @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner + end + def new_edit_linked_fragment? + @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner + end + def get_fragment? + @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner + end +end diff --git a/app/policies/madmp_schema_policy.rb b/app/policies/madmp_schema_policy.rb new file mode 100644 index 0000000..acf445b --- /dev/null +++ b/app/policies/madmp_schema_policy.rb @@ -0,0 +1,24 @@ +class MadmpSchemaPolicy < ApplicationPolicy + def initialize(user, *args) + raise Pundit::NotAuthorizedError, _("must be logged in") unless user + @user = user + end + def index? + @user.can_super_admin? + end + def new? + @user.can_super_admin? + end + def create? + @user.can_super_admin? + end + def edit? + @user.can_super_admin? + end + def update? + @user.can_super_admin? + end + def destroy? + @user.can_super_admin? + end +end diff --git a/app/policies/structured_answer_policy.rb b/app/policies/structured_answer_policy.rb deleted file mode 100644 index c4fc46a..0000000 --- a/app/policies/structured_answer_policy.rb +++ /dev/null @@ -1,19 +0,0 @@ -class StructuredAnswerPolicy < ApplicationPolicy - def initialize(user, fragment) - raise Pundit::NotAuthorizedError, _("must be logged in") unless user - @user = user - @fragment = fragment - end - def create_or_update? - @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner - end - def destroy? - @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner - end - def new_edit_linked_fragment? - @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner - end - def get_fragment? - @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner - end -end diff --git a/app/policies/structured_data_schema_policy.rb b/app/policies/structured_data_schema_policy.rb deleted file mode 100644 index 089a255..0000000 --- a/app/policies/structured_data_schema_policy.rb +++ /dev/null @@ -1,24 +0,0 @@ -class StructuredDataSchemaPolicy < ApplicationPolicy - def initialize(user, *args) - raise Pundit::NotAuthorizedError, _("must be logged in") unless user - @user = user - end - def index? - @user.can_super_admin? - end - def new? - @user.can_super_admin? - end - def create? - @user.can_super_admin? - end - def edit? - @user.can_super_admin? - end - def update? - @user.can_super_admin? - end - def destroy? - @user.can_super_admin? - end -end diff --git a/app/views/branded/layouts/_branding.html.erb b/app/views/branded/layouts/_branding.html.erb index adfe1e4..83e5ffc 100644 --- a/app/views/branded/layouts/_branding.html.erb +++ b/app/views/branded/layouts/_branding.html.erb @@ -108,8 +108,8 @@ <% end %> <% if current_user.can_super_admin? %> -
| <%= _('Label') %> <%= paginable_sort_link('madmp_schemas.label') %> | +<%= _('Name') %> <%= paginable_sort_link('madmp_schemas.name') %> | +<%= _('Classname') %> | +<%= _('Version') %> | +<%= _('Actions') %> | +
|---|---|---|---|---|
| <%= schema.label %> | +<%= schema.name %> | +<%= schema.classname %> | +<%= schema.version %> | +
+
+
+
+
+ |
+
| <%= _('Label') %> <%= paginable_sort_link('structured_data_schemas.label') %> | -<%= _('Name') %> <%= paginable_sort_link('structured_data_schemas.name') %> | -<%= _('Classname') %> | -<%= _('Version') %> | -<%= _('Actions') %> | -
|---|---|---|---|---|
| <%= schema.label %> | -<%= schema.name %> | -<%= schema.classname %> | -<%= schema.version %> | -
-
-
-
-
- |
-