diff --git a/app/models/exported_plan.rb b/app/models/exported_plan.rb index 3782b52..f2707f3 100644 --- a/app/models/exported_plan.rb +++ b/app/models/exported_plan.rb @@ -1,7 +1,8 @@ class ExportedPlan < ActiveRecord::Base include GlobalHelpers - attr_accessible :plan_id, :user_id, :format, :as => [:default, :admin] +# TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! + attr_accessible :plan_id, :user_id, :format, :user, :plan, :as => [:default, :admin] #associations between tables belongs_to :plan @@ -10,6 +11,7 @@ VALID_FORMATS = ['csv', 'html', 'json', 'pdf', 'text', 'xml', 'docx'] validates :format, inclusion: { in: VALID_FORMATS, message: I18n.t('helpers.plan.export.not_valid_format') } + validates :plan, :user, :format, presence: true # Store settings with the exported plan so it can be recreated later # if necessary (otherwise the settings associated with the plan at a @@ -18,6 +20,9 @@ s.key :export, defaults: Settings::Dmptemplate::DEFAULT_SETTINGS end +# TODO: Consider removing the accessor methods, they add no value. The view/controller could +# just access the value directly from the project/plan: exported_plan.plan.project.title + # Getters to match Settings::Dmptemplate::VALID_ADMIN_FIELDS def project_name name = self.plan.project.title diff --git a/app/models/plan.rb b/app/models/plan.rb index f25998c..96d3aea 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -1,6 +1,7 @@ class Plan < ActiveRecord::Base - attr_accessible :locked, :project_id, :version_id, :version, :plan_sections, :as => [:default, :admin] + attr_accessible :locked, :project_id, :version_id, :version, :plan_sections, + :Exported_plans, :as => [:default, :admin] A4_PAGE_HEIGHT = 297 #(in mm) A4_PAGE_WIDTH = 210 #(in mm) @@ -13,6 +14,7 @@ belongs_to :version has_many :answers has_many :plan_sections + has_many :exported_plans # accepts_nested_attributes_for :project accepts_nested_attributes_for :answers diff --git a/app/models/user.rb b/app/models/user.rb index 398c264..cfba711 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,7 +5,7 @@ # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :recoverable, - :rememberable, :trackable, :validatable, :confirmable, + :rememberable, :trackable, :validatable, :confirmable, :exported_plans, :omniauthable, omniauth_providers: [:orcid] #associations between tables @@ -15,7 +15,7 @@ has_many :user_org_roles has_many :project_groups, :dependent => :destroy has_many :user_role_types, through: :user_org_roles - + has_many :exported_plans has_many :user_identifiers has_many :identifier_schemes, through: :user_identifiers diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 88ef51b..c4c1f20 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -19,11 +19,6 @@ qs = template.phases.first.versions.first.sections.first.questions @text_area_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Area' ) }.first - @text_field_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Field' ) }.first - @radio_button_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Radio Button' ) }.first - @check_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Check Box' ) }.first - @select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Dropdown' ) }.first - @multi_select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Multi Select Box' ) }.first end # --------------------------------------------------- @@ -40,7 +35,7 @@ end # --------------------------------------------------- - test "can CRUD answers for text based questions" do + test "can CRUD Comment" do [@text_area_question, @text_field_question].each do |q| cmnt = Comment.create(user: @user, plan: @plan, question: q, text: 'Tested ABC') assert_not cmnt.id.nil?, "was expecting to be able to create a new Comment for a #{q.question_format.title} question: #{cmnt.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" diff --git a/test/unit/dmptemplate_test.rb b/test/unit/dmptemplate_test.rb index 32ee18d..6e362fd 100644 --- a/test/unit/dmptemplate_test.rb +++ b/test/unit/dmptemplate_test.rb @@ -248,7 +248,7 @@ end # --------------------------------------------------- - test "can CRUD answers for text based questions" do + test "can CRUD Template" do tmplt = Dmptemplate.create(organisation: @organisation, title: 'Testing tmeplate') assert_not tmplt.id.nil?, "was expecting to be able to create a new Dmptemplate!" diff --git a/test/unit/exported_plan_test.rb b/test/unit/exported_plan_test.rb new file mode 100644 index 0000000..fcb163a --- /dev/null +++ b/test/unit/exported_plan_test.rb @@ -0,0 +1,72 @@ +require 'test_helper' + +class ExportedPlanTest < ActiveSupport::TestCase + + setup do + @user = User.last + + # generate a template and plan + template = generate_complete_template + + project = Project.new({ + title: 'Test Project', + organisation: @user.organisation + }) + project.dmptemplate = template + project.save! + + @plan = project.plans.first + + @exported = ExportedPlan.create(user: @user, plan: @plan, format: ExportedPlan::VALID_FORMATS.first) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not ExportedPlan.new.valid? + assert_not ExportedPlan.new(user: @user, format: ExportedPlan::VALID_FORMATS.last).valid?, "expected the 'plan' field to be required" + assert_not ExportedPlan.new(plan: @plan, format: ExportedPlan::VALID_FORMATS.last).valid?, "expected the 'user' field to be required" + assert_not ExportedPlan.new(user: @user, plan: @plan).valid?, "expected the 'format' field to be required" + + # Ensure the bar minimum and complete versions are valid + a = ExportedPlan.new(user: @user, plan: @plan, format: ExportedPlan::VALID_FORMATS.last) + assert a.valid?, "expected the 'plan', 'user' and 'format' fields to be enough to create an ExportedPlan! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "as_csv" do + + end + + # --------------------------------------------------- + test "as_txt" do + + end + + # --------------------------------------------------- + test "can CRUD ExportedPlan" do + ExportedPlan::VALID_FORMATS.each do |vf| + ep = ExportedPlan.create(user: @user, plan: @plan, format: vf) + assert_not ep.id.nil?, "was expecting to be able to create a new ExportedPlan: #{ep.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + expected = (vf == ExportedPlan::VALID_FORMATS.last ? ExportedPlan::VALID_FORMATS.first : ExportedPlan::VALID_FORMATS.last) + + ep.format = expected + ep.save! + ep.reload + assert_equal expected, ep.format, "Was expecting to be able to update the format of the ExportedPlan!" + + assert ep.destroy!, "Was unable to delete the ExportedPlan!" + end + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Plan" do + verify_belongs_to_relationship(@exported, Plan.last) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with User" do + verify_belongs_to_relationship(@exported, User.last) + end + +end