diff --git a/app/models/question.rb b/app/models/question.rb index 6a46f07..4cce243 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -20,20 +20,21 @@ accepts_nested_attributes_for :suggested_answers, :allow_destroy => true accepts_nested_attributes_for :themes - attr_accessible :default_value, :dependency_id, :dependency_text, :guidance,:number, :parent_id, - :suggested_answer, :text, :section_id,:question_format_id,:options_attributes, - :suggested_answers_attributes, :option_comment_display, :theme_ids, - :as => [:default, :admin] + attr_accessible :default_value, :dependency_id, :dependency_text, :guidance,:number, :parent_id, :suggested_answer, :text, :section_id,:question_format_id,:options_attributes, :suggested_answers_attributes, :option_comment_display, :theme_ids, :as => [:default, :admin] + ## + # returns the text from the question + # + # @return [String] question's text def to_s - "#{text}" - end + "#{text}" + end - amoeba do - include_association :options - include_association :suggested_answers - clone [:themes] - end + amoeba do + include_association :options + include_association :suggested_answers + clone [:themes] + end #def question_type? # type_label = {} @@ -47,6 +48,11 @@ # return type_label #end + ## + # for each question theme, appends them separated by comas + # shouldnt have a ? after the method name + # + # @return [Hash{String=> String}] def question_themes? themes_label = {} i = 1 @@ -64,7 +70,12 @@ return themes_label end + ## # guidance for question in the org admin + # + # @param question [Question] the question to find guidance for + # @param org_admin [Organisation] the organisation to find guidance for + # @return [Hash{String => String}] def guidance_for_question(question, org_admin) # pulls together guidance from various sources for question guidances = {} @@ -77,7 +88,6 @@ end end end - # Guidance link to directly to a question question.guidances.each do |g_by_q| g_by_q.guidance_groups.each do |group| @@ -86,20 +96,17 @@ end end end - return guidances end - - - - - #get suggested answer belonging to the currents user for this question + ## + # get suggested answer belonging to the currents user for this question + # + # @param org_id [Integer] the id for the organisation + # @return [String] the suggested_answer for this question for the specified organisation def get_suggested_answer(org_id) suggested_answer = suggested_answers.find_by_organisation_id(org_id) return suggested_answer end - - end diff --git a/app/models/question_format.rb b/app/models/question_format.rb index e72a014..b8ede7c 100644 --- a/app/models/question_format.rb +++ b/app/models/question_format.rb @@ -1,12 +1,14 @@ class QuestionFormat < ActiveRecord::Base attr_accessible :title, :description, :as => [:default, :admin] - + #associations between tables has_many :questions - - def to_s + ## + # gives the title of the question_format + # + # @return [String] title of the question_format + def to_s "#{title}" end - - + end \ No newline at end of file diff --git a/app/models/section.rb b/app/models/section.rb index 223e350..fd1dcb7 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -12,6 +12,10 @@ attr_accessible :organisation_id, :description, :number, :title, :version_id , :published, :questions_attributes, :as => [:default, :admin] + ## + # return the title of the section + # + # @return [String] the title of the section def to_s "#{title}" end diff --git a/app/models/suggested_answer.rb b/app/models/suggested_answer.rb index 01d233e..3bbefa8 100644 --- a/app/models/suggested_answer.rb +++ b/app/models/suggested_answer.rb @@ -2,12 +2,15 @@ belongs_to :organisation belongs_to :question - + # accepts_nested_attributes_for :question - + attr_accessible :organisation_id, :question_id, :text, :is_example, :as => [:default, :admin] - - + + ## + # returns the text from the suggested_answer + # + # @return [String] the text from the suggested_answer def to_s "#{text}" end diff --git a/app/models/theme.rb b/app/models/theme.rb index 70b5b11..0b31f36 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -1,19 +1,23 @@ class Theme < ActiveRecord::Base #associations between tables - has_and_belongs_to_many :questions, join_table: "questions_themes" + has_and_belongs_to_many :questions, join_table: "questions_themes" has_and_belongs_to_many :guidances, join_table: "themes_in_guidance" - - + + # accepts_nested_attributes_for :guidances # accepts_nested_attributes_for :questions - + attr_accessible :guidance_ids , :as => [:default, :admin] attr_accessible :question_ids, :as => [:default, :admin] attr_accessible :description, :title, :locale , :as => [:default, :admin] - + + ## + # returns the title of the theme + # + # @return [String] title of the theme def to_s title end - + end diff --git a/app/models/token_permission_type.rb b/app/models/token_permission_type.rb index 8c02ecb..9b340f1 100644 --- a/app/models/token_permission_type.rb +++ b/app/models/token_permission_type.rb @@ -5,6 +5,10 @@ #associations between tables has_and_belongs_to_many :org_token_permissions, join_table: "org_token_permissions" + ## + # returns the token_type of the token_permission_type + # + # @return [String] token_type of the token_permission_type def to_s self.token_type end diff --git a/app/models/user.rb b/app/models/user.rb index 9edea25..637c579 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,7 +5,7 @@ # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable - devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, + devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable, :omniauth_providers => [:shibboleth] #associations between tables @@ -41,14 +41,14 @@ # Commented out due to warning in Rails 4. This line is redundant due to use of the rolify gem #has_and_belongs_to_many :roles, :join_table => :users_roles - + has_many :plan_sections accepts_nested_attributes_for :roles - - attr_accessible :password_confirmation, :encrypted_password, :remember_me, :id, :email, - :firstname, :last_login,:login_count, :orcid_id, :password, :shibboleth_id, - :user_status_id, :surname, :user_type_id, :organisation_id, :skip_invitation, + + attr_accessible :password_confirmation, :encrypted_password, :remember_me, :id, :email, + :firstname, :last_login,:login_count, :orcid_id, :password, :shibboleth_id, + :user_status_id, :surname, :user_type_id, :organisation_id, :skip_invitation, :other_organisation, :accept_terms, :role_ids, :dmponline3, :api_token # FIXME: The duplication in the block is to set defaults. It might be better if @@ -57,6 +57,11 @@ s.key :plan_list, defaults: { columns: Settings::PlanList::DEFAULT_COLUMNS } end + ## + # gives either the name of the user, or the email if name unspecified + # + # @param user_email [Boolean] defaults to true, allows the use of email if there is no firstname or surname + # @return [String] the email or the firstname and surname of the user def name(use_email = true) if ((firstname.nil? && surname.nil?) || (firstname.strip == "" && surname.strip == "")) && use_email then return email @@ -66,6 +71,13 @@ end end + ## + # sets a new organisation id for the user + # if the user has any roles such as org_admin or admin, those are removed + # if the user had an api_token, that is removed + # + # @param new_organisation_id [Integer] the id for an organisation + # @return [String] the empty string as a causality of setting api_token def organisation_id=(new_organisation_id) # if the user is not part of the new organisation if !self.user_org_roles.pluck(:organisation_id).include?(new_organisation_id.to_i) then @@ -93,9 +105,13 @@ end end # rip api_token from user - self.api_token = "" + self.remove_token! end + ## + # returns the first organisation id of the user or nil + # + # @return [Integer, nil] the id of the user's organisation def organisation_id if self.organisations.count > 0 then return self.organisations.first.id @@ -104,6 +120,10 @@ end end + ## + # returns the organisation of the user or nil + # + # @return [Organisation, nil] the organisation of the user def organisation if self.organisations.count > 0 then return self.organisations.first @@ -112,6 +132,11 @@ end end + ## + # returns the last organisation in the list of organisations + # possibly depricated as the user only has one organisation in the current schema + # + # @return [Organisation, nil] the organisation for the user def current_organisation if self.organisations.count > 0 then return self.organisations.last @@ -120,42 +145,64 @@ end end + ## + # sets a new organisation for the user + # + # @params new_organisation [Organisation] the new organisation for the user def organisation=(new_organisation) organisation_id = organisation.id end + ## + # checks if the user is an admin + # + # @return [Boolean] true if the user is an admin def is_admin? admin = roles.find_by( name: constant("user_role_types.super_admin")) return !admin.nil? end + ## + # checks if the user is an organisation admin + # + # @return [Boolean] true if the user is an organisation admin def is_org_admin? org_admin = roles.find_by(name: constant("user_role_types.organisational_admin")) return !org_admin.nil? end - def org_type - org_type = organisation.organisation_type.name - return org_type - end + ## + # checks what type the user's organisation is + # + # @return [String] the organisation type + def org_type + org_type = organisation.organisation_type.name + return org_type + end - def remove_token! - unless api_token.empty? - self.api_token = "" - self.save! - end + ## + # removes the api_token from the user + # modifies the user model + def remove_token! + unless api_token.empty? + self.api_token = "" + self.save! end + end - def keep_or_generate_token! - if api_token.empty? - self.api_token = loop do - random_token = SecureRandom.urlsafe_base64(nil, false) - break random_token unless User.exists?(api_token: random_token) - end - self.save! - # send an email to the user to notify them of their new api token - UserMailer.api_token_granted_notification(self) - end + # + # generates a new token for the user unless the user already has a token. + # modifies the user's model. + def keep_or_generate_token! + if api_token.empty? + self.api_token = loop do + random_token = SecureRandom.urlsafe_base64(nil, false) + break random_token unless User.exists?(api_token: random_token) + end + self.save! + # send an email to the user to notify them of their new api token + UserMailer.api_token_granted_notification(self) end + end end diff --git a/app/models/version.rb b/app/models/version.rb index 13e17c8..db03966 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -1,35 +1,41 @@ class Version < ActiveRecord::Base - + #associations between tables belongs_to :phase - + has_many :sections, :dependent => :destroy has_many :questions, :through => :sections, :dependent => :destroy has_many :plans - + #Link the data # accepts_nested_attributes_for :phase - accepts_nested_attributes_for :sections, :allow_destroy => true - - attr_accessible :id, :description, :number, :published, :title, :phase_id, + accepts_nested_attributes_for :sections, :allow_destroy => true + + attr_accessible :id, :description, :number, :published, :title, :phase_id, :sections_attributes, :as => [:default, :admin] - + + ## + # returns the title of the version + # + # @return [String] the title of the version def to_s "#{title}" end - - - + + + ## + # returns all sections where the org_id is the same as this version's phase's org_id + # + # @return [Array
] def global_sections sections.where("organisation_id = ? ", phase.dmptemplate.organisation_id).load end - + amoeba do include_association :sections include_association :questions set :published => 'false' - prepend :title => "Copy of " + prepend :title => "Copy of " end - - + end