diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index cc4bd8f..4679b53 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -10,11 +10,10 @@ ## # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 - attr_accessible :organisation_id, :name, :optional_subset, :published, - :org, :as => [:default, :admin] - attr_accessible :dmptemplate_ids, :as => [:default, :admin] + attr_accessible :org_id, :name, :optional_subset, :published, :org, + :as => [:default, :admin] - + validates :name, :org, presence: true # EVALUATE CLASS AND INSTANCE METHODS BELOW @@ -24,16 +23,7 @@ - - validates :name, :org, presence: true - - ## - # Converts a guidance group to a string containing the display name - # - # @return [String] the name of the organisation, with or without the name of the guidance group - def to_s - "#{display_name}" - end + ## # Converts the current guidance group to a string containing the display name. @@ -42,27 +32,33 @@ # organisation followed by the name of the guidance group. # # @return [String] the display name for the guidance group - def display_name - if organisation.guidance_groups.count > 1 - return "#{organisation.name}: #{name}" - else - return organisation.name - end - end + def display_name + if org.guidance_groups.count > 1 + return "#{org.name}: #{name}" + else + return org.name + end + end ## # Returns the list of all guidance groups not coming from the given organisations # # @param excluded_orgs [Array] a list of organisations to exclude in the result # @return [Array] a list of guidance groups - def self.guidance_groups_excluding(excluded_orgs) - excluded_org_ids = Array.new - excluded_orgs.each do |org| - excluded_org_ids << org.id - end - return_orgs = GuidanceGroup.where("organisation_id NOT IN (?)", excluded_org_ids) - return return_orgs - end + def self.guidance_groups_excluding(excluded_orgs) + excluded_org_ids = Array.new + + if excluded_orgs.is_a?(Array) + excluded_orgs.each do |org| + excluded_org_ids << org.id + end + else + excluded_org_ids << excluded_orgs + end + + return_orgs = GuidanceGroup.where("org_id NOT IN (?)", excluded_org_ids) + return return_orgs + end ## # Returns whether or not a given user can view a given guidance group @@ -79,17 +75,17 @@ viewable = false # groups are viewable if they are owned by any of the user's organisations - if guidance_group.organisation == user.organisation + if guidance_group.org == user.org viewable = true end # groups are viewable if they are owned by the managing curation center Org.where( name: GlobalHelpers.constant("organisation_types.managing_organisation")).find_each do |managing_group| - if guidance_group.organisation.id == managing_group.id + if guidance_group.org.id == managing_group.id viewable = true end end # groups are viewable if they are owned by a funder - if guidance_group.organisation.organisation_type == OrganisationType.find_by( name: GlobalHelpers.constant("organisation_types.funder")) + if guidance_group.org.org_type == 2 viewable = true end @@ -108,17 +104,17 @@ def self.all_viewable(user) # first find all groups owned by the Managing Curation Center managing_org_groups = [] - Org.where( name: GlobalHelpers.constant("organisation_types.managing_organisation")).find_each do |managing_org| + Org.where(name: GlobalHelpers.constant("organisation_types.managing_organisation")).find_each do |managing_org| managing_org_groups = managing_org_groups + managing_org.guidance_groups end # find all groups owned by a Funder organisation funder_groups = [] - funders = OrganisationType.find_by( name: GlobalHelpers.constant("organisation_types.funder")) - funders.organisations.each do |funder| + funders = Org.where(org_type: 2) + funders.each do |funder| funder_groups = funder_groups + funder.guidance_groups end - organisation_groups = [user.organisation.guidance_groups] + organisation_groups = [user.org.guidance_groups] # pass this organisation guidance groups to the view with respond_with @all_viewable_groups all_viewable_groups = managing_org_groups + funder_groups + organisation_groups diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb index d768e1d..a7ac719 100644 --- a/test/unit/guidance_group_test.rb +++ b/test/unit/guidance_group_test.rb @@ -4,85 +4,74 @@ include GlobalHelpers setup do - Organisation.create(name: GlobalHelpers.constant("organisation_types.managing_organisation")) - @user = User.first - @organisation = Organisation.first + @org = Org.last -<<<<<<< HEAD - @guidance_group = GuidanceGroup.create(name: 'Test Guidance Group', - organisation: @organisation) -======= - @organisations = Org.all ->>>>>>> final_schema + @guidance_group = GuidanceGroup.create(name: 'Test Guidance Group', org: @org, + optional_subset: false, published: true) end # --------------------------------------------------- test "required fields are required" do assert_not GuidanceGroup.new.valid? - assert_not GuidanceGroup.new(organisation: @organisation).valid?, "expected the 'name' field to be required" + assert_not GuidanceGroup.new(org: @org).valid?, "expected the 'name' field to be required" assert_not GuidanceGroup.new(name: 'Tester').valid?, "expected the 'organisation' field to be required" # Ensure the bar minimum and complete versions are valid - a = GuidanceGroup.new(name: 'Tester', organisation: @organisation) + a = GuidanceGroup.new(name: 'Tester', org: @org) assert a.valid?, "expected the 'name' and 'organisation' fields to be enough to create a GuidanceGroup! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end # --------------------------------------------------- test "display_name returns organisation name and the guidance group name" do - assert_equal "#{@organisation.name}", @guidance_group.display_name, "Expected display_name to return the organisation name if there is only one GuidanceGroup" + assert_equal "#{@org.name}", @guidance_group.display_name, "Expected display_name to return the organisation name if there is only one GuidanceGroup" - GuidanceGroup.create(name: 'Second Test', organisation: @organisation) - assert_equal "#{@organisation.name}: #{@guidance_group.name}", @guidance_group.display_name, "Expected display_name to return the organisation name and guidance group name if there are more than one GuidanceGroup" - end - - # --------------------------------------------------- - test "to_s returns organisation name and the guidance group name" do - assert_equal @guidance_group.display_name, @guidance_group.to_s + GuidanceGroup.create(name: 'Second Test', org: @org) + assert_equal "#{@org.name}: #{@guidance_group.name}", @guidance_group.display_name, "Expected display_name to return the organisation name and guidance group name if there are more than one GuidanceGroup" end # --------------------------------------------------- test "guidance_groups_excluding does not return guidance groups for the current organisation" do - assert_not GuidanceGroup.guidance_groups_excluding([@organisation]).include?(@guidance_group) + assert_not GuidanceGroup.guidance_groups_excluding([@org]).include?(@guidance_group) end # --------------------------------------------------- test "user can view guidance_group if it belongs to their organisation" do - org = @user.organisation - gg = GuidanceGroup.create(name: 'User Test', organisation: org) + org = @user.org + gg = GuidanceGroup.create(name: 'User Test', org: org) assert GuidanceGroup.can_view?(@user, gg.id) end # --------------------------------------------------- test "user can view guidance_group if it belongs to a funder" do - org = Organisation.find_by(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))) - gg = GuidanceGroup.create(name: 'Funder Test', organisation: org) + org = Org.find_by(org_type: 2) + gg = GuidanceGroup.create(name: 'Funder Test', org: org) assert GuidanceGroup.can_view?(@user, gg.id) end # --------------------------------------------------- test "user can view guidance_group if it belongs to the managing curation centre" do - org = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) - gg = GuidanceGroup.create(name: 'Managing CC Test', organisation: org) + org = Org.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + gg = GuidanceGroup.create(name: 'Managing CC Test', org: org) assert GuidanceGroup.can_view?(@user, gg.id) end # --------------------------------------------------- test "user can view all oftheir organisations, funders, and the managing curation centre's guidance groups" do - @organisation.users << @user - @organisation.save - @organisation.reload + @org.users << @user + @org.save + @org.reload - funding = Organisation.where(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))).first - managing = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + funding = Org.where(org_type: 2).first + managing = Org.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) ggs = [@guidance_group, - GuidanceGroup.create(name: 'User Test', organisation: @organisation), - GuidanceGroup.create(name: 'Funder Test', organisation: funding), - GuidanceGroup.create(name: 'Managing CC Test', organisation: managing)] + GuidanceGroup.create(name: 'User Test', org: @org), + GuidanceGroup.create(name: 'Funder Test', org: funding), + GuidanceGroup.create(name: 'Managing CC Test', org: managing)] v = GuidanceGroup.all_viewable(@user) @@ -93,7 +82,7 @@ # --------------------------------------------------- test "can CRUD GuidanceGroup" do - gg = GuidanceGroup.create(name: 'Tester', organisation: @organisation) + gg = GuidanceGroup.create(name: 'Tester', org: @org) assert_not gg.id.nil?, "was expecting to be able to create a new GuidanceGroup!" gg.name = 'Testing an update' @@ -105,26 +94,14 @@ end # --------------------------------------------------- - test "can manage has_many relationship with Project" do - proj = Project.new(title: 'Test Project', dmptemplate: Dmptemplate.first) - verify_has_many_relationship(@guidance_group, proj, @guidance_group.projects.count) - end - - # --------------------------------------------------- - test "can manage has_many relationship with Template" do - t = Dmptemplate.new(title: 'Test Theme', organisation: @organisation) - verify_has_many_relationship(@guidance_group, t, @guidance_group.dmptemplates.count) - end - - # --------------------------------------------------- test "can manage has_many relationship with Guidance" do g = Guidance.new(text: 'Test Guidance') verify_has_many_relationship(@guidance_group, g, @guidance_group.guidances.count) end # --------------------------------------------------- - test "can manage belongs_to relationship with Organisation" do - verify_belongs_to_relationship(@guidance_group, @organisation) + test "can manage belongs_to relationship with Org" do + verify_belongs_to_relationship(@guidance_group, @org) end end