diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 0aa0c88..0828218 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -1,101 +1,148 @@ class Organisation < ActiveRecord::Base - #associations between tables - belongs_to :organisation_type - has_many :guidance_groups - has_many :dmptemplates - has_many :sections - has_many :users - has_many :option_warnings - has_many :suggested_answers - has_many :org_token_permissions + #associations between tables + belongs_to :organisation_type + has_many :guidance_groups + has_many :dmptemplates + has_many :sections + has_many :users, through: :user_org_roles + has_many :option_warnings + has_many :suggested_answers + has_and_belongs_to_many :token_permission_types, join_table: "org_token_permissions" has_many :user_org_roles belongs_to :parent, :class_name => 'Organisation' - has_many :children, :class_name => 'Organisation', :foreign_key => 'parent_id' + has_many :children, :class_name => 'Organisation', :foreign_key => 'parent_id' - accepts_nested_attributes_for :organisation_type - accepts_nested_attributes_for :dmptemplates + accepts_nested_attributes_for :organisation_type + accepts_nested_attributes_for :dmptemplates + accepts_nested_attributes_for :token_permission_types - attr_accessible :abbreviation, :banner_text, :description, :domain, :logo_file_name, :name, :stylesheet_file_id, :target_url, :organisation_type_id, :wayfless_entity, :parent_id, :sort_name + attr_accessible :abbreviation, :banner_text, :description, :domain, :logo_file_name, :name, :stylesheet_file_id, :target_url, :organisation_type_id, :wayfless_entity, :parent_id, :sort_name, :token_permission_type_ids + ## + # returns the name of the current organisation + # + # @return [String] the name of the organisation + def to_s + name + end - def to_s - name - end + ## + # returns the abbreviation for the organisation if it exists, name otherwise + # + # @return [String] abbreviation or name + def short_name + if abbreviation.nil? then + return name + else + return abbreviation + end + end - def short_name - if abbreviation.nil? then - return name - else - return abbreviation - end - end + ## + # returns all organisations who have parents of a given organisation type + # + # @params org_type [String] the name of an organisation type + # @return [Array] a list of organisations + def self.orgs_with_parent_of_type(org_type) + parents = OrganisationType.find_by(name: (org_type)).organisations + children = Array.new + parents.each do |parent| + children += parent.children + end + return children + end - #retrieves info off a child org - def self.orgs_with_parent_of_type(org_type) - parents = OrganisationType.find_by_name(org_type).organisations - children = Array.new - parents.each do |parent| - children += parent.children - end - return children - end + ## + # returns a list of all guidance groups belonging to other organisations + # + # @return [Array] + def self.other_organisations + #DISCUSS - What does this method do? + org_types = [I18n.t('helpers.org_type.organisation')] + organisations_list = [] + org_types.each do |ot| + new_org_obejct = OrganisationType.find_by(name: ot) + org_with_guidance = GuidanceGroup.joins(new_org_obejct.organisations) - def self.other_organisations - org_types = [I18n.t('helpers.org_type.organisation')] - organisations_list = [] - org_types.each do |ot| - new_org_obejct = OrganisationType.find_by_name(ot) + organisations_list = organisations_list + org_with_guidance + end + return organisations_list + end - org_with_guidance = GuidanceGroup.joins(new_org_obejct.organisations) + ## + # returns a list of all sections of a given version from this organisation and it's parents + # + # @params version_id [Integer] version number of the section + # @return [Array
] list of sections + def all_sections(version_id) + if parent.nil? + secs = sections.where("version_id = ?", version_id) + if secs.nil? then + secs = Array.new + end + return secs + else + return sections.where("version_id = ? ", version_id).all + parent.all_sections(version_id) + end + end - organisations_list = organisations_list + org_with_guidance - end - return organisations_list - end + ## + # returns the guidance groups of this organisation and all of it's children + # + # @return [Array] list of guidance groups + def all_guidance_groups + ggs = guidance_groups + children.each do |c| + ggs = ggs + c.all_guidance_groups + end + return ggs + end - def all_sections(version_id) - if parent.nil? - secs = sections.where("version_id = ?", version_id) - if secs.nil? then - secs = Array.new - end - return secs - else - return sections.where("version_id = ? ", version_id).all + parent.all_sections(version_id) - end - end + ## + # returns the highest parent organisation in the tree + # + # @return [organisation] the root organisation + def root + if parent.nil? + return self + else + return parent.root + end + end - def all_guidance_groups - ggs = guidance_groups - children.each do |c| - ggs = ggs + c.all_guidance_groups - end - return ggs - end + ## + # takes in the id of, and returns an OptionWarning + # + # @params option_id [number] the id of the desired warning + # @return [OptionWarning] the specified warning + def warning(option_id) + warning = option_warnings.find_by(option_id: option_id) + if warning.nil? && !parent.nil? then + return parent.warning(option_id) + else + return warning + end + end - def root - if parent.nil? - return self - else - return parent.root - end - end + ## + # returns all published templates belonging to the organisation + # + # @return [Array] published dmptemplates + def published_templates + return dmptemplates.where("published = ?", 1) + end - def warning(option_id) - warning = option_warnings.find_by_option_id(option_id) - if warning.nil? && !parent.nil? then - return parent.warning(option_id) - else - return warning - end - end - - def published_templates - return dmptemplates.where("published = ?", 1) - end + def check_api_credentials + if token_permission_types.count == 0 + users.each do |user| + user.api_token = "" + user.save! + end + end + end end