diff --git a/app/models/plan.rb b/app/models/plan.rb index eba48ae..41b27d2 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -6,11 +6,13 @@ has_many :sections, through: :phases has_many :questions, through: :sections has_many :answers - has_many :roles has_many :notes, through: :answers - has_many :users, through: :roles has_many :exported_plans + has_many :roles + has_many :users, through: :roles + + ## # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 @@ -41,9 +43,6 @@ end alias_method :super_settings, :settings - - - diff --git a/app/models/role.rb b/app/models/role.rb index 73048cc..7bbe072 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,6 +1,8 @@ class Role < ActiveRecord::Base include FlagShihTzu + before_validation :check_access_level + ## # Associations belongs_to :user @@ -15,6 +17,7 @@ column: 'access' validates :user, :plan, :access, presence: true + validates :access, numericality: {greater_than: 0} ## # return the access level for the current project group @@ -51,5 +54,11 @@ else self.editor = false end + self.creator = true unless self.administrator? || self.editor? + end + + # Ensures that the access attribute is set + def check_access_level + self.access_level = self.access_level end end diff --git a/app/models/section.rb b/app/models/section.rb index 60fd094..4b484ab 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -14,6 +14,8 @@ :questions_attributes, :organisation, :phase, :modifiable, :as => [:default, :admin] + validates :phase, :title, :number, presence: true + ## # return the title of the section # diff --git a/test/unit/role_test.rb b/test/unit/role_test.rb index 7797783..3530e79 100644 --- a/test/unit/role_test.rb +++ b/test/unit/role_test.rb @@ -7,27 +7,28 @@ scaffold_plan - @role = Role.create(user: User.first, plan: @plan, access: 1) + @role = Role.create(user: User.first, plan: @plan) end # --------------------------------------------------- test "required fields are required" do assert_not Role.new.valid? - assert_not Role.new(user: @user, plan: Plan.first).valid?, "expected the 'access' field to be required" assert_not Role.new(plan: Plan.first, access: 1).valid?, "expected the 'user' field to be required" assert_not Role.new(user: @user, access: 1).valid?, "expected the 'plan' field to be required" # Ensure the bar minimum and complete versions are valid plan = Plan.create(title: 'Test Plan', template: Template.last) - a = Role.new(user: @user, plan: plan, access: 1) + a = Role.new(user: @user, plan: plan) assert a.valid?, "expected the 'user', 'plan' and 'access' fields to be enough to create an Role! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end # --------------------------------------------------- + test "access is properly defaulted" do + assert_equal 1, @role.access_level + end + + # --------------------------------------------------- test "access_level acts a proxy to the 'access' FlagShihTzu bit flag field" do - - puts @role.inspect - assert @role.creator?, "expected the role to be creator" @role.administrator = true @@ -41,13 +42,13 @@ # --------------------------------------------------- test "can CRUD Role" do plan = Plan.create(title: 'Test Plan', template: Template.last) - obj = Role.create(user: @user, plan: plan, access: 1) + obj = Role.create(user: @user, plan: plan, access_level: 1) assert_not obj.id.nil?, "was expecting to be able to create a new Role: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" - obj.access = 2 + obj.access_level = 2 obj.save! obj.reload - assert_equal 2, obj.access, "Was expecting to be able to update the text of the Role!" + assert_equal 2, obj.access_level, "Was expecting to be able to update the text of the Role!" assert obj.destroy!, "Was unable to delete the Role!" end diff --git a/test/unit/section_test.rb b/test/unit/section_test.rb new file mode 100644 index 0000000..f827044 --- /dev/null +++ b/test/unit/section_test.rb @@ -0,0 +1,53 @@ +require 'test_helper' + +class SectionTest < ActiveSupport::TestCase + + setup do + scaffold_template + + @section = Section.create(title: 'Test Section', description: 'My test section', number: 99, + published: true, phase: @template.phases.first, modifiable: false) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Section.new.valid? + assert_not Section.new(phase: @template.phases.last, number: 9).valid?, "expected the 'title' field to be required" + assert_not Section.new(title: 'Tester', number: 9).valid?, "expected the 'phase' field to be required" + assert_not Section.new(phase: @template.phases.last, title: 'Tester').valid?, "expected the 'number' field to be required" + + # Ensure the bare minimum and complete versions are valid + a = Section.new(phase: @template.phases.last, title: 'Tester', number: 9) + assert a.valid?, "expected the 'phase', 'title' and 'number' fields to be enough to create an Section! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the title" do + assert_equal @section.title, @section.to_s + end + + # --------------------------------------------------- + test "can CRUD Section" do + obj = Section.create(phase: @template.phases.last, title: 'Tester', number: 9) + assert_not obj.id.nil?, "was expecting to be able to create a new Section: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.description = 'my tester' + obj.save! + obj.reload + assert_equal 'my tester', obj.description, "Was expecting to be able to update the description of the Section!" + + assert obj.destroy!, "Was unable to delete the Section!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Phase" do + section = Section.new(title: 'Tester', number: 99) + verify_belongs_to_relationship(section, @template.phases.first) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Question" do + question = Question.new(text: 'Testing', number: 1) + verify_has_many_relationship(@section, question, @section.questions.count) + end +end \ No newline at end of file