From 0780bd241699a76947a40ea8486e2154a1ff8452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Wed, 5 Oct 2016 17:45:20 +0200 Subject: [PATCH] Ruby constants are now automatically available in JS. Refactoring was needed. --- app/assets/javascripts/comments.js.erb | 5 +- .../protocols/import_export/import.js.erb | 2 +- .../javascripts/sitewide/constants.js.erb | 49 +-- .../sitewide/form_validators.js.erb | 14 +- app/controllers/assets_controller.rb | 4 +- .../my_module_comments_controller.rb | 2 +- app/controllers/organizations_controller.rb | 5 +- .../project_comments_controller.rb | 2 +- app/controllers/result_comments_controller.rb | 2 +- app/controllers/search_controller.rb | 18 +- app/controllers/step_comments_controller.rb | 2 +- .../users/registrations_controller.rb | 2 +- app/controllers/users/settings_controller.rb | 13 +- app/helpers/activity_helper.rb | 4 +- app/helpers/reports_helper.rb | 2 +- app/models/asset.rb | 26 +- app/models/checklist.rb | 12 +- app/models/checklist_item.rb | 4 +- app/models/comment.rb | 18 +- app/models/custom_field.rb | 2 +- app/models/experiment.rb | 13 +- app/models/my_module.rb | 19 +- app/models/my_module_group.rb | 12 +- app/models/organization.rb | 10 +- app/models/project.rb | 13 +- app/models/protocol.rb | 12 +- app/models/protocol_keyword.rb | 5 +- app/models/report.rb | 13 +- app/models/result.rb | 12 +- app/models/result_text.rb | 4 +- app/models/sample.rb | 10 +- app/models/sample_custom_field.rb | 4 +- app/models/sample_group.rb | 8 +- app/models/sample_type.rb | 4 +- app/models/step.rb | 16 +- app/models/table.rb | 13 +- app/models/tag.rb | 16 +- app/models/user.rb | 25 +- app/views/activities/_activity.html.erb | 13 +- app/views/my_module_tags/_index_edit.html.erb | 8 +- .../experiment_archive/_experiment.html.erb | 7 +- .../projects/show/_workflow_img.html.erb | 8 +- .../elements/_result_asset_element.html.erb | 6 +- app/views/results/_result_asset.html.erb | 3 +- app/views/shared/_navigation.html.erb | 23 +- .../shared/_secondary_navigation.html.erb | 22 +- config/initializers/constants.rb | 319 +++++++++--------- db/seeds.rb | 4 +- lib/tasks/db_fake_data.rake | 3 +- lib/tasks/db_users.rake | 6 +- test/fixtures/assets.yml | 12 +- test/models/organization_test.rb | 2 +- 52 files changed, 439 insertions(+), 394 deletions(-) diff --git a/app/assets/javascripts/comments.js.erb b/app/assets/javascripts/comments.js.erb index 2497bffb9..25b132d82 100644 --- a/app/assets/javascripts/comments.js.erb +++ b/app/assets/javascripts/comments.js.erb @@ -182,7 +182,8 @@ var Comments = (function() { var parentTopPos = $el.offset().top; if (leftPos + menu.width() > $(window).width()) { menu.offset({ left: leftPos - menu.width(), - top: (parentTopPos + <%= DROPDOWN_TOP_OFFSET_PX %>)}); + top: (parentTopPos + + <%= Constants::DROPDOWN_TOP_OFFSET_PX %>)}); } }); } @@ -208,7 +209,7 @@ var Comments = (function() { var $el = $(el); var offset = useParentOffset ? $el.offset().top : $el.position().top; $el.find('.dropdown-menu-fixed') - .offset({ top: (offset + <%= DROPDOWN_TOP_OFFSET_PX %>) }); + .offset({ top: (offset + <%= Constants::DROPDOWN_TOP_OFFSET_PX %>) }); }); } diff --git a/app/assets/javascripts/protocols/import_export/import.js.erb b/app/assets/javascripts/protocols/import_export/import.js.erb index 4bf594c03..1ff9df895 100644 --- a/app/assets/javascripts/protocols/import_export/import.js.erb +++ b/app/assets/javascripts/protocols/import_export/import.js.erb @@ -497,7 +497,7 @@ function importProtocolFromFile( $.extend(data_json, params); var rough_size = roughSizeOfObject(data_json); - if (rough_size > <%= FILE_MAX_SIZE.megabytes %>) { + if (rough_size > <%= Constants::FILE_MAX_SIZE_MB.megabytes %>) { // Call the callback function resultCallback({ name: protocolJson["name"], new_name: null, status: "size_too_large" }); return; diff --git a/app/assets/javascripts/sitewide/constants.js.erb b/app/assets/javascripts/sitewide/constants.js.erb index a395ceef4..6bbfee306 100644 --- a/app/assets/javascripts/sitewide/constants.js.erb +++ b/app/assets/javascripts/sitewide/constants.js.erb @@ -1,47 +1,4 @@ -// All the constants from server-side available on client-side. +// All Ruby global constants are provided to JS +<%= Constants.output_to_js %> -var APP_VERSION = "<%= APP_VERSION %>"; -var TAG_COLORS = <%= TAG_COLORS %>; -var TEXT_EXTRACT_FILE_TYPES = <%= TEXT_EXTRACT_FILE_TYPES %>; -var DEFAULT_PRIVATE_ORG_NAME = "<%= DEFAULT_PRIVATE_ORG_NAME %>"; - -var NAME_MIN_LENGTH = <%= NAME_MIN_LENGTH %>; -var NAME_MAX_LENGTH = <%= NAME_MAX_LENGTH %>; -var NAME_TRUNCATION_LENGTH = <%= NAME_TRUNCATION_LENGTH %>; -var TEXT_MAX_LENGTH = <%= TEXT_MAX_LENGTH %>; -var COLOR_MAX_LENGTH = <%= TEXT_MAX_LENGTH %>; -var DROPDOWN_TEXT_MAX_LENGTH = <%= DROPDOWN_TEXT_MAX_LENGTH %>; -var FILENAME_TRUNCATION_LENGTH = <%= FILENAME_TRUNCATION_LENGTH %>; -var USER_INITIALS_MAX_LENGTH = <%= USER_INITIALS_MAX_LENGTH %>; -var EMAIL_MAX_LENGTH = <%= EMAIL_MAX_LENGTH %>; - -var SEARCH_LIMIT = <%= SEARCH_LIMIT %>; -var SEARCH_NO_LIMIT = <%= SEARCH_NO_LIMIT %>; -var MODAL_SEARCH_LIMIT = <%= MODAL_SEARCH_LIMIT %>; -var COMMENTS_SEARCH_LIMIT = <%= COMMENTS_SEARCH_LIMIT %>; -var ACTIVITY_SEARCH_LIMIT = <%= ACTIVITY_SEARCH_LIMIT %>; - -var TABLE_JSON_MAX_SIZE = <%= TABLE_JSON_MAX_SIZE %>; -var FILE_MAX_SIZE = <%= FILE_MAX_SIZE %>; -var AVATAR_MAX_SIZE = <%= AVATAR_MAX_SIZE %>; - -var MEDIUM_PIC_FORMAT = "<%= MEDIUM_PIC_FORMAT %>"; -var THUMB_PIC_FORMAT = "<%= THUMB_PIC_FORMAT %>"; -var ICON_PIC_FORMAT = "<%= ICON_PIC_FORMAT %>"; -var ICON_SMALL_PIC_FORMAT = "<%= ICON_SMALL_PIC_FORMAT %>"; - -var URL_SHORT_EXPIRE_TIME = <%= URL_SHORT_EXPIRE_TIME %>; -var URL_LONG_EXPIRE_TIME = <%= URL_LONG_EXPIRE_TIME %>; - -var MINIMAL_ORGANIZATION_SPACE_TAKEN = - <%= MINIMAL_ORGANIZATION_SPACE_TAKEN %>; -var ASSET_ESTIMATED_SIZE_FACTOR = <%= ASSET_ESTIMATED_SIZE_FACTOR %>; - -var TUTORIALS_URL = "<%= TUTORIALS_URL %>"; -var FAQ_URL = "<%= FAQ_URL %>"; -var SUPPORT_URL = "<%= SUPPORT_URL %>"; -var PLANS_URL = "<%= PLANS_URL %>"; -var CONTACT_URL = "<%= CONTACT_URL %>"; -var DEFAULT_AVATAR_URL = "<%= DEFAULT_AVATAR_URL %>"; - -var KICKSTARTER_SUPPORTERS = <%= KICKSTARTER_SUPPORTERS %>; +// JS global constants diff --git a/app/assets/javascripts/sitewide/form_validators.js.erb b/app/assets/javascripts/sitewide/form_validators.js.erb index bcb261f0f..17513cc6c 100644 --- a/app/assets/javascripts/sitewide/form_validators.js.erb +++ b/app/assets/javascripts/sitewide/form_validators.js.erb @@ -21,9 +21,9 @@ $.fn.onSubmitValidator = function(validatorCb) { var TextLimitEnum = Object.freeze({ OPTIONAL: 0, REQUIRED: 1, - NAME_MIN_LENGTH: "<%= NAME_MIN_LENGTH %>", - NAME_MAX_LENGTH: "<%= NAME_MAX_LENGTH %>", - TEXT_MAX_LENGTH: "<%= TEXT_MAX_LENGTH %>" + NAME_MIN_LENGTH: "<%= Constants::NAME_MIN_LENGTH %>", + NAME_MAX_LENGTH: "<%= Constants::NAME_MAX_LENGTH %>", + TEXT_MAX_LENGTH: "<%= Constants::TEXT_MAX_LENGTH %>" }); /* @@ -102,8 +102,8 @@ function checklistsValidator(ev, checklists, editMode) { } var FileTypeSizeEnum = Object.freeze({ - FILE: "<%= FILE_MAX_SIZE.megabytes %>", - AVATAR: "<%= AVATAR_MAX_SIZE.megabytes %>" + FILE: "<%= Constants::FILE_MAX_SIZE_MB.megabytes %>", + AVATAR: "<%= Constants::AVATAR_MAX_SIZE_MB.megabytes %>" }); function filesValidator(ev, fileInputs, fileTypeEnum, canBeEmpty) { @@ -139,9 +139,9 @@ function filesSizeValidator(ev, fileInputs, fileTypeEnum) { if (file.size > fileTypeEnum) { switch (fileTypeEnum) { case FileTypeSizeEnum.FILE: - return "<%= I18n.t 'general.file.size_exceeded', file_size: FILE_MAX_SIZE %>".strToErrorFormat(); + return "<%= I18n.t 'general.file.size_exceeded', file_size: Constants::FILE_MAX_SIZE_MB %>".strToErrorFormat(); case FileTypeSizeEnum.AVATAR: - return "<%= I18n.t 'general.file.size_exceeded', file_size: AVATAR_MAX_SIZE %>".strToErrorFormat(); + return "<%= I18n.t 'general.file.size_exceeded', file_size: Constants::AVATAR_MAX_SIZE_MB %>".strToErrorFormat(); } } }; diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 7a7dd169a..54965a2e6 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -106,7 +106,7 @@ class AssetsController < ApplicationController success_action_status: '201', acl: 'private', storage_class: "STANDARD", - content_length_range: 1..FILE_MAX_SIZE.megabytes, + content_length_range: 1..Constants::FILE_MAX_SIZE_MB.megabytes, content_type: asset.file_content_type ) posts.push({ @@ -121,7 +121,7 @@ class AssetsController < ApplicationController success_action_status: '201', acl: 'public-read', storage_class: "REDUCED_REDUNDANCY", - content_length_range: 1..FILE_MAX_SIZE.megabytes, + content_length_range: 1..Constants::FILE_MAX_SIZE_MB.megabytes, content_type: asset.file_content_type ) posts.push({ diff --git a/app/controllers/my_module_comments_controller.rb b/app/controllers/my_module_comments_controller.rb index bac0316f1..4aac9ad74 100644 --- a/app/controllers/my_module_comments_controller.rb +++ b/app/controllers/my_module_comments_controller.rb @@ -162,7 +162,7 @@ class MyModuleCommentsController < ApplicationController def load_vars @last_comment_id = params[:from].to_i - @per_page = COMMENTS_SEARCH_LIMIT + @per_page = Constants::COMMENTS_SEARCH_LIMIT @my_module = MyModule.find_by_id(params[:my_module_id]) unless @my_module diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index e1200965f..38f5bc86d 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -11,8 +11,9 @@ class OrganizationsController < ApplicationController if params[:file] begin - if params[:file].size > FILE_MAX_SIZE.megabytes - error = t 'general.file.size_exceeded', file_size: FILE_MAX_SIZE + if params[:file].size > Constants::FILE_MAX_SIZE_MB.megabytes + error = t 'general.file.size_exceeded', + file_size: Constants::FILE_MAX_SIZE_MB format.html { flash[:alert] = error diff --git a/app/controllers/project_comments_controller.rb b/app/controllers/project_comments_controller.rb index 0e58634a0..4690bbbf5 100644 --- a/app/controllers/project_comments_controller.rb +++ b/app/controllers/project_comments_controller.rb @@ -158,7 +158,7 @@ class ProjectCommentsController < ApplicationController def load_vars @last_comment_id = params[:from].to_i - @per_page = COMMENTS_SEARCH_LIMIT + @per_page = Constants::COMMENTS_SEARCH_LIMIT @project = Project.find_by_id(params[:project_id]) unless @project diff --git a/app/controllers/result_comments_controller.rb b/app/controllers/result_comments_controller.rb index ae187161a..1016f3ba9 100644 --- a/app/controllers/result_comments_controller.rb +++ b/app/controllers/result_comments_controller.rb @@ -159,7 +159,7 @@ class ResultCommentsController < ApplicationController def load_vars @last_comment_id = params[:from].to_i - @per_page = COMMENTS_SEARCH_LIMIT + @per_page = Constants::COMMENTS_SEARCH_LIMIT @result = Result.find_by_id(params[:result_id]) @my_module = @result.my_module diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 1ba1ce95a..58f43517e 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -24,7 +24,7 @@ class SearchController < ApplicationController search_tables if @search_category == :tables search_comments if @search_category == :comments - @search_pages = (@search_count.to_f / SEARCH_LIMIT.to_f).ceil + @search_pages = (@search_count.to_f / Constants::SEARCH_LIMIT.to_f).ceil @start_page = @search_page - 2 @start_page = 1 if @start_page < 1 @end_page = @start_page + 4 @@ -48,27 +48,29 @@ class SearchController < ApplicationController @search_page = params[:page].to_i || 1 @display_query = @search_query - if @search_query.length < NAME_MIN_LENGTH + if @search_query.length < Constants::NAME_MIN_LENGTH flash[:error] = t 'general.query.length_too_short', - min_length: NAME_MIN_LENGTH + min_length: Constants::NAME_MIN_LENGTH return redirect_to :back end # splits the search query to validate all entries @splited_query = @search_query.split - if @splited_query.first.length < NAME_MIN_LENGTH + if @splited_query.first.length < Constants::NAME_MIN_LENGTH flash[:error] = t 'general.query.length_too_short', - min_length: NAME_MIN_LENGTH + min_length: Constants::NAME_MIN_LENGTH redirect_to :back - elsif @splited_query.first.length > TEXT_MAX_LENGTH + elsif @splited_query.first.length > Constants::TEXT_MAX_LENGTH flash[:error] = t 'general.query.length_too_long', - max_length: TEXT_MAX_LENGTH + max_length: Constants::TEXT_MAX_LENGTH redirect_to :back elsif @splited_query.length > 1 @search_query = '' @splited_query.each_with_index do |w, i| - @search_query += "#{@splited_query[i]} " if w.length >= NAME_MIN_LENGTH + if w.length >= Constants::NAME_MIN_LENGTH + @search_query += "#{@splited_query[i]} " + end end else @search_query = @splited_query.join(' ') diff --git a/app/controllers/step_comments_controller.rb b/app/controllers/step_comments_controller.rb index acd7fd966..d5c8bf2ec 100644 --- a/app/controllers/step_comments_controller.rb +++ b/app/controllers/step_comments_controller.rb @@ -168,7 +168,7 @@ class StepCommentsController < ApplicationController def load_vars @last_comment_id = params[:from].to_i - @per_page = COMMENTS_SEARCH_LIMIT + @per_page = Constants::COMMENTS_SEARCH_LIMIT @step = Step.find_by_id(params[:step_id]) @protocol = @step.protocol diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index afe838025..1585e74f7 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -229,7 +229,7 @@ class Users::RegistrationsController < Devise::RegistrationsController success_action_status: '201', acl: 'public-read', storage_class: "REDUCED_REDUNDANCY", - content_length_range: 1..FILE_MAX_SIZE.megabytes, + content_length_range: 1..Constants::FILE_MAX_SIZE_MB.megabytes, content_type: content_type ) posts.push({ diff --git a/app/controllers/users/settings_controller.rb b/app/controllers/users/settings_controller.rb index a168e347a..ca83a48c3 100644 --- a/app/controllers/users/settings_controller.rb +++ b/app/controllers/users/settings_controller.rb @@ -121,17 +121,19 @@ class Users::SettingsController < ApplicationController format.json { if params.include? :existing_query and query = params[:existing_query].strip() - if query.length < NAME_MIN_LENGTH + if query.length < Constants::NAME_MIN_LENGTH render json: { "existing_query": [ - t('general.query.length_too_short', min_length: NAME_MIN_LENGTH) + t('general.query.length_too_short', + min_length: Constants::NAME_MIN_LENGTH) ] }, status: :unprocessable_entity - elsif query.length > NAME_MAX_LENGTH + elsif query.length > Constants::NAME_MAX_LENGTH render json: { "existing_query": [ - t('general.query.length_too_long', max_length: NAME_MAX_LENGTH) + t('general.query.length_too_long', + max_length: Constants::NAME_MAX_LENGTH) ] }, status: :unprocessable_entity @@ -140,7 +142,8 @@ class Users::SettingsController < ApplicationController nr_of_results = User.search(true, query, @org).count - users = User.search(false, query, @org).limit(MODAL_SEARCH_LIMIT) + users = User.search(false, query, @org) + .limit(Constants::MODAL_SEARCH_LIMIT) nr_of_members = User.organization_search(false, query, @org).count diff --git a/app/helpers/activity_helper.rb b/app/helpers/activity_helper.rb index 62bec3fd1..659b222ab 100644 --- a/app/helpers/activity_helper.rb +++ b/app/helpers/activity_helper.rb @@ -1,7 +1,7 @@ module ActivityHelper - def activity_truncate(message, len = NAME_TRUNCATION_LENGTH) + def activity_truncate(message, len = Constants::NAME_TRUNCATION_LENGTH) activity_title = message.match(/(.*?)<\/strong>/)[1] - if activity_title.length > NAME_TRUNCATION_LENGTH + if activity_title.length > Constants::NAME_TRUNCATION_LENGTH title = "" else diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 62dc42570..22ac12682 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -81,7 +81,7 @@ end def report_image_asset_url(asset) prefix = (ENV["PAPERCLIP_STORAGE"].present? && ENV["MAIL_SERVER_URL"].present? && ENV["PAPERCLIP_STORAGE"] == "filesystem") ? ENV["MAIL_SERVER_URL"] : "" prefix = (!prefix.empty? && !prefix.include?("http://") && !prefix.include?("https://")) ? "http://#{prefix}" : prefix - url = prefix + asset.url(:medium, timeout: URL_LONG_EXPIRE_TIME) + url = prefix + asset.url(:medium, timeout: Constants::URL_LONG_EXPIRE_TIME) image_tag(url) end diff --git a/app/models/asset.rb b/app/models/asset.rb index 5ba874643..797e7ae61 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -6,11 +6,11 @@ class Asset < ActiveRecord::Base require 'tempfile' # Paperclip validation - has_attached_file :file, styles: { medium: MEDIUM_PIC_FORMAT } + has_attached_file :file, styles: { medium: Constants::MEDIUM_PIC_FORMAT } validates_attachment :file, presence: true, - size: { less_than: FILE_MAX_SIZE.megabytes } + size: { less_than: Constants::FILE_MAX_SIZE_MB.megabytes } validates :estimated_size, presence: true validates :file_present, inclusion: { in: [true, false] } @@ -71,14 +71,14 @@ class Asset < ActiveRecord::Base ) step_ids = Step - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .joins(:step_assets) .select("step_assets.id") .distinct result_ids = Result - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .joins(:result_asset) .select("result_assets.id") .distinct @@ -121,10 +121,10 @@ class Asset < ActiveRecord::Base ) # Show all results if needed - if page != SEARCH_NO_LIMIT + if page != Constants::SEARCH_NO_LIMIT ids = ids - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end Asset @@ -140,7 +140,9 @@ class Asset < ActiveRecord::Base end def text? - TEXT_EXTRACT_FILE_TYPES.any? { |v| file_content_type.start_with? v } + Constants::TEXT_EXTRACT_FILE_TYPES.any? do |v| + file_content_type.start_with? v + end end # TODO: get the current_user @@ -235,7 +237,7 @@ class Asset < ActiveRecord::Base es += get_octet_length_record(asset_text_datum, :data) es += get_octet_length_record(asset_text_datum, :data_vector) end - es = es * ASSET_ESTIMATED_SIZE_FACTOR + es = es * Constants::ASSET_ESTIMATED_SIZE_FACTOR update(estimated_size: es) Rails.logger.info "Asset #{id}: Estimated size successfully calculated" @@ -246,7 +248,7 @@ class Asset < ActiveRecord::Base end end - def url(style = :original, timeout: URL_SHORT_EXPIRE_TIME) + def url(style = :original, timeout: Constants::URL_SHORT_EXPIRE_TIME) if file.is_stored_on_s3? presigned_url(style, timeout: timeout) else @@ -255,7 +257,9 @@ class Asset < ActiveRecord::Base end # When using S3 file upload, we can limit file accessibility with url signing - def presigned_url(style = :original, download: false, timeout: URL_SHORT_EXPIRE_TIME) + def presigned_url(style = :original, + download: false, + timeout: Constants::URL_SHORT_EXPIRE_TIME) if file.is_stored_on_s3? if download download_arg = 'attachment; filename=' + URI.escape(file_file_name) diff --git a/app/models/checklist.rb b/app/models/checklist.rb index afa52d656..6995e3f2e 100644 --- a/app/models/checklist.rb +++ b/app/models/checklist.rb @@ -2,7 +2,9 @@ class Checklist < ActiveRecord::Base include SearchableModel auto_strip_attributes :name, nullify: false - validates :name, presence: true, length: { maximum: TEXT_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::TEXT_MAX_LENGTH } validates :step, presence: true belongs_to :step, inverse_of: :checklists @@ -23,7 +25,7 @@ class Checklist < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) step_ids = Step - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -43,12 +45,12 @@ class Checklist < ActiveRecord::Base .where_attributes_like(["checklists.name", "checklist_items.text"], a_query) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/checklist_item.rb b/app/models/checklist_item.rb index 1189fc068..4c61ef6f0 100644 --- a/app/models/checklist_item.rb +++ b/app/models/checklist_item.rb @@ -1,6 +1,8 @@ class ChecklistItem < ActiveRecord::Base auto_strip_attributes :text, nullify: false - validates :text, presence: true, length: { maximum: TEXT_MAX_LENGTH } + validates :text, + presence: true, + length: { maximum: Constants::TEXT_MAX_LENGTH } validates :checklist, presence: true validates :checked, inclusion: { in: [true, false] } diff --git a/app/models/comment.rb b/app/models/comment.rb index 0b13eb517..6c0ade691 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -2,7 +2,9 @@ class Comment < ActiveRecord::Base include SearchableModel auto_strip_attributes :message, nullify: false - validates :message, presence: true, length: { maximum: TEXT_MAX_LENGTH } + validates :message, + presence: true, + length: { maximum: Constants::TEXT_MAX_LENGTH } validates :user, presence: true validate :belongs_to_only_one_object @@ -24,19 +26,19 @@ class Comment < ActiveRecord::Base ) project_ids = Project - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") my_module_ids = MyModule - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") step_ids = Step - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") result_ids = Result - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") @@ -73,12 +75,12 @@ class Comment < ActiveRecord::Base ) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 54fa8e1a7..508e87116 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -2,7 +2,7 @@ class CustomField < ActiveRecord::Base auto_strip_attributes :name, nullify: false validates :name, presence: true, - length: { maximum: NAME_MAX_LENGTH }, + length: { maximum: Constants::NAME_MAX_LENGTH }, uniqueness: { scope: :organization, case_sensitive: true }, exclusion: { in: ['Assigned', 'Sample name', 'Sample type', 'Sample group', 'Added on', 'Added by'] } diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 59abc74ba..1620a3290 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -19,9 +19,10 @@ class Experiment < ActiveRecord::Base auto_strip_attributes :name, :description, nullify: false validates :name, - length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH }, + length: { minimum: Constants::NAME_MIN_LENGTH, + maximum: Constants::NAME_MAX_LENGTH }, uniqueness: { scope: :project, case_sensitive: false } - validates :description, length: { maximum: TEXT_MAX_LENGTH } + validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :project, presence: true validates :created_by, presence: true validates :last_modified_by, presence: true @@ -35,7 +36,7 @@ class Experiment < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) project_ids = Project - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -62,12 +63,12 @@ class Experiment < ActiveRecord::Base end # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 06fb4070e..0e08567db 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -5,8 +5,9 @@ class MyModule < ActiveRecord::Base auto_strip_attributes :name, :description, nullify: false validates :name, - length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH } - validates :description, length: { maximum: TEXT_MAX_LENGTH } + length: { minimum: Constants::NAME_MIN_LENGTH, + maximum: Constants::NAME_MAX_LENGTH } + validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :x, :y, :workflow_order, presence: true validates :experiment, presence: true validates :my_module_group, presence: true, if: "!my_module_group_id.nil?" @@ -43,7 +44,7 @@ class MyModule < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) exp_ids = Experiment - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -70,12 +71,12 @@ class MyModule < ActiveRecord::Base end # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end @@ -145,13 +146,13 @@ class MyModule < ActiveRecord::Base ) end - def last_activities(count = ACTIVITY_SEARCH_LIMIT) + def last_activities(count = Constants::ACTIVITY_SEARCH_LIMIT) Activity.where(my_module_id: id).order(:created_at).last(count) end # Get module comments ordered by created_at time. Results are paginated # using last comment id and per_page parameters. - def last_comments(last_id = 1, per_page = COMMENTS_SEARCH_LIMIT) + def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT) last_id = 9999999999999 if last_id <= 1 comments = Comment.joins(:my_module_comment) .where(my_module_comments: { my_module_id: id }) @@ -161,7 +162,7 @@ class MyModule < ActiveRecord::Base comments.reverse end - def last_activities(last_id = 1, count = ACTIVITY_SEARCH_LIMIT) + def last_activities(last_id = 1, count = Constants::ACTIVITY_SEARCH_LIMIT) last_id = 9999999999999 if last_id <= 1 Activity.joins(:my_module) .where(my_module_id: id) diff --git a/app/models/my_module_group.rb b/app/models/my_module_group.rb index 3fe60a2bf..0eb76a239 100644 --- a/app/models/my_module_group.rb +++ b/app/models/my_module_group.rb @@ -2,7 +2,9 @@ class MyModuleGroup < ActiveRecord::Base include SearchableModel auto_strip_attributes :name, nullify: false - validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } validates :experiment, presence: true belongs_to :experiment, inverse_of: :my_module_groups @@ -12,7 +14,7 @@ class MyModuleGroup < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) exp_ids = Experiment - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") @@ -32,12 +34,12 @@ class MyModuleGroup < ActiveRecord::Base .where_attributes_like("my_module_groups.name", a_query) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 7533bc14c..d49e49521 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -5,8 +5,9 @@ class Organization < ActiveRecord::Base auto_strip_attributes :name, :description, nullify: false validates :name, - length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH } - validates :description, length: { maximum: TEXT_MAX_LENGTH } + length: { minimum: Constants::NAME_MIN_LENGTH, + maximum: Constants::NAME_MAX_LENGTH } + validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :space_taken, presence: true belongs_to :created_by, :foreign_key => 'created_by_id', :class_name => 'User' @@ -274,7 +275,7 @@ class Organization < ActiveRecord::Base end end # project.experiments.each |experiment| - self.space_taken = [st, MINIMAL_ORGANIZATION_SPACE_TAKEN].max + self.space_taken = [st, Constants::MINIMAL_ORGANIZATION_SPACE_TAKEN].max Rails::logger.info "Organization #{self.id}: " + "space (re)calculated to: " + "#{self.space_taken}B (#{number_to_human_size(self.space_taken)})" @@ -293,7 +294,8 @@ class Organization < ActiveRecord::Base # Release specified amount of bytes def release_space(space) orig_space = self.space_taken - self.space_taken = [space_taken - space, MINIMAL_ORGANIZATION_SPACE_TAKEN].max + self.space_taken = [space_taken - space, + Constants::MINIMAL_ORGANIZATION_SPACE_TAKEN].max Rails::logger.info "Organization #{self.id}: " + "space released: " + "#{orig_space}B - #{space}B = " + diff --git a/app/models/project.rb b/app/models/project.rb index a4409fec7..222c57c8d 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -5,7 +5,8 @@ class Project < ActiveRecord::Base auto_strip_attributes :name, nullify: false validates :name, - length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH }, + length: { minimum: Constants::NAME_MIN_LENGTH, + maximum: Constants::NAME_MAX_LENGTH }, uniqueness: { scope: :organization, case_sensitive: false } validates :visibility, presence: true validates :organization, presence: true @@ -64,22 +65,22 @@ class Project < ActiveRecord::Base end # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end - def last_activities(count = ACTIVITY_SEARCH_LIMIT) + def last_activities(count = Constants::ACTIVITY_SEARCH_LIMIT) activities.order(created_at: :desc).first(count) end # Get project comments order by created_at time. Results are paginated # using last comment id and per_page parameters. - def last_comments(last_id = 1, per_page = COMMENTS_SEARCH_LIMIT) + def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT) last_id = 9999999999999 if last_id <= 1 comments = Comment.joins(:project_comment) .where(project_comments: { project_id: id }) diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 0e0fa5036..c76dde60f 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -15,8 +15,8 @@ class Protocol < ActiveRecord::Base auto_strip_attributes :name, :description, nullify: false # Name is required when its actually specified (i.e. :in_repository? is true) - validates :name, length: { maximum: NAME_MAX_LENGTH } - validates :description, length: { maximum: TEXT_MAX_LENGTH } + validates :name, length: { maximum: Constants::NAME_MAX_LENGTH } + validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :organization, presence: true validates :protocol_type, presence: true @@ -76,7 +76,7 @@ class Protocol < ActiveRecord::Base module_ids = MyModule - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") where_str = @@ -140,12 +140,12 @@ class Protocol < ActiveRecord::Base ) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/protocol_keyword.rb b/app/models/protocol_keyword.rb index 25eea9ed9..52d9dfd41 100644 --- a/app/models/protocol_keyword.rb +++ b/app/models/protocol_keyword.rb @@ -1,11 +1,12 @@ class ProtocolKeyword < ActiveRecord::Base auto_strip_attributes :name, nullify: false validates :name, - length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH } + length: { minimum: Constants::NAME_MIN_LENGTH, + maximum: Constants::NAME_MAX_LENGTH } validates :organization, presence: true belongs_to :organization, inverse_of: :protocol_keywords has_many :protocol_protocol_keywords, inverse_of: :protocol_keyword, dependent: :destroy has_many :protocols, through: :protocol_protocol_keywords -end \ No newline at end of file +end diff --git a/app/models/report.rb b/app/models/report.rb index e44be0a57..3cd7aa376 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -3,9 +3,10 @@ class Report < ActiveRecord::Base auto_strip_attributes :name, :description, nullify: false validates :name, - length: { minimum: NAME_MIN_LENGTH, maximum: NAME_MAX_LENGTH }, + length: { minimum: Constants::NAME_MIN_LENGTH, + maximum: Constants::NAME_MAX_LENGTH }, uniqueness: { scope: [:user, :project], case_sensitive: false } - validates :description, length: { maximum: TEXT_MAX_LENGTH } + validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :project, presence: true validates :user, presence: true @@ -26,7 +27,7 @@ class Report < ActiveRecord::Base project_ids = Project - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -53,12 +54,12 @@ class Report < ActiveRecord::Base ) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/result.rb b/app/models/result.rb index 554c2f30f..0a66b22b4 100644 --- a/app/models/result.rb +++ b/app/models/result.rb @@ -3,7 +3,7 @@ class Result < ActiveRecord::Base auto_strip_attributes :name, nullify: false validates :user, :my_module, presence: true - validates :name, length: { maximum: NAME_MAX_LENGTH } + validates :name, length: { maximum: Constants::NAME_MAX_LENGTH } validate :text_or_asset_or_table belongs_to :user, inverse_of: :results @@ -35,7 +35,7 @@ class Result < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) module_ids = MyModule - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -59,12 +59,12 @@ class Result < ActiveRecord::Base end # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end @@ -72,7 +72,7 @@ class Result < ActiveRecord::Base is_asset ? result_asset.space_taken : 0 end - def last_comments(last_id = 1, per_page = COMMENTS_SEARCH_LIMIT) + def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT) last_id = 9999999999999 if last_id <= 1 comments = Comment.joins(:result_comment) .where(result_comments: { result_id: id }) diff --git a/app/models/result_text.rb b/app/models/result_text.rb index 5b77f79d9..39975f893 100644 --- a/app/models/result_text.rb +++ b/app/models/result_text.rb @@ -1,6 +1,8 @@ class ResultText < ActiveRecord::Base auto_strip_attributes :text, nullify: false - validates :text, presence: true, length: { maximum: TEXT_MAX_LENGTH } + validates :text, + presence: true, + length: { maximum: Constants::TEXT_MAX_LENGTH } validates :result, presence: true belongs_to :result, inverse_of: :result_text diff --git a/app/models/sample.rb b/app/models/sample.rb index f18f259d8..2d6809b1b 100644 --- a/app/models/sample.rb +++ b/app/models/sample.rb @@ -2,7 +2,9 @@ class Sample < ActiveRecord::Base include SearchableModel auto_strip_attributes :name, nullify: false - validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } validates :user, :organization, presence: true belongs_to :user, inverse_of: :samples @@ -59,12 +61,12 @@ class Sample < ActiveRecord::Base ) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/sample_custom_field.rb b/app/models/sample_custom_field.rb index 91397e7af..043bda051 100644 --- a/app/models/sample_custom_field.rb +++ b/app/models/sample_custom_field.rb @@ -1,6 +1,8 @@ class SampleCustomField < ActiveRecord::Base auto_strip_attributes :value, nullify: false - validates :value, presence: true, length: { maximum: NAME_MAX_LENGTH } + validates :value, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } validates :custom_field, :sample, presence: true belongs_to :custom_field, inverse_of: :sample_custom_fields diff --git a/app/models/sample_group.rb b/app/models/sample_group.rb index b121bf0fd..c794a93d0 100644 --- a/app/models/sample_group.rb +++ b/app/models/sample_group.rb @@ -1,7 +1,11 @@ class SampleGroup < ActiveRecord::Base auto_strip_attributes :name, :color, nullify: false - validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH } - validates :color, presence: true, length: { maximum: COLOR_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } + validates :color, + presence: true, + length: { maximum: Constants::COLOR_MAX_LENGTH } validates :organization, presence: true belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' diff --git a/app/models/sample_type.rb b/app/models/sample_type.rb index c23d4597d..1e4225fa3 100644 --- a/app/models/sample_type.rb +++ b/app/models/sample_type.rb @@ -1,6 +1,8 @@ class SampleType < ActiveRecord::Base auto_strip_attributes :name, nullify: false - validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } validates :organization, presence: true belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' diff --git a/app/models/step.rb b/app/models/step.rb index 4dc7dba74..d82449ead 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -2,8 +2,10 @@ class Step < ActiveRecord::Base include SearchableModel auto_strip_attributes :name, :description, nullify: false - validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH } - validates :description, length: { maximum: TEXT_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } + validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :position, presence: true validates :completed, inclusion: { in: [true, false] } validates :user, :protocol, presence: true @@ -42,7 +44,7 @@ class Step < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) protocol_ids = Protocol - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -61,12 +63,12 @@ class Step < ActiveRecord::Base .where_attributes_like([:name, :description], a_query) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end @@ -86,7 +88,7 @@ class Step < ActiveRecord::Base protocol.present? ? protocol.my_module : nil end - def last_comments(last_id = 1, per_page = COMMENTS_SEARCH_LIMIT) + def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT) last_id = 9999999999999 if last_id <= 1 comments = Comment.joins(:step_comment) .where(step_comments: { step_id: id }) diff --git a/app/models/table.rb b/app/models/table.rb index a0087fae6..43dd90893 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -3,7 +3,7 @@ class Table < ActiveRecord::Base validates :contents, presence: true, - length: { maximum: TABLE_JSON_MAX_SIZE.megabytes } + length: { maximum: Constants::TABLE_JSON_MAX_SIZE_MB.megabytes } belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' @@ -20,14 +20,14 @@ class Table < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) step_ids = Step - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .joins(:step_tables) .select("step_tables.id") .distinct result_ids = Result - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .joins(:result_table) .select("result_tables.id") .distinct @@ -55,12 +55,12 @@ class Table < ActiveRecord::Base new_query = table_query # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end @@ -78,4 +78,3 @@ class Table < ActiveRecord::Base end end end - diff --git a/app/models/tag.rb b/app/models/tag.rb index 028fc5aa1..93509cdc2 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -2,8 +2,12 @@ class Tag < ActiveRecord::Base include SearchableModel auto_strip_attributes :name, :color, nullify: false - validates :name, presence: true, length: { maximum: NAME_MAX_LENGTH } - validates :color, presence: true, length: { maximum: COLOR_MAX_LENGTH } + validates :name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } + validates :color, + presence: true, + length: { maximum: Constants::COLOR_MAX_LENGTH } validates :project, presence: true belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' @@ -15,7 +19,7 @@ class Tag < ActiveRecord::Base def self.search(user, include_archived, query = nil, page = 1) project_ids = Project - .search(user, include_archived, nil, SEARCH_NO_LIMIT) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) .select("id") if query @@ -34,12 +38,12 @@ class Tag < ActiveRecord::Base .where_attributes_like(:name, a_query) # Show all results if needed - if page == SEARCH_NO_LIMIT + if page == Constants::SEARCH_NO_LIMIT new_query else new_query - .limit(SEARCH_LIMIT) - .offset((page - 1) * SEARCH_LIMIT) + .limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/user.rb b/app/models/user.rb index b9e6f9021..111e7bf41 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,12 +5,12 @@ class User < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable, stretches: 10 has_attached_file :avatar, styles: { - medium: MEDIUM_PIC_FORMAT, - thumb: THUMB_PIC_FORMAT, - icon: ICON_PIC_FORMAT, - icon_small: ICON_SMALL_PIC_FORMAT + medium: Constants::MEDIUM_PIC_FORMAT, + thumb: Constants::THUMB_PIC_FORMAT, + icon: Constants::ICON_PIC_FORMAT, + icon_small: Constants::ICON_SMALL_PIC_FORMAT }, - default_url: DEFAULT_AVATAR_URL + default_url: Constants::DEFAULT_AVATAR_URL enum tutorial_status: { no_tutorial_done: 0, @@ -18,15 +18,19 @@ class User < ActiveRecord::Base } auto_strip_attributes :full_name, :initials, nullify: false - validates :full_name, presence: true, length: { maximum: NAME_MAX_LENGTH } + validates :full_name, + presence: true, + length: { maximum: Constants::NAME_MAX_LENGTH } validates :initials, presence: true, - length: { maximum: USER_INITIALS_MAX_LENGTH } - validates :email, presence: true, length: { maximum: EMAIL_MAX_LENGTH } + length: { maximum: Constants::USER_INITIALS_MAX_LENGTH } + validates :email, + presence: true, + length: { maximum: Constants::EMAIL_MAX_LENGTH } validates_attachment :avatar, :content_type => { :content_type => ["image/jpeg", "image/png"] }, - size: { less_than: AVATAR_MAX_SIZE.megabytes } + size: { less_than: Constants::AVATAR_MAX_SIZE_MB.megabytes } validates :time_zone, presence: true validate :time_zone_check @@ -234,7 +238,8 @@ class User < ActiveRecord::Base # Finds all activities of user that is assigned to project. If user # is not an owner of the project, user must be also assigned to # module. - def last_activities(last_activity_id = nil, per_page = ACTIVITY_SEARCH_LIMIT) + def last_activities(last_activity_id = nil, + per_page = Constants::ACTIVITY_SEARCH_LIMIT) # TODO replace with some kind of Infinity value last_activity_id = 999999999999999999999999 if last_activity_id < 1 Activity diff --git a/app/views/activities/_activity.html.erb b/app/views/activities/_activity.html.erb index 8fb5f5ae1..0b77bf2be 100644 --- a/app/views/activities/_activity.html.erb +++ b/app/views/activities/_activity.html.erb @@ -6,16 +6,21 @@ <%= activity_truncate(activity.message) %> <% if activity.my_module %> [<%=t 'Project' %>: - <% if activity.my_module.experiment.project.name.length > NAME_TRUNCATION_LENGTH %> + <% if activity.my_module.experiment.project.name.length > + Constants::NAME_TRUNCATION_LENGTH %> <% else %> <%= activity.my_module.experiment.project.name %> <% end %>, <%=t 'Module' %>: - <% if activity.my_module.name.length > NAME_TRUNCATION_LENGTH %> -