diff --git a/app/models/guidance.rb b/app/models/guidance.rb index 76f8cba..0aaee5a 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -8,27 +8,31 @@ class Guidance < ActiveRecord::Base - #associations between tables + #associations between tables attr_accessible :text, :question_id, :published, :as => [:default, :admin] - attr_accessible :guidance_group_ids, :as => [:default, :admin] - attr_accessible :theme_ids, :as => [:default, :admin] + attr_accessible :guidance_group_ids, :as => [:default, :admin] + attr_accessible :theme_ids, :as => [:default, :admin] - belongs_to :question + belongs_to :question - #belongs_to :dmptemplate - #belongs_to :theme + #belongs_to :dmptemplate + #belongs_to :theme - has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" - has_and_belongs_to_many :themes, join_table: "themes_in_guidance" + has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" + has_and_belongs_to_many :themes, join_table: "themes_in_guidance" - accepts_nested_attributes_for :themes - accepts_nested_attributes_for :guidance_groups + accepts_nested_attributes_for :themes + accepts_nested_attributes_for :guidance_groups - #verifies if one guidance belongs to a org + ## + # Determine if a guidance is in a group which belongs to a specified organisation + # + # @param org_id [Integer] the integer id for an organisation + # @return [Boolean] true if this guidance is in a group belonging to the specified organisation, false otherwise def in_group_belonging_to?(organisation_id) guidance_groups.each do |guidance_group| - if guidance_group.organisation_id == organisation_id then + if guidance_group.organisation_id == organisation_id return true end end @@ -36,13 +40,17 @@ end - #all guidance that belong to an organisation + ## + # returns all guidance that belongs to a specified organisation + # + # @param org_id [Integer] the integer id for an organisation + # @return [Array] list of guidance def self.by_organisation(org_id) all_guidance = Guidance.all org_guidance = Array.new all_guidance.each do |guidance| - if guidance.in_group_belonging_to?(org_id) then + if guidance.in_group_belonging_to?(org_id) org_guidance << guidance end end @@ -51,18 +59,30 @@ end - + ## + # returns all templates belgonging to a specified guidance group + # + # @param guidance_group [Integer] the integer id for an guidance_group + # @return [Array] list of templates def get_guidance_group_templates? (guidance_group) + # DISCUSS - here we have yet another way of finding a specific or group of + # an object. Would it make sense to standardise the project by only using + # either finders or where, or alteast the same syntax within the where statement templates = guidancegroups.where("guidance_group_id (?)", guidance_group.id).template return templates end - def self.can_view(user, id) - # we define guidances viewable to a user by those owned by a guidance group: - # owned by the DCC - # owned by a funder organisation - # owned by an organisation, of which the user is a member - + ## + # Returns whether or not a given user can view a given guidance + # we define guidances viewable to a user by those owned by a guidance group: + # owned by the DCC + # owned by a funder organisation + # owned by an organisation, of which the user is a member + # + # @param id [Integer] the integer id for a guidance + # @param user [User] a user object + # @return [Boolean] true if the specified user can view the specified guidance, false otherwise + def self.can_view?(user, id) guidance = Guidance.find_by(id: id) viewable = false @@ -91,6 +111,15 @@ return viewable end + ## + # Returns a list of all guidances which a specified user can view + # we define guidances viewable to a user by those owned by a guidance group: + # owned by the DCC + # owned by a funder organisation + # owned by an organisation, of which the user is a member + # + # @param user [User] a user object + # @return [Array] a list of all "viewable" guidances to a user def self.all_viewable(user) # we define vuiable guidances as those owned by a guidance group: # owned by the DCC @@ -103,18 +132,18 @@ funder_groups = [] funders = OrganisationType.find_by( name: "Funder") funders.organisations.each do |funder| - funder_groups = funder_groups + funder.guidance_groups + funder_groups += funder.guidance_groups end # find all groups owned by any of the user's organisations organisation_groups = [] user.organisations.each do |organisation| - organisation_groups = organisation_groups + organisation.guidance_groups + organisation_groups += organisation.guidance_groups end # find all guidances belonging to any of the viewable groups all_viewable_guidances = [] all_viewable_groups = dcc_groups + funder_groups + organisation_groups all_viewable_groups.each do |group| - all_viewable_guidances = all_viewable_guidances + group.guidances + all_viewable_guidances += group.guidances end # pass the list of viewable guidances to the view return all_viewable_guidances