From be5faad97ced54306c885e8c5ae253b283df7671 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 31 Jan 2017 13:33:55 +0100 Subject: [PATCH] fixes hound --- .../javascripts/users/settings/teams.js | 4 +- app/controllers/users/settings_controller.rb | 33 +-- app/datatables/sample_datatable.rb | 11 +- app/models/protocol.rb | 206 +++++++++++------- app/models/sample.rb | 10 +- app/models/team.rb | 4 +- app/models/user_team.rb | 2 +- db/seeds.rb | 2 +- lib/tasks/db_users.rake | 72 +++--- 9 files changed, 189 insertions(+), 155 deletions(-) diff --git a/app/assets/javascripts/users/settings/teams.js b/app/assets/javascripts/users/settings/teams.js index 597acffe2..6e1008211 100644 --- a/app/assets/javascripts/users/settings/teams.js +++ b/app/assets/javascripts/users/settings/teams.js @@ -1,4 +1,4 @@ -function initLeaveOrganizations() { +function initLeaveTeams() { // Bind the "leave team" buttons in teams table $(document) .on( @@ -56,4 +56,4 @@ function initLeaveOrganizations() { ); } -initLeaveOrganizations(); +initLeaveTeams(); diff --git a/app/controllers/users/settings_controller.rb b/app/controllers/users/settings_controller.rb index eebd5d620..07483f301 100644 --- a/app/controllers/users/settings_controller.rb +++ b/app/controllers/users/settings_controller.rb @@ -177,36 +177,37 @@ class Users::SettingsController < ApplicationController def leave_user_team_html respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "users/settings/teams/leave_user_team_modal_body.html.erb", + html: render_to_string( + partial: 'users/settings/teams/leave_user_team_modal_body.html.erb', locals: { user_team: @user_team } - }), + ), heading: I18n.t( - "users.settings.teams.index.leave_uo_heading", + 'users.settings.teams.index.leave_uo_heading', team: escape_input(@user_team.team.name) ) } - } + end end end def destroy_user_team_html respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "users/settings/teams/destroy_user_team_modal_body.html.erb", + html: render_to_string( + partial: 'users/settings/teams/' \ + 'destroy_user_team_modal_body.html.erb', locals: { user_team: @user_team } - }), + ), heading: I18n.t( - "users.settings.teams.edit.destroy_uo_heading", + 'users.settings.teams.edit.destroy_uo_heading', user: escape_input(@user_team.user.full_name), team: escape_input(@user_team.team.name) ) } - } + end end end @@ -254,7 +255,7 @@ class Users::SettingsController < ApplicationController if !invalid if params[:leave] then flash[:notice] = I18n.t( - "users.settings.teams.index.leave_flash", + 'users.settings.teams.index.leave_flash', team: @user_team.team.name ) flash.keep(:notice) @@ -285,7 +286,7 @@ class Users::SettingsController < ApplicationController .includes(team: :users) .where(role: 1..2) .order(created_at: :asc) - .map { |uo| uo.team } + .map(&:team) @member_of = @teams.count respond_to do |format| @@ -379,8 +380,8 @@ class Users::SettingsController < ApplicationController @team = @user_team.team # Don't allow the user to modify UserTeam-s if he's not admin, # unless he/she is modifying his/her UserTeam - if current_user != @user_team.user and - !is_admin_of_team(@user_team.team) + if current_user != @user_team.user && + !is_admin_of_team(@user_team.team) render_403 end end diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index 14ccac27c..9404bc600 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -253,12 +253,13 @@ class SampleDatatable < AjaxDatatablesRails::Base .select('"samples"."id"') .distinct - # grabs the ids that are not the previous one but are still of the same team + # grabs the ids that are not the previous one but are still + # of the same team unassigned = Sample - .where('"samples"."team_id" = ?', @team.id) - .where('"samples"."id" NOT IN (?)', assigned) - .select('"samples"."id"') - .distinct + .where('"samples"."team_id" = ?', @team.id) + .where('"samples"."id" NOT IN (?)', assigned) + .select('"samples"."id"') + .distinct # check the input param and merge the two arrays of ids if params[:order].values[0]['dir'] == 'asc' diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 762d316d8..1ae7920b0 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -41,103 +41,148 @@ class Protocol < ActiveRecord::Base end with_options if: :in_repository_public? do |protocol| # Public protocol must have unique name inside its team - protocol.validates_uniqueness_of :name, scope: [:team], conditions: -> { where(protocol_type: Protocol.protocol_types[:in_repository_public]) } + protocol + .validates_uniqueness_of :name, + scope: :team, + conditions: -> { + where( + protocol_type: + Protocol + .protocol_types[:in_repository_public] + ) + } protocol.validates :published_on, presence: true end with_options if: :in_repository_private? do |protocol| # Private protocol must have unique name inside its team & user scope - protocol.validates_uniqueness_of :name, scope: [:team, :added_by], conditions: -> { where(protocol_type: Protocol.protocol_types[:in_repository_private]) } + protocol + .validates_uniqueness_of :name, + scope: [:team, :added_by], + conditions: -> { + where( + protocol_type: + Protocol + .protocol_types[:in_repository_private] + ) + } end with_options if: :in_repository_archived? do |protocol| # Archived protocol must have unique name inside its team & user scope - protocol.validates_uniqueness_of :name, scope: [:team, :added_by], conditions: -> { where(protocol_type: Protocol.protocol_types[:in_repository_archived]) } + protocol + .validates_uniqueness_of :name, + scope: [:team, :added_by], + conditions: -> { + where( + protocol_type: + Protocol + .protocol_types[:in_repository_archived] + ) + } protocol.validates :archived_by, presence: true protocol.validates :archived_on, presence: true end - belongs_to :added_by, foreign_key: 'added_by_id', class_name: 'User', inverse_of: :added_protocols + belongs_to :added_by, + foreign_key: 'added_by_id', + class_name: 'User', + inverse_of: :added_protocols belongs_to :my_module, inverse_of: :protocols belongs_to :team, inverse_of: :protocols belongs_to :parent, foreign_key: 'parent_id', class_name: 'Protocol' - belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User', inverse_of: :archived_protocols - belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User', inverse_of: :restored_protocols - has_many :linked_children, class_name: 'Protocol', foreign_key: 'parent_id' - has_many :protocol_protocol_keywords, inverse_of: :protocol, dependent: :destroy + belongs_to :archived_by, + foreign_key: 'archived_by_id', + class_name: 'User', + inverse_of: :archived_protocols + belongs_to :restored_by, + foreign_key: 'restored_by_id', + class_name: 'User', + inverse_of: :restored_protocols + has_many :linked_children, + class_name: 'Protocol', + foreign_key: 'parent_id' + has_many :protocol_protocol_keywords, + inverse_of: :protocol, + dependent: :destroy has_many :protocol_keywords, through: :protocol_protocol_keywords has_many :steps, inverse_of: :protocol, dependent: :destroy def self.search(user, include_archived, query = nil, page = 1) - team_ids = - Team - .joins(:user_teams) - .where("user_teams.user_id = ?", user.id) - .select("id") - .distinct + team_ids = Team.joins(:user_teams) + .where('user_teams.user_id = ?', user.id) + .select('id') + .distinct - module_ids = - MyModule - .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) - .select("id") + module_ids = MyModule.search(user, + include_archived, + nil, + Constants::SEARCH_NO_LIMIT) + .select('id') where_str = - "(protocol_type IN (?) AND my_module_id IN (?)) OR " + - "(protocol_type = ? AND protocols.team_id IN (?) AND added_by_id = ?) OR " + - "(protocol_type = ? AND protocols.team_id IN (?))" + '(protocol_type IN (?) AND my_module_id IN (?)) OR ' \ + '(protocol_type = ? AND protocols.team_id IN (?) AND ' \ + 'added_by_id = ?) OR (protocol_type = ? AND protocols.team_id IN (?))' if include_archived where_str += - " OR (protocol_type = ? AND protocols.team_id IN (?) AND added_by_id = ?)" + ' OR (protocol_type = ? AND protocols.team_id IN (?) ' \ + 'AND added_by_id = ?)' new_query = Protocol - .where( - where_str, - [Protocol.protocol_types[:unlinked], Protocol.protocol_types[:linked]], - module_ids, - Protocol.protocol_types[:in_repository_private], - team_ids, - user.id, - Protocol.protocol_types[:in_repository_public], - team_ids, - Protocol.protocol_types[:in_repository_archived], - team_ids, - user.id - ) + .where( + where_str, + [Protocol.protocol_types[:unlinked], + Protocol.protocol_types[:linked]], + module_ids, + Protocol.protocol_types[:in_repository_private], + team_ids, + user.id, + Protocol.protocol_types[:in_repository_public], + team_ids, + Protocol.protocol_types[:in_repository_archived], + team_ids, + user.id + ) else new_query = Protocol - .where( - where_str, - [Protocol.protocol_types[:unlinked], Protocol.protocol_types[:linked]], - module_ids, - Protocol.protocol_types[:in_repository_private], - team_ids, - user.id, - Protocol.protocol_types[:in_repository_public], - team_ids - ) + .where( + where_str, + [Protocol.protocol_types[:unlinked], + Protocol.protocol_types[:linked]], + module_ids, + Protocol.protocol_types[:in_repository_private], + team_ids, + user.id, + Protocol.protocol_types[:in_repository_public], + team_ids + ) end if query a_query = query.strip - .gsub("_","\\_") - .gsub("%","\\%") - .split(/\s+/) - .map {|t| "%" + t + "%" } + .gsub('_', '\\_') + .gsub('%', '\\%') + .split(/\s+/) + .map { |t| '%' + t + '%' } else a_query = query end new_query = new_query - .distinct - .joins("LEFT JOIN protocol_protocol_keywords ON protocols.id = protocol_protocol_keywords.protocol_id") - .joins("LEFT JOIN protocol_keywords ON protocol_keywords.id = protocol_protocol_keywords.protocol_keyword_id") - .where_attributes_like( - [ - "protocols.name", - "protocols.description", - "protocols.authors", - "protocol_keywords.name" - ], - a_query - ) + .distinct + .joins('LEFT JOIN protocol_protocol_keywords ON ' \ + 'protocols.id = protocol_protocol_keywords.protocol_id') + .joins('LEFT JOIN protocol_keywords ' \ + 'ON protocol_keywords.id = ' \ + 'protocol_protocol_keywords.protocol_keyword_id') + .where_attributes_like( + [ + 'protocols.name', + 'protocols.description', + 'protocols.authors', + 'protocol_keywords.name' + ], + a_query + ) # Show all results if needed if page == Constants::SEARCH_NO_LIMIT @@ -271,9 +316,7 @@ class Protocol < ActiveRecord::Base # Copy tables step.tables.each do |table| - table2 = Table.new( - name: table.name, - contents: table.contents) + table2 = Table.new(name: table.name, contents: table.contents) table2.created_by = current_user table2.last_modified_by = current_user step2.tables << table2 @@ -288,38 +331,38 @@ class Protocol < ActiveRecord::Base end def in_repository_active? - in_repository_private? || - in_repository_public? + in_repository_private? || in_repository_public? end def in_repository? - in_repository_active? || - in_repository_archived? + in_repository_active? || in_repository_archived? end def in_module? - unlinked? || - linked? + unlinked? || linked? end def linked_no_diff? - linked? && updated_at == parent_updated_at && - parent.updated_at == parent_updated_at + linked? && + updated_at == parent_updated_at && + parent.updated_at == parent_updated_at end def newer_than_parent? linked? && parent.updated_at == parent_updated_at && - updated_at > parent_updated_at + updated_at > parent_updated_at end def parent_newer? - linked? && updated_at == parent_updated_at && - parent.updated_at > parent_updated_at + linked? && + updated_at == parent_updated_at && + parent.updated_at > parent_updated_at end def parent_and_self_newer? - linked? && parent.updated_at > parent_updated_at && - updated_at > parent_updated_at + linked? && + parent.updated_at > parent_updated_at && + updated_at > parent_updated_at end def number_of_steps @@ -327,7 +370,7 @@ class Protocol < ActiveRecord::Base end def completed_steps - steps.select { |step| step.completed } + steps.select(&:completed) end def space_taken @@ -602,15 +645,12 @@ class Protocol < ActiveRecord::Base success = clone.save end - unless success - raise ActiveRecord::RecordNotSaved - end + raise ActiveRecord::RecordNotSaved unless success Protocol.clone_contents(self, clone, current_user, true) clone.reload - - return clone + clone end def update_linked_children diff --git a/app/models/sample.rb b/app/models/sample.rb index bb258514e..54f3ee496 100644 --- a/app/models/sample.rb +++ b/app/models/sample.rb @@ -26,12 +26,10 @@ class Sample < ActiveRecord::Base page = 1, current_team = nil ) - team_ids = - Team - .joins(:user_teams) - .where("user_teams.user_id = ?", user.id) - .select("id") - .distinct + team_ids = Team.joins(:user_teams) + .where('user_teams.user_id = ?', user.id) + .select('id') + .distinct if query a_query = '%' + query.strip.gsub('_', '\\_').gsub('%', '\\%') + '%' diff --git a/app/models/team.rb b/app/models/team.rb index 40f245186..4a31155f6 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -128,7 +128,7 @@ class Team < ActiveRecord::Base # We need to have sample saved before messing with custom fields (they # need sample id) if index == stype_index - stype = SampleType.where(name: value, team_id: id).take(); + stype = SampleType.where(name: value, team_id: id).take if stype sample.sample_type = stype @@ -140,7 +140,7 @@ class Team < ActiveRecord::Base end sample.save elsif index == sgroup_index - sgroup = SampleGroup.where(name: value, team_id: id).take(); + sgroup = SampleGroup.where(name: value, team_id: id).take if sgroup sample.sample_group = sgroup diff --git a/app/models/user_team.rb b/app/models/user_team.rb index 508f88776..ddbb0fc21 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -13,7 +13,7 @@ class UserTeam < ActiveRecord::Base after_create :create_samples_table_state def role_str - I18n.t("user_teams.enums.role.#{role.to_s}") + I18n.t("user_teams.enums.role.#{role}") end def create_samples_table_state diff --git a/db/seeds.rb b/db/seeds.rb index 5a494d351..fea6d4199 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,6 +7,6 @@ create_user( 'admin@scinote.net', admin_password, true, - Constants::DEFAULT_PRIVATE_ORG_NAME, + Constants::DEFAULT_PRIVATE_TEAM_NAME, [] ) diff --git a/lib/tasks/db_users.rake b/lib/tasks/db_users.rake index 362c02a3a..7bcfe7e8f 100644 --- a/lib/tasks/db_users.rake +++ b/lib/tasks/db_users.rake @@ -21,82 +21,78 @@ namespace :db do begin ActiveRecord::Base.transaction do # Parse user & team hashes from YAML - teams = yaml.select{ |k, v| /team_[0-9]+/ =~ k } - users = yaml.select{ |k, v| /user_[0-9]+/ =~ k } + teams = yaml.select { |k, v| /team_[0-9]+/ =~ k } + users = yaml.select { |k, v| /user_[0-9]+/ =~ k } # Create teams teams.each do |k, team_hash| - team = Team.order(created_at: :desc).where(name: team_hash["name"]).first - if team.blank? - team = Team.create({ - name: team_hash["name"][0..99] - }) - end - team_hash["id"] = team.id + team = Team.order(created_at: :desc) + .where(name: team_hash['name']) + .first + team = Team.create(name: team_hash['name'][0..99]) if team.blank? + team_hash['id'] = team.id end # Create users - puts "Created users" + puts 'Created users' users.each do |k, user_hash| - password = user_hash["password"] - if password.blank? - password = generate_user_password - end + password = user_hash['password'] + password = generate_user_password if password.blank? - user_teams = user_hash["teams"] - if user_teams.blank? - user_teams = "" - end + user_teams = user_hash['teams'] + user_teams = '' if user_teams.blank team_ids = user_teams - .split(",") - .collect{ |o| o.strip } + .split(',') + .collect(&:strip) .uniq - .select{ |o| teams.include? o } - .collect{ |o| teams[o]["id"] } + .select { |o| teams.include? o } + .collect { |o| teams[o]['id'] } user = create_user( - user_hash["full_name"], - user_hash["email"], + user_hash['full_name'], + user_hash['email'], password, true, create_teams ? Constants::DEFAULT_PRIVATE_ORG_NAME : nil, team_ids ) - if user.id.present? then - puts "" + if user.id.present? + puts '' print_user(user, password) end end - puts "" + puts '' end rescue ActiveRecord::ActiveRecordError, ArgumentError - puts "Error creating all users, transaction reverted" + puts 'Error creating all users, transaction reverted' end end - desc "Add a single user to the database" + desc 'Add a single user to the database' task :add_user => :environment do - puts "Type in user's full name (e.g. 'Steve Johnson')" + puts 'Type in user\'s full name (e.g. \'Steve Johnson\')' full_name = $stdin.gets.to_s.strip - puts "Type in user's email (e.g. 'steve.johnson@gmail.com')" + puts 'Type in user\'s email (e.g. \'steve.johnson@gmail.com\')' email = $stdin.gets.to_s.strip - puts "Type in user's password (e.g. 'password'), or leave blank to let Rails generate password" + puts 'Type in user\'s password (e.g. \'password\'), or ' \ + 'leave blank to let Rails generate password' password = $stdin.gets.to_s.strip if password.empty? password = generate_user_password end - puts "Do you want Rails to create default user's team? (T/F)" - create_team = $stdin.gets.to_s.strip == "T" - puts "Type names of any additional teams you want the user to be admin of (delimited with ','), or leave blank" + puts 'Do you want Rails to create default user\'s team? (T/F)' + create_team = $stdin.gets.to_s.strip == 'T' + puts 'Type names of any additional teams you want the user ' \ + 'to be admin of (delimited with \',\'), or leave blank' team_names = $stdin.gets.to_s.strip if team_names.empty? team_names = [] else - team_names = team_names.split(",").collect { |n| n.strip } + team_names = team_names.split(',').collect(&:strip) end begin @@ -105,9 +101,7 @@ namespace :db do team_ids = [] team_names.each do |team_name| team = Team.order(created_at: :desc).where(name: team_name).first - if team.blank? then - team = Team.create({ name: team_name[0..99] }) - end + team = Team.create(name: team_name[0..99]) if team.blank team_ids << team.id end