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? %> -
  • > - <%= link_to d_('dmpopidor', 'Schemas'), super_admin_structured_data_schemas_path %> +
  • > + <%= link_to d_('dmpopidor', 'Schemas'), super_admin_madmp_schemas_path %>
  • <% end %> <% if current_user.can_super_admin? %> diff --git a/app/views/branded/org_admin/questions/_form.html.erb b/app/views/branded/org_admin/questions/_form.html.erb index 0de0859..6445ce6 100644 --- a/app/views/branded/org_admin/questions/_form.html.erb +++ b/app/views/branded/org_admin/questions/_form.html.erb @@ -43,12 +43,12 @@ data-attribute="question_schema" style="<%= 'display:none' if !question.question_format.structured?%>" > - <%= f.label(:structured_data_schema_id, _('Data Schema'), class: "control-label") %> - <%= f.select :structured_data_schema_id, - options_from_collection_for_select(structured_data_schemas, + <%= f.label(:madmp_schema_id, _('Data Schema'), class: "control-label") %> + <%= f.select :madmp_schema_id, + options_from_collection_for_select(madmp_schemas, :id, :detailed_name, - question.structured_data_schema_id), + question.madmp_schema_id), {}, class: "form-control" %> diff --git a/app/views/branded/org_admin/questions/_show.html.erb b/app/views/branded/org_admin/questions/_show.html.erb index bad9a8b..36a2531 100644 --- a/app/views/branded/org_admin/questions/_show.html.erb +++ b/app/views/branded/org_admin/questions/_show.html.erb @@ -9,7 +9,7 @@
    <% q_format = question.question_format %> - <% q_schema = question.structured_data_schema %> + <% q_schema = question.madmp_schema %>
    <%= _('Question number')%>
    <%= question.number %>
    diff --git a/app/views/branded/paginable/madmp_schemas/_index.html.erb b/app/views/branded/paginable/madmp_schemas/_index.html.erb new file mode 100644 index 0000000..c433453 --- /dev/null +++ b/app/views/branded/paginable/madmp_schemas/_index.html.erb @@ -0,0 +1,39 @@ +
    + + + + + + + + + + + + <% scope.each do |schema| %> + + + + + + + + <% end %> + +
    <%= _('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 %> + +
    +
    diff --git a/app/views/branded/paginable/structured_data_schemas/_index.html.erb b/app/views/branded/paginable/structured_data_schemas/_index.html.erb deleted file mode 100644 index 6871596..0000000 --- a/app/views/branded/paginable/structured_data_schemas/_index.html.erb +++ /dev/null @@ -1,39 +0,0 @@ -
    - - - - - - - - - - - - <% scope.each do |schema| %> - - - - - - - - <% end %> - -
    <%= _('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 %> - -
    -
    diff --git a/app/views/branded/plans/_edit_details.html.erb b/app/views/branded/plans/_edit_details.html.erb index ccea85d..b23551b 100644 --- a/app/views/branded/plans/_edit_details.html.erb +++ b/app/views/branded/plans/_edit_details.html.erb @@ -8,7 +8,7 @@
    -
    +

    <%= _("Plan Metadata") %>

    <%= render(partial: 'plans/plan_details/plan_metadata', locals: { f: f, @@ -17,7 +17,7 @@ } ) %>
    -
    +

    <%= _("Project Details") %>

    <%= render(partial: 'plans/plan_details/plan_project', locals: { f: f, diff --git a/app/views/branded/plans/new_edit_linked_fragment.js.erb b/app/views/branded/plans/new_edit_linked_fragment.js.erb index 2ce6f03..5cc65ae 100644 --- a/app/views/branded/plans/new_edit_linked_fragment.js.erb +++ b/app/views/branded/plans/new_edit_linked_fragment.js.erb @@ -3,7 +3,7 @@ $('#modal-window').modal(); - $('#structured_answer_form').bind('ajax:success', (event, response) => { + $('#madmp_fragment_form').bind('ajax:success', (event, response) => { $(`.project-details.${response.type}-list`).html(response.html); $('#modal-window').modal('hide'); }).bind('ajax:error', () => { diff --git a/app/views/branded/plans/plan_details/_linked_fragment_list.html.erb b/app/views/branded/plans/plan_details/_linked_fragment_list.html.erb index ff41762..f43bd5c 100644 --- a/app/views/branded/plans/plan_details/_linked_fragment_list.html.erb +++ b/app/views/branded/plans/plan_details/_linked_fragment_list.html.erb @@ -24,7 +24,7 @@ + data-url="<%= madmp_fragment_path(obj.id, plan_id: plan.id) %>"><%= _('Delete') %> <% end %> diff --git a/app/views/branded/plans/plan_details/_linked_fragment_modal.html.erb b/app/views/branded/plans/plan_details/_linked_fragment_modal.html.erb index 7dafc84..058b146 100644 --- a/app/views/branded/plans/plan_details/_linked_fragment_modal.html.erb +++ b/app/views/branded/plans/plan_details/_linked_fragment_modal.html.erb @@ -6,8 +6,8 @@
    - <%= form_for @fragment, url: create_or_update_structured_answers_path(@fragment.id, type: @type, plan_id: @plan.id), - html: {method: :post, remote: true, class: 'form-horizontal edit_plan', id: "structured_answer_form" } do |f| %> + <%= form_for @fragment, url: create_or_update_madmp_fragments_path(@fragment.id, type: @type, plan_id: @plan.id), + html: {method: :post, remote: true, class: 'form-horizontal edit_plan', id: "madmp_fragment_form" } do |f| %> <%= f.hidden_field :dmp_id, :value => @parent_fragment.dmp.id , id: nil %> <%= f.hidden_field :parent_id, :value => @parent_fragment.id , id: nil %> <%= f.hidden_field :id, id: nil %> diff --git a/app/views/branded/plans/plan_details/_plan_funding_form.html.erb b/app/views/branded/plans/plan_details/_plan_funding_form.html.erb index 0d9e0cd..e119f61 100644 --- a/app/views/branded/plans/plan_details/_plan_funding_form.html.erb +++ b/app/views/branded/plans/plan_details/_plan_funding_form.html.erb @@ -23,7 +23,7 @@ <%= create_select_field( f, fragment && fragment["funder"] && fragment["funder"]["funderId"] ? fragment["funder"]["funderId"]["idType"] : "ROR", - "structured_answer[funder[funderId[idType]]]", + "madmp_fragment[funder[funderId[idType]]]", _('Identifier Type'), ["ROR", "ISSNI", "RNSR", "FundRef"] ) @@ -55,7 +55,7 @@ <%= create_select_field( f, fragment && fragment["grantId"] ? fragment["grantId"]["idType"] : "DOI", - "structured_answer[grantId[idType]]", + "madmp_fragment[grantId[idType]]", _('Identifier Type'), ["DOI", "URL"] ) @@ -66,7 +66,7 @@ <%= create_select_field( f, fragment ? fragment["fundingStatus"] : "Planned", - "structured_answer[fundingStatus]", + "madmp_fragment[fundingStatus]", _('Funding Status'), ["Planned","Applied", "Granted", "Rejected"] ) diff --git a/app/views/branded/plans/plan_details/_plan_partner_form.html.erb b/app/views/branded/plans/plan_details/_plan_partner_form.html.erb index e5de7d9..aafc7cd 100644 --- a/app/views/branded/plans/plan_details/_plan_partner_form.html.erb +++ b/app/views/branded/plans/plan_details/_plan_partner_form.html.erb @@ -25,7 +25,7 @@ <%= create_select_field( f, fragment && fragment["orgId"] ? fragment["orgId"]["idType"] : "ROR", - "structured_answer[orgId[idType]]", + "madmp_fragment[orgId[idType]]", _('Identifier Type'), ["ROR", "ISSNI", "RNSR"] ) diff --git a/app/views/branded/questions/_new_edit_question_structured.erb b/app/views/branded/questions/_new_edit_question_structured.erb index e6298b1..539e6df 100644 --- a/app/views/branded/questions/_new_edit_question_structured.erb +++ b/app/views/branded/questions/_new_edit_question_structured.erb @@ -1,13 +1,13 @@ <%= f.label(:text, sanitize(question.text), class: 'control-label') unless question.nil? %> -<% data = answer.structured_answer.data unless answer.structured_answer.nil? %> -<% parent_id = answer.structured_answer.id unless answer.structured_answer.nil? %> -
    +<% data = answer.madmp_fragment.data unless answer.madmp_fragment.nil? %> +<% parent_id = answer.madmp_fragment.id unless answer.madmp_fragment.nil? %> +
    <%= render(partial: 'shared/dynamic_form/form', locals: { f: f, data: data, readonly: readonly, research_output: research_output, - schema: question.structured_data_schema.schema, + schema: question.madmp_schema.schema, parent_id: parent_id }) %>
    \ No newline at end of file diff --git a/app/views/branded/questions/fields/_schema_field.html.erb b/app/views/branded/questions/fields/_schema_field.html.erb index 802c189..74e0ac4 100644 --- a/app/views/branded/questions/fields/_schema_field.html.erb +++ b/app/views/branded/questions/fields/_schema_field.html.erb @@ -3,9 +3,9 @@ <%#= f.text_field field_name, value: field_value, multiple: multiple, class: 'form-control' %> <%= f.text_field field_name, value: field_value, multiple: multiple, class: 'form-control sub-fragment-id', id: 'children_id' %> <% if field_value&.empty? || field_value.nil? %> - <%= f.button _('Edit'), class: 'btn btn-default', data: { toggle: 'modal', target: '#sub-fragment-modal', open: "/structured_answers/new?schema_id=#{schema_id}" }, id: '#sub-fragment-modal-btn' %> + <%= f.button _('Edit'), class: 'btn btn-default', data: { toggle: 'modal', target: '#sub-fragment-modal', open: "/madmp_fragments/new?schema_id=#{schema_id}" }, id: '#sub-fragment-modal-btn' %> <% else %> - <%= f.button _('Edit'), class: 'btn btn-default', data: { toggle: 'modal', target: '#sub-fragment-modal', open: "/structured_answers/#{field_value}/edit" }, id: '#sub-fragment-modal-btn' %> + <%= f.button _('Edit'), class: 'btn btn-default', data: { toggle: 'modal', target: '#sub-fragment-modal', open: "/madmp_fragments/#{field_value}/edit" }, id: '#sub-fragment-modal-btn' %> <% end %> <% if multiple %> Add diff --git a/app/views/branded/shared/dynamic_form/_form.html.erb b/app/views/branded/shared/dynamic_form/_form.html.erb index 356bacd..a4b111a 100644 --- a/app/views/branded/shared/dynamic_form/_form.html.erb +++ b/app/views/branded/shared/dynamic_form/_form.html.erb @@ -22,7 +22,7 @@ <% when 'array' %> <% if prop['items']['type'] == 'object' %> <%= render(partial: 'shared/dynamic_form/fields/complex_multiple_field', locals: { - field_values: StructuredAnswer.where(parent_id: parent_id, classname: prop['items']['classname']), + field_values: MadmpFragment.where(parent_id: parent_id, classname: prop['items']['classname']), parent_id: parent_id, classname: prop['items']['classname'], field_name: field_name, answer_id: nil @@ -44,7 +44,7 @@ <%= field_name %> <% sub_schema = prop["classname"] ? - StructuredDataSchema.find_by(classname: prop["classname"]).schema : + MadmpSchema.find_by(classname: prop["classname"]).schema : prop %> <%= render(partial: 'shared/dynamic_form/form', locals: { diff --git a/app/views/branded/shared/dynamic_form/_linked_fragment.js.erb b/app/views/branded/shared/dynamic_form/_linked_fragment.js.erb index f77fb09..678cfba 100644 --- a/app/views/branded/shared/dynamic_form/_linked_fragment.js.erb +++ b/app/views/branded/shared/dynamic_form/_linked_fragment.js.erb @@ -3,7 +3,7 @@ $('#modal-window').modal(); - $('#structured_answer_form').bind('ajax:success', (event, response) => { + $('#madmp_fragment_form').bind('ajax:success', (event, response) => { $(`.fragment-${response.fragment_id} .linked-fragments-list tbody`).html(response.html); $('#modal-window').modal('hide'); }).bind('ajax:error', () => { diff --git a/app/views/branded/shared/dynamic_form/_modal.html.erb b/app/views/branded/shared/dynamic_form/_modal.html.erb index 61c262e..d3ca2d5 100644 --- a/app/views/branded/shared/dynamic_form/_modal.html.erb +++ b/app/views/branded/shared/dynamic_form/_modal.html.erb @@ -6,8 +6,8 @@
    - <%= form_for @fragment, url: create_or_update_structured_answers_path(@fragment.id, classname: @classname), - html: {method: :post, remote: true, class: 'form-horizontal', id: "structured_answer_form" } do |f| %> + <%= form_for @fragment, url: create_or_update_madmp_fragments_path(@fragment.id, classname: @classname), + html: {method: :post, remote: true, class: 'form-horizontal', id: "madmp_fragment_form" } do |f| %> <%= f.hidden_field :dmp_id, id: nil %> <%= f.hidden_field :parent_id, id: nil %> <%= f.hidden_field :id, id: nil %> diff --git a/app/views/branded/shared/dynamic_form/fields/_complex_multiple_field.html.erb b/app/views/branded/shared/dynamic_form/fields/_complex_multiple_field.html.erb index 5509820..f581728 100644 --- a/app/views/branded/shared/dynamic_form/fields/_complex_multiple_field.html.erb +++ b/app/views/branded/shared/dynamic_form/fields/_complex_multiple_field.html.erb @@ -16,7 +16,7 @@ <% unless parent_id.nil?%> - <%= link_to 'Create', new_edit_linked_fragment_structured_answers_url( + <%= link_to 'Create', new_edit_linked_fragment_madmp_fragments_url( nil, :parent_id => parent_id, :classname => classname diff --git a/app/views/branded/shared/dynamic_form/linked_fragment/_list.html.erb b/app/views/branded/shared/dynamic_form/linked_fragment/_list.html.erb index 25080a0..6e00ada 100644 --- a/app/views/branded/shared/dynamic_form/linked_fragment/_list.html.erb +++ b/app/views/branded/shared/dynamic_form/linked_fragment/_list.html.erb @@ -2,7 +2,7 @@ <%= obj.to_s %> - <%= link_to new_edit_linked_fragment_structured_answers_url( + <%= link_to new_edit_linked_fragment_madmp_fragments_url( nil, :fragment_id => obj.id, :parent_id => parent_id, @@ -17,7 +17,7 @@ + data-url="<%= madmp_fragment_path(obj.id) %>"><%= _('Delete') %> <% end %> \ No newline at end of file diff --git a/app/views/branded/shared/fragments/_person_fragment_form.html.erb b/app/views/branded/shared/fragments/_person_fragment_form.html.erb index 6b056fc..0813d9b 100644 --- a/app/views/branded/shared/fragments/_person_fragment_form.html.erb +++ b/app/views/branded/shared/fragments/_person_fragment_form.html.erb @@ -11,7 +11,7 @@ options_for_select( person_list.map { |p| [ p.to_s, p.id, - { "data-url" => get_fragment_structured_answers_path(p.id, plan_id: plan.id) } + { "data-url" => get_fragment_madmp_fragments_path(p.id, plan_id: plan.id) } ] }, selected: person_id), class: "form-control generic-fragment-picker", diff --git a/app/views/branded/super_admin/madmp_schemas/_form.html.erb b/app/views/branded/super_admin/madmp_schemas/_form.html.erb new file mode 100644 index 0000000..6c45976 --- /dev/null +++ b/app/views/branded/super_admin/madmp_schemas/_form.html.erb @@ -0,0 +1,33 @@ +<% url = @schema.new_record? ? super_admin_madmp_schemas_path : super_admin_madmp_schema_path(@schema) %> + + <%= form_for @schema, url: url, html: { class: 'schema' } do |f| %> +
    + <%= f.label(:label, _('Label'), class: 'control-label') %> + <%= f.text_field(:label, class: "form-control", spellcheck: true, "aria-required": true) %> +
    +
    + <%= f.label(:name, _('Name'), class: 'control-label') %> + <%= f.text_field(:name, class: "form-control", spellcheck: true, "aria-required": true) %> +
    +
    + <%= f.label(:name, _('Classname'), class: 'control-label') %> + <%= f.text_field(:classname, class: "form-control", spellcheck: true, "aria-required": true) %> +
    +
    + <%= f.label(:version, _('Version'), class: 'control-label') %> + <%= f.number_field(:version, class: "form-control", spellcheck: true, "aria-required": true) %> +
    +
    + <%= f.label(:schema, _('Schema')) %> + <%= f.text_area(:schema, class: "form-control", rows: 10) %> +
    +
    + <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %> + <% unless @schema.new_record? %> + <%= link_to(_('Delete'), super_admin_madmp_schema_path(@schema), class: 'btn btn-default', + rel: 'nofollow', 'data-method': 'delete', + 'data-confirm': _("Are you sure you want to delete the schema \"%{title}\"?") % { title: @schema.name }) %> + <% end %> + <%= link_to(_('Cancel'), super_admin_madmp_schemas_path, class: 'btn btn-default', role: 'button') %> +
    + <% end %> \ No newline at end of file diff --git a/app/views/branded/super_admin/madmp_schemas/edit.html.erb b/app/views/branded/super_admin/madmp_schemas/edit.html.erb new file mode 100644 index 0000000..b4a7c1d --- /dev/null +++ b/app/views/branded/super_admin/madmp_schemas/edit.html.erb @@ -0,0 +1,6 @@ +

    + <%= @schema.label %> + <%= link_to(_('View all schemas'), super_admin_madmp_schemas_path, + class: 'btn btn-default pull-right', role: 'button') %> +

    +<%= render partial: 'form' %> \ No newline at end of file diff --git a/app/views/branded/super_admin/madmp_schemas/index.html.erb b/app/views/branded/super_admin/madmp_schemas/index.html.erb new file mode 100644 index 0000000..dbafb29 --- /dev/null +++ b/app/views/branded/super_admin/madmp_schemas/index.html.erb @@ -0,0 +1,23 @@ +<%# Available locals: orgs %> +<% title _('Schemas') %> +
    +
    +

    + <%= _('Schemas') %> + <%= _('Create Schema') %> +

    +
    +
    +
    +
    + + <%= paginable_renderise( + partial: '/paginable/madmp_schemas/index', + controller: 'paginable/madmp_schemas', + action: 'index', + scope: madmp_schemas, + query_params: { sort_field: 'madmp_schemas.name', sort_direction: :asc }) %> +
    +
    +
    \ No newline at end of file diff --git a/app/views/branded/super_admin/madmp_schemas/new.html.erb b/app/views/branded/super_admin/madmp_schemas/new.html.erb new file mode 100644 index 0000000..33b244b --- /dev/null +++ b/app/views/branded/super_admin/madmp_schemas/new.html.erb @@ -0,0 +1,6 @@ +

    + <%= _("New schema") %> + <%= link_to(_('View all schemas'), super_admin_madmp_schemas_path, + class: 'btn btn-default pull-right', role: 'button') %> +

    +<%= render partial: 'form' %> \ No newline at end of file diff --git a/app/views/branded/super_admin/structured_data_schemas/_form.html.erb b/app/views/branded/super_admin/structured_data_schemas/_form.html.erb deleted file mode 100644 index 09b629c..0000000 --- a/app/views/branded/super_admin/structured_data_schemas/_form.html.erb +++ /dev/null @@ -1,33 +0,0 @@ -<% url = @schema.new_record? ? super_admin_structured_data_schemas_path : super_admin_structured_data_schema_path(@schema) %> - - <%= form_for @schema, url: url, html: { class: 'schema' } do |f| %> -
    - <%= f.label(:label, _('Label'), class: 'control-label') %> - <%= f.text_field(:label, class: "form-control", spellcheck: true, "aria-required": true) %> -
    -
    - <%= f.label(:name, _('Name'), class: 'control-label') %> - <%= f.text_field(:name, class: "form-control", spellcheck: true, "aria-required": true) %> -
    -
    - <%= f.label(:name, _('Classname'), class: 'control-label') %> - <%= f.text_field(:classname, class: "form-control", spellcheck: true, "aria-required": true) %> -
    -
    - <%= f.label(:version, _('Version'), class: 'control-label') %> - <%= f.number_field(:version, class: "form-control", spellcheck: true, "aria-required": true) %> -
    -
    - <%= f.label(:schema, _('Schema')) %> - <%= f.text_area(:schema, class: "form-control", rows: 10) %> -
    -
    - <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %> - <% unless @schema.new_record? %> - <%= link_to(_('Delete'), super_admin_structured_data_schema_path(@schema), class: 'btn btn-default', - rel: 'nofollow', 'data-method': 'delete', - 'data-confirm': _("Are you sure you want to delete the schema \"%{title}\"?") % { title: @schema.name }) %> - <% end %> - <%= link_to(_('Cancel'), super_admin_structured_data_schemas_path, class: 'btn btn-default', role: 'button') %> -
    - <% end %> \ No newline at end of file diff --git a/app/views/branded/super_admin/structured_data_schemas/edit.html.erb b/app/views/branded/super_admin/structured_data_schemas/edit.html.erb deleted file mode 100644 index a14f1fd..0000000 --- a/app/views/branded/super_admin/structured_data_schemas/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

    - <%= @schema.label %> - <%= link_to(_('View all schemas'), super_admin_structured_data_schemas_path, - class: 'btn btn-default pull-right', role: 'button') %> -

    -<%= render partial: 'form' %> \ No newline at end of file diff --git a/app/views/branded/super_admin/structured_data_schemas/index.html.erb b/app/views/branded/super_admin/structured_data_schemas/index.html.erb deleted file mode 100644 index a8fceb7..0000000 --- a/app/views/branded/super_admin/structured_data_schemas/index.html.erb +++ /dev/null @@ -1,23 +0,0 @@ -<%# Available locals: orgs %> -<% title _('Schemas') %> -
    -
    -

    - <%= _('Schemas') %> - <%= _('Create Schema') %> -

    -
    -
    -
    -
    - - <%= paginable_renderise( - partial: '/paginable/structured_data_schemas/index', - controller: 'paginable/structured_data_schemas', - action: 'index', - scope: structured_data_schemas, - query_params: { sort_field: 'structured_data_schemas.name', sort_direction: :asc }) %> -
    -
    -
    \ No newline at end of file diff --git a/app/views/branded/super_admin/structured_data_schemas/new.html.erb b/app/views/branded/super_admin/structured_data_schemas/new.html.erb deleted file mode 100644 index d1596fd..0000000 --- a/app/views/branded/super_admin/structured_data_schemas/new.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

    - <%= _("New schema") %> - <%= link_to(_('View all schemas'), super_admin_structured_data_schemas_path, - class: 'btn btn-default pull-right', role: 'button') %> -

    -<%= render partial: 'form' %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 95d154e..e980939 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -123,7 +123,7 @@ resources :research_outputs, only: [:index, :destroy], controller: 'research_outputs' end - resources :structured_answers, only: [:new, :edit, :create, :update, :destroy] do + resources :madmp_fragments, only: [:new, :edit, :create, :update, :destroy] do post 'create_or_update', on: :collection get 'new_edit_linked_fragment', on: :collection get 'get_fragment/:id', action: :get_fragment, on: :collection, as: :get_fragment @@ -223,7 +223,7 @@ get 'index/:page', action: :index, on: :collection, as: :index end # Paginable actions for structured data schemas - resources :structured_data_schemas, only: [] do + resources :madmp_schemas, only: [] do get 'index/:page', action: :index, on: :collection, as: :index end end @@ -291,7 +291,7 @@ namespace :super_admin do resources :orgs, only: [:index, :new, :create, :destroy] resources :themes, only: [:index, :new, :create, :edit, :update, :destroy] - resources :structured_data_schemas, only: [:index, :new, :create, :edit, :update, :destroy] + resources :madmp_schemas, only: [:index, :new, :create, :edit, :update, :destroy] resources :users, only: [:edit, :update] do member do put :merge diff --git a/db/migrate/20200615122747_rename_madmp_tables.rb b/db/migrate/20200615122747_rename_madmp_tables.rb new file mode 100644 index 0000000..7778819 --- /dev/null +++ b/db/migrate/20200615122747_rename_madmp_tables.rb @@ -0,0 +1,16 @@ +class RenameMadmpTables < ActiveRecord::Migration + def change + remove_foreign_key :structured_answers, :structured_data_schemas + remove_reference :structured_answers, :structured_data_schema + + remove_foreign_key :questions, :structured_data_schemas + remove_reference :questions, :structured_data_schema + + rename_table :structured_answers, :madmp_fragments + rename_table :structured_data_schemas, :madmp_schemas + + + add_reference :questions, :madmp_schema, index: true, foreign_key: true + add_reference :madmp_fragments, :madmp_schema, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b1d93ba..c8d1cf8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20200515113700) do +ActiveRecord::Schema.define(version: 20200615122747) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -119,6 +119,33 @@ t.boolean "default_language" end + create_table "madmp_fragments", force: :cascade do |t| + t.json "data" + t.integer "answer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "classname" + t.integer "dmp_id" + t.integer "parent_id" + t.integer "madmp_schema_id" + end + + add_index "madmp_fragments", ["answer_id"], name: "index_madmp_fragments_on_answer_id", using: :btree + add_index "madmp_fragments", ["madmp_schema_id"], name: "index_madmp_fragments_on_madmp_schema_id", using: :btree + + create_table "madmp_schemas", force: :cascade do |t| + t.string "label" + t.string "name" + t.integer "version" + t.json "schema" + t.integer "org_id" + t.string "classname" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "madmp_schemas", ["org_id"], name: "index_madmp_schemas_on_org_id", using: :btree + create_table "notes", force: :cascade do |t| t.integer "user_id" t.text "text" @@ -289,15 +316,15 @@ t.datetime "created_at" t.datetime "updated_at" t.integer "question_format_id" - t.boolean "option_comment_display", default: true + t.boolean "option_comment_display", default: true t.boolean "modifiable" - t.string "versionable_id", limit: 36 - t.integer "structured_data_schema_id" + t.string "versionable_id", limit: 36 + t.integer "madmp_schema_id" end + add_index "questions", ["madmp_schema_id"], name: "index_questions_on_madmp_schema_id", using: :btree add_index "questions", ["question_format_id"], name: "questions_question_format_id_idx", using: :btree add_index "questions", ["section_id"], name: "questions_section_id_idx", using: :btree - add_index "questions", ["structured_data_schema_id"], name: "index_questions_on_structured_data_schema_id", using: :btree add_index "questions", ["versionable_id"], name: "index_questions_on_versionable_id", using: :btree create_table "questions_themes", id: false, force: :cascade do |t| @@ -416,33 +443,6 @@ t.text "details" end - create_table "structured_answers", force: :cascade do |t| - t.json "data" - t.integer "answer_id" - t.integer "structured_data_schema_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "classname" - t.integer "dmp_id" - t.integer "parent_id" - end - - add_index "structured_answers", ["answer_id"], name: "index_structured_answers_on_answer_id", using: :btree - add_index "structured_answers", ["structured_data_schema_id"], name: "index_structured_answers_on_structured_data_schema_id", using: :btree - - create_table "structured_data_schemas", force: :cascade do |t| - t.string "label" - t.string "name" - t.integer "version" - t.json "schema" - t.integer "org_id" - t.string "classname" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - add_index "structured_data_schemas", ["org_id"], name: "index_structured_data_schemas_on_org_id", using: :btree - create_table "templates", force: :cascade do |t| t.string "title" t.text "description" @@ -556,6 +556,9 @@ add_foreign_key "answers_question_options", "question_options" add_foreign_key "guidance_groups", "orgs" add_foreign_key "guidances", "guidance_groups" + add_foreign_key "madmp_fragments", "answers" + add_foreign_key "madmp_fragments", "madmp_schemas" + add_foreign_key "madmp_schemas", "orgs" add_foreign_key "notes", "answers" add_foreign_key "notes", "users" add_foreign_key "notification_acknowledgements", "notifications" @@ -571,9 +574,9 @@ add_foreign_key "plans_guidance_groups", "guidance_groups" add_foreign_key "plans_guidance_groups", "plans" add_foreign_key "question_options", "questions" + add_foreign_key "questions", "madmp_schemas" add_foreign_key "questions", "question_formats" add_foreign_key "questions", "sections" - add_foreign_key "questions", "structured_data_schemas" add_foreign_key "questions_themes", "questions" add_foreign_key "questions_themes", "themes" add_foreign_key "research_outputs", "plans" @@ -583,9 +586,6 @@ add_foreign_key "sections", "phases" add_foreign_key "static_page_contents", "languages" add_foreign_key "static_page_contents", "static_pages" - add_foreign_key "structured_answers", "answers" - add_foreign_key "structured_answers", "structured_data_schemas" - add_foreign_key "structured_data_schemas", "orgs" add_foreign_key "templates", "orgs" add_foreign_key "themes_in_guidance", "guidances" add_foreign_key "themes_in_guidance", "themes" diff --git a/db/seeds.rb b/db/seeds.rb index f26c537..56db7b7 100755 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -844,5 +844,5 @@ f = File.open(path) d = JSON.load(f) n = File.basename(path, '.json') - StructuredDataSchema.create(label: n, name: n, version: 1, schema: d.to_json, org_id: nil, classname: nil) + MadmpSchema.create(label: n, name: n, version: 1, schema: d.to_json, org_id: nil, classname: nil) end \ No newline at end of file diff --git a/lib/dmpopidor/controllers/answers.rb b/lib/dmpopidor/controllers/answers.rb index 9295ec0..5f45cc8 100644 --- a/lib/dmpopidor/controllers/answers.rb +++ b/lib/dmpopidor/controllers/answers.rb @@ -53,7 +53,7 @@ s_params = schema_params(params, json_schema) data = data_reformater(json_schema.schema, s_params) parent_id = ResearchOutput.find(p_params[:research_output_id]).json_fragment().id - StructuredAnswer.save_structured_answer(@answer, data, json_schema, parent_id) + MadmpFragment.save_madmp_fragment(@answer, data, json_schema, parent_id) end if q.question_format.rda_metadata? @answer.update_answer_hash( @@ -77,7 +77,7 @@ s_params = schema_params(params, json_schema) data = data_reformater(json_schema.schema, s_params) parent_id = ResearchOutput.find(p_params[:research_output_id]).json_fragment().id - StructuredAnswer.save_structured_answer(@answer, data, json_schema, parent_id) + MadmpFragment.save_madmp_fragment(@answer, data, json_schema, parent_id) end rescue ActiveRecord::StaleObjectError @stale_answer = @answer @@ -161,7 +161,7 @@ # TODO: move to global var with before_action trigger + rename accordingly (set_json_schema ?) def json_schema question = Question.find(params['question_id']) - question.structured_data_schema + question.madmp_schema end # Get the parameters corresponding to the schema diff --git a/lib/dmpopidor/controllers/application.rb b/lib/dmpopidor/controllers/application.rb index a905cee..0aa0d59 100644 --- a/lib/dmpopidor/controllers/application.rb +++ b/lib/dmpopidor/controllers/application.rb @@ -17,7 +17,7 @@ Pref: _("preferences"), Department: _("department"), User: obj == current_user ? _("profile") : _("user"), - StructuredDataSchema: _("schema") + MadmpSchema: _("schema") } if obj.respond_to?(:customization_of) && obj.send(:customization_of).present? display_name[:Template] = "customization" diff --git a/lib/dmpopidor/controllers/org_admin/questions.rb b/lib/dmpopidor/controllers/org_admin/questions.rb index 8503bd9..6149f89 100644 --- a/lib/dmpopidor/controllers/org_admin/questions.rb +++ b/lib/dmpopidor/controllers/org_admin/questions.rb @@ -11,14 +11,14 @@ :question_options, section: { phase: :template }) .find(params[:id]) - structured_data_schemas = StructuredDataSchema.all + madmp_schemas = MadmpSchema.all authorize question render partial: "edit", locals: { template: question.section.phase.template, section: question.section, question: question, question_formats: allowed_question_formats, - structured_data_schemas: structured_data_schemas + madmp_schemas: madmp_schemas } end @@ -33,7 +33,7 @@ question_format: question_format, number: nbr.present? ? nbr + 1 : 1) question_formats = allowed_question_formats - structured_data_schemas = StructuredDataSchema.all + madmp_schemas = MadmpSchema.all authorize question render partial: "form", locals: { template: section.phase.template, @@ -45,7 +45,7 @@ phase_id: section.phase.id, id: section.id), question_formats: question_formats, - structured_data_schemas: structured_data_schemas + madmp_schemas: madmp_schemas } end @@ -55,7 +55,7 @@ def question_params params.require(:question) .permit(:number, :text, :question_format_id, :option_comment_display, - :default_value, :structured_data_schema_id, + :default_value, :madmp_schema_id, question_options_attributes: %i[id number text is_default _destroy], annotations_attributes: %i[id text org_id org type _destroy], theme_ids: []) diff --git a/lib/dmpopidor/controllers/plans.rb b/lib/dmpopidor/controllers/plans.rb index af5ec4e..71aa419 100644 --- a/lib/dmpopidor/controllers/plans.rb +++ b/lib/dmpopidor/controllers/plans.rb @@ -200,9 +200,9 @@ def new_edit_linked_fragment @plan = Plan.find(params[:id]) @type = params[:type] - @parent_fragment = StructuredAnswer.find(params[:parent_id]) - @schema = StructuredDataSchema.find_by(classname: @type) - @fragment = params[:fragment_id] ? StructuredAnswer.find(params[:fragment_id]) : StructuredAnswer.new + @parent_fragment = MadmpFragment.find(params[:parent_id]) + @schema = MadmpSchema.find_by(classname: @type) + @fragment = params[:fragment_id] ? MadmpFragment.find(params[:fragment_id]) : MadmpFragment.new authorize @plan respond_to do |format| format.html diff --git a/lib/dmpopidor/models/answer.rb b/lib/dmpopidor/models/answer.rb index 85cba72..031e39e 100644 --- a/lib/dmpopidor/models/answer.rb +++ b/lib/dmpopidor/models/answer.rb @@ -12,7 +12,7 @@ if question.question_format.option_based? return question_options.any? elsif question.question_format.structured - return !structured_answer.nil? + return !madmp_fragment.nil? else # (e.g. textarea or textfield question formats) return not(is_blank?) end @@ -25,7 +25,7 @@ def age if question.present? if question.question_format.structured - return structured_answer.updated_at.iso8601 + return madmp_fragment.updated_at.iso8601 else updated_at.iso8601 end diff --git a/lib/dmpopidor/models/plan.rb b/lib/dmpopidor/models/plan.rb index abed2df..019d82a 100644 --- a/lib/dmpopidor/models/plan.rb +++ b/lib/dmpopidor/models/plan.rb @@ -196,12 +196,12 @@ if person_fragment.nil? person_fragment = dmp_fragment.persons.create( data: person, - structured_data_schema_id: StructuredDataSchema.find_by(classname: "person").id + madmp_schema_id: MadmpSchema.find_by(classname: "person").id ) else person_fragment.update( data: person, - structured_data_schema_id: StructuredDataSchema.find_by(classname: "person").id + madmp_schema_id: MadmpSchema.find_by(classname: "person").id ) person_fragment.save! end diff --git a/spec/factories/madmp_fragments.rb b/spec/factories/madmp_fragments.rb new file mode 100644 index 0000000..c970346 --- /dev/null +++ b/spec/factories/madmp_fragments.rb @@ -0,0 +1,36 @@ +# == 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) +# + +FactoryBot.define do + factory :madmp_fragment do + data { { } } + classname { "dmp" } + answer + madmp_schema + + trait :data do + data { { } } + end + + trait :classname do + classname { "dmp" } + end + end +end diff --git a/spec/factories/madmp_schemas.rb b/spec/factories/madmp_schemas.rb new file mode 100644 index 0000000..a0d63b2 --- /dev/null +++ b/spec/factories/madmp_schemas.rb @@ -0,0 +1,29 @@ +# == 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) +# + +FactoryBot.define do + factory :madmp_schema do + label { "MyString" } + name { "MyString" } + version { 1 } + schema { "" } + org + classname { Faker::Company.bs } + end +end diff --git a/spec/factories/plans.rb b/spec/factories/plans.rb index 42ed2f9..e0e627c 100644 --- a/spec/factories/plans.rb +++ b/spec/factories/plans.rb @@ -85,7 +85,7 @@ end # after(:create) do |plan, evaluator| - # create(:structured_answer, classname: "dmp", data: { "plan_id" => plan.id }) + # create(:madmp_fragment, classname: "dmp", data: { "plan_id" => plan.id }) # end end diff --git a/spec/factories/questions.rb b/spec/factories/questions.rb index 2c6b6e5..c14f30c 100644 --- a/spec/factories/questions.rb +++ b/spec/factories/questions.rb @@ -13,11 +13,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) diff --git a/spec/factories/structured_answers.rb b/spec/factories/structured_answers.rb deleted file mode 100644 index 93d8857..0000000 --- a/spec/factories/structured_answers.rb +++ /dev/null @@ -1,36 +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) -# - -FactoryBot.define do - factory :structured_answer do - data { { } } - classname { "dmp" } - answer - structured_data_schema - - trait :data do - data { { } } - end - - trait :classname do - classname { "dmp" } - end - end -end diff --git a/spec/factories/structured_data_schemas.rb b/spec/factories/structured_data_schemas.rb deleted file mode 100644 index de359e5..0000000 --- a/spec/factories/structured_data_schemas.rb +++ /dev/null @@ -1,29 +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) -# - -FactoryBot.define do - factory :structured_data_schema do - label { "MyString" } - name { "MyString" } - version { 1 } - schema { "" } - org - classname { Faker::Company.bs } - end -end diff --git a/spec/models/madmp_fragment_spec.rb b/spec/models/madmp_fragment_spec.rb new file mode 100644 index 0000000..7054cad --- /dev/null +++ b/spec/models/madmp_fragment_spec.rb @@ -0,0 +1,36 @@ +require 'rails_helper' + +RSpec.describe MadmpFragment, type: :model do + + context "validations" do + + it { is_expected.to validate_presence_of(:madmp_schema) } + + end + + context "associations" do + + it { is_expected.to belong_to :madmp_schema } + + it { is_expected.to belong_to :answer } + + end + + # describe ".update_parent_references" do + + # it "should be called after a structured answer is created" do + + # parent_answer = FactoryBot.create(:madmp_fragment, classname: "dmp") + + # subject = Fragment::ResearchOutput.new + + # subject.parent_id = parent_answer.id + # subject.dmp_id = parent_answer.id + + # expect(subject).to receive(:update_parent_references) + + # subject.save + + # end + # end +end diff --git a/spec/models/madmp_schema_spec.rb b/spec/models/madmp_schema_spec.rb new file mode 100644 index 0000000..e25c8a0 --- /dev/null +++ b/spec/models/madmp_schema_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe MadmpSchema, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/structured_answer_spec.rb b/spec/models/structured_answer_spec.rb deleted file mode 100644 index b91ade4..0000000 --- a/spec/models/structured_answer_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'rails_helper' - -RSpec.describe StructuredAnswer, type: :model do - - context "validations" do - - it { is_expected.to validate_presence_of(:structured_data_schema) } - - end - - context "associations" do - - it { is_expected.to belong_to :structured_data_schema } - - it { is_expected.to belong_to :answer } - - end - - # describe ".update_parent_references" do - - # it "should be called after a structured answer is created" do - - # parent_answer = FactoryBot.create(:structured_answer, classname: "dmp") - - # subject = Fragment::ResearchOutput.new - - # subject.parent_id = parent_answer.id - # subject.dmp_id = parent_answer.id - - # expect(subject).to receive(:update_parent_references) - - # subject.save - - # end - # end -end diff --git a/spec/models/structured_data_schema_spec.rb b/spec/models/structured_data_schema_spec.rb deleted file mode 100644 index 6e7886a..0000000 --- a/spec/models/structured_data_schema_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.describe StructuredDataSchema, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/test.rb b/test.rb index fd8797a..1e8ab47 100644 --- a/test.rb +++ b/test.rb @@ -4,8 +4,8 @@ f = File.open(path) d = JSON.load(f) n = File.basename(path, '.json') - StructuredDataSchema.create(label: n, name: n, version: 1, schema: d.to_json, org_id: nil, classname: nil) + MadmpSchema.create(label: n, name: n, version: 1, schema: d.to_json, org_id: nil, classname: nil) end -Question.find(19051).update(question_format: qf, structured_data_schema: StructuredDataSchema.find_by(name: 'simple')) -Question.find(19052).update(question_format: qf, structured_data_schema: StructuredDataSchema.find_by(name: 'occ_simple')) -Question.find(19053).update(question_format: qf, structured_data_schema: StructuredDataSchema.find_by(name: 'occ_struct')) \ No newline at end of file +Question.find(19051).update(question_format: qf, madmp_schema: MadmpSchema.find_by(name: 'simple')) +Question.find(19052).update(question_format: qf, madmp_schema: MadmpSchema.find_by(name: 'occ_simple')) +Question.find(19053).update(question_format: qf, madmp_schema: MadmpSchema.find_by(name: 'occ_struct')) \ No newline at end of file