From 996c99e2c78c7c5e991897bf7258ccb1cdf76298 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Wed, 31 Jan 2018 16:01:43 -0600 Subject: [PATCH 01/44] Start update to Rails 5. .ruby-version: Set to ruby-2.3.0 since Rails 5 requires Ruby 2.2.2+. Gemfile: Set rails version to 5.0.6. Comment out prototype-rails for now at least. Remove version restriction from multi_json. Change Gem "protected_attributes" to "protected_attributes_continued" until we start using strong parameters. Comment out prototype_legacy_helper for now. Gemfile.lock: Generated by doing > rails activerecord-session_store activerecord-postgis-adapter multi_json application_controller.rb: In #log_searches, call #permit! on params before calling #inspect. application_helper.rb: In the "sortable" helper, call #permit on params before calling #merge. In "link_to_submit" helper, replace the call to "link_to_function" with a suitable "link_to" call. --- .ruby-version | 2 +- Gemfile | 10 +- Gemfile.lock | 156 ++++++++++------------ app/controllers/application_controller.rb | 2 +- app/helpers/application_helper.rb | 4 +- 5 files changed, 83 insertions(+), 91 deletions(-) diff --git a/.ruby-version b/.ruby-version index d427c89b6..a831048a2 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-2.1.5 +ruby-2.3.0 diff --git a/Gemfile b/Gemfile index 48345bbd6..090c363fa 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,8 @@ source "http://rubygems.org" -gem "rails", "4.2.10" +gem "rails", "5.0.6" # needed for rails 3.1 and above until we phase out prototype: -gem 'prototype-rails', github: 'rails/prototype-rails', branch: '4.2' # see https://github.com/rails/prototype-rails/issues/37 +## gem 'prototype-rails', github: 'rails/prototype-rails', branch: '4.2' # see https://github.com/rails/prototype-rails/issues/37 gem "query_reviewer", "0.1.6" gem "nokogiri" gem "narray", "0.6.0.4" @@ -10,7 +10,7 @@ gem "choice", "0.1.6" gem "comma", "3.0.4" gem "json" gem "rgeo", "~> 0.5.0" -gem "multi_json", "1.3.6" +gem "multi_json" gem "railroad", "0.5.0" gem "recaptcha", "0.3.4", :require => "recaptcha/rails" gem "ruby-graphviz", "1.0.8" @@ -25,10 +25,10 @@ gem 'rabl' gem 'yajl-ruby', '~> 1.3.1' gem 'rubyzip', '~> 1.2.1' gem 'activerecord-session_store' # no longer part of Rails proper -gem 'protected_attributes' # Use this until and unless we start using Strong Parameters. +gem 'protected_attributes_continued' # Use this until and unless we start using Strong Parameters. # to-do: remove prototype dependencies so we no longer need this gem -gem 'prototype_legacy_helper', '0.0.0', :git => 'git://github.com/rails/prototype_legacy_helper.git' +#gem 'prototype_legacy_helper', '0.0.0', :git => 'git://github.com/rails/prototype_legacy_helper.git' gem 'memoist' diff --git a/Gemfile.lock b/Gemfile.lock index 56820fce7..d4c88ecff 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,67 +1,58 @@ -GIT - remote: git://github.com/rails/prototype-rails.git - revision: 0fed929ff48c10c3b978edd3baa983a81f404dbf - branch: 4.2 - specs: - prototype-rails (4.0.0) - rails (~> 4.0) - -GIT - remote: git://github.com/rails/prototype_legacy_helper.git - revision: a2cd95c3e3c1a4f7a9566efdab5ce59c886cb05f - specs: - prototype_legacy_helper (0.0.0) - GEM remote: http://rubygems.org/ specs: - actionmailer (4.2.10) - actionpack (= 4.2.10) - actionview (= 4.2.10) - activejob (= 4.2.10) + actioncable (5.0.6) + actionpack (= 5.0.6) + nio4r (>= 1.2, < 3.0) + websocket-driver (~> 0.6.1) + actionmailer (5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 1.0, >= 1.0.5) - actionpack (4.2.10) - actionview (= 4.2.10) - activesupport (= 4.2.10) - rack (~> 1.6) - rack-test (~> 0.6.2) - rails-dom-testing (~> 1.0, >= 1.0.5) + rails-dom-testing (~> 2.0) + actionpack (5.0.6) + actionview (= 5.0.6) + activesupport (= 5.0.6) + rack (~> 2.0) + rack-test (~> 0.6.3) + rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (4.2.10) - activesupport (= 4.2.10) + actionview (5.0.6) + activesupport (= 5.0.6) builder (~> 3.1) erubis (~> 2.7.0) - rails-dom-testing (~> 1.0, >= 1.0.5) + rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (4.2.10) - activesupport (= 4.2.10) - globalid (>= 0.3.0) - activemodel (4.2.10) - activesupport (= 4.2.10) - builder (~> 3.1) - activerecord (4.2.10) - activemodel (= 4.2.10) - activesupport (= 4.2.10) - arel (~> 6.0) - activerecord-postgis-adapter (3.1.5) - activerecord (~> 4.2) - rgeo-activerecord (>= 4.0.4) - activerecord-session_store (0.1.2) - actionpack (>= 4.0.0, < 5) - activerecord (>= 4.0.0, < 5) - railties (>= 4.0.0, < 5) - activesupport (4.2.10) + activejob (5.0.6) + activesupport (= 5.0.6) + globalid (>= 0.3.6) + activemodel (5.0.6) + activesupport (= 5.0.6) + activerecord (5.0.6) + activemodel (= 5.0.6) + activesupport (= 5.0.6) + arel (~> 7.0) + activerecord-postgis-adapter (4.0.5) + activerecord (~> 5.0.0) + rgeo-activerecord (~> 5.0.0) + activerecord-session_store (1.1.0) + actionpack (>= 4.0, < 5.2) + activerecord (>= 4.0, < 5.2) + multi_json (~> 1.11, >= 1.11.2) + rack (>= 1.5.2, < 3) + railties (>= 4.0, < 5.2) + activesupport (5.0.6) + concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.5.2) public_suffix (>= 2.0.2, < 4.0) apipie-rails (0.4.0) json rails (>= 4.0) - arel (6.0.4) + arel (7.1.4) bootstrap-will_paginate (1.0.0) will_paginate builder (3.2.3) @@ -106,17 +97,18 @@ GEM mini_mime (1.0.0) mini_portile2 (2.3.0) minitest (5.11.3) - multi_json (1.3.6) + multi_json (1.13.1) mustache (1.0.5) narray (0.6.0.4) - nokogiri (1.8.1) + nio4r (2.2.0) + nokogiri (1.8.2) mini_portile2 (~> 2.3.0) - passenger (5.1.12) + passenger (5.2.0) rack rake (>= 0.8.1) pg (0.21.0) - protected_attributes (1.1.4) - activemodel (>= 4.0.1, < 5.0) + protected_attributes_continued (1.3.0) + activemodel (~> 5.0) pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) @@ -124,40 +116,39 @@ GEM query_reviewer (0.1.6) rabl (0.13.1) activesupport (>= 2.3.14) - rack (1.6.8) + rack (2.0.4) rack-test (0.6.3) rack (>= 1.0) railroad (0.5.0) - rails (4.2.10) - actionmailer (= 4.2.10) - actionpack (= 4.2.10) - actionview (= 4.2.10) - activejob (= 4.2.10) - activemodel (= 4.2.10) - activerecord (= 4.2.10) - activesupport (= 4.2.10) - bundler (>= 1.3.0, < 2.0) - railties (= 4.2.10) - sprockets-rails - rails-deprecated_sanitizer (1.0.3) - activesupport (>= 4.2.0.alpha) - rails-dom-testing (1.0.9) - activesupport (>= 4.2.0, < 5.0) - nokogiri (~> 1.6) - rails-deprecated_sanitizer (>= 1.0.1) + rails (5.0.6) + actioncable (= 5.0.6) + actionmailer (= 5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) + activemodel (= 5.0.6) + activerecord (= 5.0.6) + activesupport (= 5.0.6) + bundler (>= 1.3.0) + railties (= 5.0.6) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) rails-html-sanitizer (1.0.3) loofah (~> 2.0) rails3-restful-authentication (3.0.1) - railties (4.2.10) - actionpack (= 4.2.10) - activesupport (= 4.2.10) + railties (5.0.6) + actionpack (= 5.0.6) + activesupport (= 5.0.6) + method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (12.3.0) recaptcha (0.3.4) - rgeo (0.5.3) - rgeo-activerecord (4.0.5) - activerecord (~> 4.2) + rgeo (0.5.0) + rgeo-activerecord (5.0.1) + activerecord (~> 5.0.0) rgeo (~> 0.3) rspec (3.7.0) rspec-core (~> 3.7.0) @@ -204,6 +195,9 @@ GEM trollop (2.1.2) tzinfo (1.2.4) thread_safe (~> 0.1) + websocket-driver (0.6.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) will_paginate (3.1.6) xpath (2.1.0) nokogiri (~> 1.3) @@ -227,19 +221,17 @@ DEPENDENCIES json json-schema memoist - multi_json (= 1.3.6) + multi_json narray (= 0.6.0.4) nokogiri passenger pg - protected_attributes - prototype-rails! - prototype_legacy_helper (= 0.0.0)! + protected_attributes_continued pry query_reviewer (= 0.1.6) rabl railroad (= 0.5.0) - rails (= 4.2.10) + rails (= 5.0.6) rails3-restful-authentication (~> 3.0.1) recaptcha (= 0.3.4) rgeo (~> 0.5.0) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 97ff8ada3..dfb418383 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -117,7 +117,7 @@ def log_searches(m, arg = params[:search]) search_info += "\nsearch string: \"" + (params['search'] || "") + "\"" search_info += "\nformat: " + (params['format'] || 'html') search_info += "\nSQL query: " + sql # method_object.call(arg).send(additional_method).to_sql - search_info += "\nall parameters: " + params.inspect + search_info += "\nall parameters: " + params.permit!.inspect logger.info(search_info) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3033d5e39..8eb186b50 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -44,7 +44,7 @@ def sortable(column, title = nil) title ||= column.titleize css_class = (column == sort_column) ? "current #{sort_direction}" : nil direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc" - link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class} + link_to title, params.permit(:sort, :direction, :page, :_).merge(:sort => column, :direction => direction, :page => nil), {:class => css_class} end def format_stat(record) @@ -65,7 +65,7 @@ def format_stat(record) # Call this to make a link inside a form that submits the form. def link_to_submit(*args, &block) - link_to_function (block_given? ? capture(&block) : args[0]), "jQuery(this).closest('form').submit()", args.extract_options! + link_to (block_given? ? capture(&block) : args[0]), "#", args.extract_options!.merge(onclick: "jQuery(this).closest('form').submit(); return false") end # Given a FormBuilder object `f`, a string `label`, an SQL table name From 1a9d9452b5cad2aab944a7a6f1d6a168d191e864 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Fri, 2 Feb 2018 11:28:43 -0600 Subject: [PATCH 02/44] Set PhusionPassenger version to 5.1.12 until we find out how to work with the latest (5.2.0) version. --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 090c363fa..84d4bbc98 100644 --- a/Gemfile +++ b/Gemfile @@ -82,7 +82,7 @@ end group :production do # gem "rmagick", "2.13.1" - gem "passenger" + gem "passenger", "5.1.12" end # API-related Gems: diff --git a/Gemfile.lock b/Gemfile.lock index d4c88ecff..936ab9069 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -103,7 +103,7 @@ GEM nio4r (2.2.0) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) - passenger (5.2.0) + passenger (5.1.12) rack rake (>= 0.8.1) pg (0.21.0) @@ -224,7 +224,7 @@ DEPENDENCIES multi_json narray (= 0.6.0.4) nokogiri - passenger + passenger (= 5.1.12) pg protected_attributes_continued pry From 4a4de926255e5190915c93e54ca75af09f1e3c14 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Fri, 2 Feb 2018 14:25:34 -0600 Subject: [PATCH 03/44] Make some changes to config files suggested by app:update task. --- config/application.rb | 2 +- config/boot.rb | 2 +- config/environment.rb | 2 +- config/environments/development.rb | 32 ++++++++++++++++---- config/environments/production.rb | 48 +++++++++++++++--------------- config/environments/test.rb | 9 ++++-- config/initializers/assets.rb | 10 +++---- 7 files changed, 64 insertions(+), 41 deletions(-) diff --git a/config/application.rb b/config/application.rb index 4d7430fc5..572a1da6b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,4 +1,4 @@ -require File.expand_path('../boot', __FILE__) +require_relative 'boot' require 'rails/all' diff --git a/config/boot.rb b/config/boot.rb index 6b750f00b..30f5120df 100644 --- a/config/boot.rb +++ b/config/boot.rb @@ -1,3 +1,3 @@ -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) require 'bundler/setup' # Set up gems listed in the Gemfile. diff --git a/config/environment.rb b/config/environment.rb index 01de3b5bb..0448b3ca0 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,5 @@ # Load the Rails application. -require File.expand_path('../application', __FILE__) +require_relative 'application' # Initialize the Rails application. Rails.application.initialize! diff --git a/config/environments/development.rb b/config/environments/development.rb index b55e2144b..fb101a451 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -9,13 +9,28 @@ # Do not eager load code on boot. config.eager_load = false - # Show full error reports and disable caching. - config.consider_all_requests_local = true - config.action_controller.perform_caching = false + # Show full error reports. + config.consider_all_requests_local = true + + # Enable/disable caching. By default caching is disabled. + if Rails.root.join('tmp/caching-dev.txt').exist? + config.action_controller.perform_caching = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + 'Cache-Control' => 'public, max-age=172800' + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false + config.action_mailer.perform_caching = false + # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log @@ -29,13 +44,20 @@ # Asset digests allow you to set far-future HTTP expiration dates on all assets, # yet still be able to expire them through the digest params. - config.assets.digest = true + ## config.assets.digest = true # Adds additional error checking when serving assets at runtime. # Checks for improperly declared sprockets dependencies. # Raises helpful error messages. - config.assets.raise_runtime_errors = true + ## config.assets.raise_runtime_errors = true + + # Suppress logger output for asset requests. + config.assets.quiet = true # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Use an evented file watcher to asynchronously detect changes in source code, + # routes, locales, etc. This feature depends on the listen gem. + # config.file_watcher = ActiveSupport::EventedFileUpdateChecker end diff --git a/config/environments/production.rb b/config/environments/production.rb index 96103997d..fc143dda9 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,7 +1,6 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. - # The production environment is meant for finished, "live" apps. # Code is not reloaded between requests. config.cache_classes = true @@ -15,18 +14,9 @@ config.consider_all_requests_local = false config.action_controller.perform_caching = true - # Enable Rack::Cache to put a simple HTTP cache in front of your application - # Add `rack-cache` to your Gemfile before enabling this. - # For large-scale production use, consider using a caching reverse proxy like - # NGINX, varnish or squid. - # config.action_dispatch.rack_cache = true - # Disable serving static files from the `/public` folder by default since # Apache or NGINX already handles this. - config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? - - # Compress JavaScripts and CSS - ## config.assets.compress = true + config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? # Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier @@ -35,16 +25,20 @@ # Do not fallback to assets pipeline if a precompiled asset is missed. ## config.assets.compile = false - # Asset digests allow you to set far-future HTTP expiration dates on all assets, - # yet still be able to expire them through the digest params. - config.assets.digest = true - # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + # Specifies the header that your server uses for sending files. # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + # Mount Action Cable outside main process or domain + # config.action_cable.mount_path = nil + # config.action_cable.url = 'wss://example.com/cable' + # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true @@ -53,10 +47,7 @@ config.log_level = :debug # Prepend all log lines with the following tags. - # config.log_tags = [ :subdomain, :uuid ] - - # Use a different logger for distributed setups. - # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) + config.log_tags = [ :request_id ] # Use a different cache store in production. # config.cache_store = :mem_cache_store @@ -64,8 +55,10 @@ # set assets path ## config.assets.prefix = (ENV['RAILS_RELATIVE_URL_ROOT'] || "") + "/assets" - # Enable serving of images, stylesheets, and JavaScripts from an asset server. - # config.action_controller.asset_host = 'http://assets.example.com' + # Use a real queuing backend for Active Job (and separate queues per environment) + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "bety_rails3_#{Rails.env}" + config.action_mailer.perform_caching = false # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. @@ -78,12 +71,19 @@ # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify - # Disable automatic flushing of the log to improve performance. - # config.autoflush_log = false - # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new + # Use a different logger for distributed setups. + # require 'syslog/logger' + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new(STDOUT) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false end diff --git a/config/environments/test.rb b/config/environments/test.rb index 2acb5eb97..ff7c69fda 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -12,9 +12,11 @@ # preloads Rails for running tests, you may have to set it to true. config.eager_load = false - # Configure static file server for tests with Cache-Control for performance. - config.serve_static_files = true - config.static_cache_control = 'public, max-age=3600' + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + 'Cache-Control' => 'public, max-age=3600' + } # Show full error reports and disable caching. config.consider_all_requests_local = true @@ -25,6 +27,7 @@ # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false + config.action_mailer.perform_caching = false # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index a459b7d5f..ff2d2299a 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -3,11 +3,9 @@ # Version of your assets, change this if you want to expire all your assets. # Rails.application.config.assets.version = '1.0' +# Add additional assets to the asset load path +# Rails.application.config.assets.paths << Emoji.images_path + # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. -Rails.application.config.assets.precompile += %w( - prototype.js - effects.js - controls.js - dragdrop.js -) +# Rails.application.config.assets.precompile += %w( search.js ) From 9aafb78af61d85d962c2be4e8e79074b67c06cc6 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Mon, 5 Feb 2018 13:51:07 -0600 Subject: [PATCH 04/44] A few changes to get some more spec to pass: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit app/controllers/api/v1/traits_controller.rb: Api::V1::TraitsController#create used to set the response content_type to “application/xml”. It seemed that this was overridden by the type of the template rendered but is no longer. So we should just not set it. Gemfile, Gemfile.lock: Had to add the “rails-controller-testing” to support tests which use the “assigns” method. app/controllers/bulk_upload_controller.rb: In the BulkUploadController#display_csv_file, we have to replace the test “if uploaded_io” with “if !uploaded_io.blank?” because the call "post 'display_csv_file', { 'CSV file' => nil }” in the bulk upload controller spec now results in the value of the “CSV file” parameter being “”:String instead of NilClass. --- Gemfile | 1 + Gemfile.lock | 5 +++++ app/controllers/api/v1/traits_controller.rb | 2 +- app/controllers/bulk_upload_controller.rb | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 84d4bbc98..742fa6901 100644 --- a/Gemfile +++ b/Gemfile @@ -60,6 +60,7 @@ group :test do # gem "webrat", "0.7.1" gem "capybara" gem "database_cleaner" + gem "rails-controller-testing" # TO-DO: rewrite the specs that rely on this end # If you have difficulty installing or don't wish to install capybara-webkit, diff --git a/Gemfile.lock b/Gemfile.lock index 936ab9069..ac61c05f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -132,6 +132,10 @@ GEM bundler (>= 1.3.0) railties (= 5.0.6) sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.2) + actionpack (~> 5.x, >= 5.0.1) + actionview (~> 5.x, >= 5.0.1) + activesupport (~> 5.x) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -232,6 +236,7 @@ DEPENDENCIES rabl railroad (= 0.5.0) rails (= 5.0.6) + rails-controller-testing rails3-restful-authentication (~> 3.0.1) recaptcha (= 0.3.4) rgeo (~> 0.5.0) diff --git a/app/controllers/api/v1/traits_controller.rb b/app/controllers/api/v1/traits_controller.rb index 9d5221092..e38c3bd93 100644 --- a/app/controllers/api/v1/traits_controller.rb +++ b/app/controllers/api/v1/traits_controller.rb @@ -38,7 +38,7 @@ def create end create_from_xml_string(xml_data) - render status: 201, content_type: "application/xml" + render status: 201 rescue Nokogiri::XML::SyntaxError, InvalidDocument, InvalidData, Yajl::ParseError, Api::CsvHandler::BadHeading diff --git a/app/controllers/bulk_upload_controller.rb b/app/controllers/bulk_upload_controller.rb index da331e7de..1486a103e 100644 --- a/app/controllers/bulk_upload_controller.rb +++ b/app/controllers/bulk_upload_controller.rb @@ -173,7 +173,7 @@ def choose_global_citation def display_csv_file uploaded_io = params["CSV file"] - if uploaded_io + if !uploaded_io.blank? # we got here via the "upload file" button @data_set = BulkUploadDataSet.new(session, uploaded_io) elsif session[:csvpath] From 271c6c35cd8f50edc4264963a681ee7ee1e86741 Mon Sep 17 00:00:00 2001 From: Rob Kooper Date: Wed, 7 Feb 2018 09:53:49 -0600 Subject: [PATCH 05/44] unicorn and bety root path Added gem + configuration to run bety using unicorn. This is a frontend to multiple instances of bety running, which will speed up the response time. Added ability to set the path to the bety application using an environment variable. --- Dockerfile | 18 +++++--- Gemfile | 4 ++ config.ru | 5 ++- config/unicorn.rb | 97 ++++++++++++++++++++++++++++++++++++++++++++ docker/entrypoint.sh | 7 +++- 5 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 config/unicorn.rb diff --git a/Dockerfile b/Dockerfile index e2fa2de5b..f485e4ac1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,4 @@ -FROM ruby:2.1 -MAINTAINER Max Burnette +FROM ruby:2.3 # Install dependencies RUN apt-get update \ @@ -15,11 +14,12 @@ WORKDIR /home/bety # install gems (allowing for caching) COPY /Gemfile* /home/bety/ -RUN gem install bundler && \ - bundle install --without javascript_testing --path vendor/bundle +RUN gem install bundler \ + && bundle install --with docker --without "test development production debug javascript_testing" + # port that is exposed (standard ruby port) -EXPOSE 3000 +EXPOSE 8000 # copy rest of the files COPY / /home/bety @@ -41,11 +41,17 @@ ARG BETY_GIT_DATE="unknown" ENV LOCAL_SERVER=99 \ REMOTE_SERVERS="0 1 2 5" \ RAILS_ENV="production" \ + RAILS_RELATIVE_URL_ROOT="" \ + UNICORN_WORKER_PROCESSES="3" \ + UNICORN_PORT="8000" \ BETY_GIT_TAGS=${BETY_GIT_TAGS} \ BETY_GIT_BRANCH=${BETY_GIT_BRANCH} \ BETY_GIT_CHECKSUM=${BETY_GIT_CHECKSUM} \ BETY_GIT_DATE=${BETY_GIT_DATE} +# expose public files +VOLUME ["/home/bety/public"] + # default command to run bety web-app ENTRYPOINT ["/home/bety/docker/entrypoint.sh"] -CMD ["server"] +CMD ["unicorn"] diff --git a/Gemfile b/Gemfile index 5286132ba..64d06704f 100644 --- a/Gemfile +++ b/Gemfile @@ -82,6 +82,10 @@ group :production do gem "passenger" end +group :docker do + gem 'unicorn' +end + # API-related Gems: gem "rspec_api_documentation" diff --git a/config.ru b/config.ru index 52aeeab9d..6e715ca17 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,7 @@ # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) -run BetyRails3::Application + +map (ENV['RAILS_RELATIVE_URL_ROOT'] || "") + "/" do + run BetyRails3::Application +end diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 000000000..a19c1d143 --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,97 @@ +# Sample verbose configuration file for Unicorn (not Rack) +# +# This configuration file documents many features of Unicorn +# that may not be needed for some applications. See +# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb +# for a much simpler configuration file. +# +# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete +# documentation. + +# Go with at least 1 per CPU core, a higher amount will usually help for fast +# responses such as reading from a cache. +worker_processes ENV['UNICORN_WORKER_PROCESSES'].to_i + +# Help ensure your application will always spawn in the symlinked +# "current" directory that Capistrano sets up. +#working_directory "/home/bety" # available in 0.94.0+ + +# feel free to point this anywhere accessible on the filesystem +#pid "/home/bety/tmp/pids/unicorn.pid" + +# Listen on both a Unix domain socket and a TCP port. +# If you are load-balancing multiple Unicorn masters, lower the backlog +# setting to e.g. 64 for faster failover. +#listen "/home/git/gitlab/tmp/sockets/gitlab.socket", :backlog => 1024 +#listen "127.0.0.1:8080", :tcp_nopush => true +listen "0.0.0.0:" + ENV['UNICORN_PORT'] + +# nuke workers after 30 seconds instead of 60 seconds (the default) +timeout 30 + +# By default, the Unicorn logger will write to stderr. +# Additionally, some applications/frameworks log to stderr or stdout, +# so prevent them from going to /dev/null when daemonized here: +#stderr_path "/home/bety/log/unicorn.stderr.log" +#stdout_path "/home/bety/log/unicorn.stdout.log" + +# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings +# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow +preload_app true +GC.respond_to?(:copy_on_write_friendly=) and + GC.copy_on_write_friendly = true + +# Enable this flag to have unicorn test client connections by writing the +# beginning of the HTTP headers before calling the application. This +# prevents calling the application for connections that have disconnected +# while queued. This is only guaranteed to detect clients on the same +# host unicorn runs on, and unlikely to detect disconnects even on a +# fast LAN. +check_client_connection false + +before_fork do |server, worker| + # the following is highly recommended for Rails + "preload_app true" + # as there's no need for the master process to hold a connection + defined?(ActiveRecord::Base) and + ActiveRecord::Base.connection.disconnect! + + # The following is only recommended for memory/DB-constrained + # installations. It is not needed if your system can house + # twice as many worker_processes as you have configured. + # + # This allows a new master process to incrementally + # phase out the old master process with SIGTTOU to avoid a + # thundering herd (especially in the "preload_app false" case) + # when doing a transparent upgrade. The last worker spawned + # will then kill off the old master process with a SIGQUIT. + old_pid = "#{server.config[:pid]}.oldbin" + if old_pid != server.pid + begin + sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU + Process.kill(sig, File.read(old_pid).to_i) + rescue Errno::ENOENT, Errno::ESRCH + end + end + # + # Throttle the master from forking too quickly by sleeping. Due + # to the implementation of standard Unix signal handlers, this + # helps (but does not completely) prevent identical, repeated signals + # from being lost when the receiving process is busy. + # sleep 1 +end + +after_fork do |server, worker| + # per-process listener ports for debugging/admin/migrations + # addr = "127.0.0.1:#{9293 + worker.nr}" + # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) + + # the following is *required* for Rails + "preload_app true", + defined?(ActiveRecord::Base) and + ActiveRecord::Base.establish_connection + + # if preload_app is true, then you may also want to check and + # restart any other shared sockets/descriptors such as Memcached, + # and Redis. TokyoCabinet file handles are safe to reuse + # between any number of forked children (assuming your kernel + # correctly implements pread()/pwrite() system calls) +end diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 83ba8859e..7161d14f1 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -46,9 +46,14 @@ case $1 in ;; "server" ) wait_for_postgres - echo "Start running BETY" + echo "Start running BETY (rails server)" exec rails s ;; + "unicorn" ) + wait_for_postgres + echo "Start running BETY (unicorn)" + exec bundle exec unicorn -c config/unicorn.rb + ;; "help" ) echo "initialize : create a new database and initialize with data from server 0" echo "sync : synchronize database with remote servers ${REMOTE_SERVERS}" From e7d40ca69f6c0b31e2dec79a1c3ffcb9d51cd322 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Thu, 8 Feb 2018 15:04:49 -0600 Subject: [PATCH 06/44] Wrote replacement for obsolete Prototype method "observe_field", and added machinery to make it work for the case of species search. --- app/controllers/species_controller.rb | 23 +++++------------------ app/helpers/application_helper.rb | 20 ++++++++++++++++++++ app/views/species/species_search.js.erb | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 app/views/species/species_search.js.erb diff --git a/app/controllers/species_controller.rb b/app/controllers/species_controller.rb index 03a624afb..deae39c13 100644 --- a/app/controllers/species_controller.rb +++ b/app/controllers/species_controller.rb @@ -53,27 +53,14 @@ def species_search {:query => @query + "%", :query2 => "%" + @query + "%"}).limit(100).order("scientificname") @species.uniq! else - @species = [] + @species = Specie.none end - render :update do |page| - if params[:cont] == "species" - page.replace_html "search_results", :partial => 'search', :object => @species - else - if @species.empty? - page.replace_html "#{params[:cont]}_specie_id", "" - else - page.replace_html "#{params[:cont]}_specie_id", options_from_collection_for_select(@species, :id, :select_default) - end - end - if params[:symbol].length > 3 - page.replace_html "results", "

Search results for #{sanitize(@query)}

" - else - page.replace_html "results", "

Search must be longer then 3 characters

" - end + respond_to do |format| + format.js { + render layout: false + } end - -# render :partial => 'species/species_search' end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8eb186b50..12a9e9999 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -155,3 +155,23 @@ def commit_date e = ENV['BETY_GIT_DATE'] e.nil? || e.empty? ? `git log --pretty=format:"%ad" -1` : e end + +# Replacement for the Prototype method of this name. +def observe_field(element_id, **options) + raw( + %Q{} + ).html_safe +end + +# Dummy method to replace Prototype method of this name. Needs to be fleshed +# out. +def submit_to_remote(a, b, c) + 'Create' +end diff --git a/app/views/species/species_search.js.erb b/app/views/species/species_search.js.erb new file mode 100644 index 000000000..6655a518b --- /dev/null +++ b/app/views/species/species_search.js.erb @@ -0,0 +1,17 @@ +<% options = escape_javascript options_from_collection_for_select(@species, :id, :select_default) %> + +<% if params[:cont] == "species" %> + jQuery('#search_results').html(<%= @species %>) +<% else %> + <% if @species.empty? == true %> + jQuery("<%= "#" + params[:cont] + "_specie_id" %>").html("") + <% else %> + jQuery("<%= "#" + params[:cont] + "_specie_id" %>").html('<%= options %>') + <% end %> +<% end %> +<% if params[:symbol].length > 3 %> + jQuery("#results").html("

Search results for <%= sanitize(@query) %>

") +<% else %> + jQuery("#results").html("

Search must be longer then 3 characters

") +<% end %> + From 95bf0a697ed0d99eb6e800a8d0f4db604f0cb0d6 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Fri, 9 Feb 2018 11:00:09 -0600 Subject: [PATCH 07/44] Updated capybara to fix problems mentioned here: https://github.com/teamcapybara/capybara/issues/1592 --- Gemfile | 4 ++-- Gemfile.lock | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 742fa6901..7f686af6a 100644 --- a/Gemfile +++ b/Gemfile @@ -58,7 +58,7 @@ end group :test do # phasing out webrat: # gem "webrat", "0.7.1" - gem "capybara" + gem "capybara", "~> 2.8" gem "database_cleaner" gem "rails-controller-testing" # TO-DO: rewrite the specs that rely on this end @@ -69,7 +69,7 @@ end # bundle install --without javascript_testing # group :javascript_testing do - gem "capybara-webkit", "1.7.1" + gem "capybara-webkit", "~>1.7" end # This group is used by RSpec if the environment variable RAILS_DEBUG is set to diff --git a/Gemfile.lock b/Gemfile.lock index ac61c05f8..fff127821 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,14 +56,15 @@ GEM bootstrap-will_paginate (1.0.0) will_paginate builder (3.2.3) - capybara (2.5.0) + capybara (2.13.0) + addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - capybara-webkit (1.7.1) - capybara (>= 2.3.0, < 2.6.0) + capybara-webkit (1.14.0) + capybara (>= 2.3.0, < 2.14.0) json childprocess (0.8.0) ffi (~> 1.0, >= 1.0.11) @@ -216,8 +217,8 @@ DEPENDENCIES activerecord-session_store apipie-rails bootstrap-will_paginate - capybara - capybara-webkit (= 1.7.1) + capybara (~> 2.8) + capybara-webkit (~> 1.7) choice (= 0.1.6) comma (= 3.0.4) database_cleaner From bf247994fb5805e4965d3f7354372f40bdf89172 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Fri, 9 Feb 2018 13:35:30 -0600 Subject: [PATCH 08/44] Rewrite observe_field for the case where options[:url] contains a key for :id. In this case url_for already contains part of the query string and we must append the rest using "&" rather than "?". --- app/helpers/application_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 12a9e9999..340f3d6a8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -163,7 +163,8 @@ def observe_field(element_id, **options) jQuery(document).ready(function() { jQuery("##{element_id.to_s}").bind("keyup", function() { var queryString = #{options[:with]} - jQuery.post("#{url_for(options[:url])}" + '?' + queryString) + var connector = "#{options[:url].has_key?(:id) ? '&' : '?'}" + jQuery.post("#{url_for(options[:url])}" + connector + queryString) }) }) } From 61091b1490ed2b384c83f313d9e36b9501915dcc Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Fri, 9 Feb 2018 13:38:06 -0600 Subject: [PATCH 09/44] Rewrite Inputs controller to render a js.erb template rather than using "render :update". Added said js.erb template. --- app/controllers/inputs_controller.rb | 8 +++++--- app/views/inputs/edit_inputs_files.js.erb | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 app/views/inputs/edit_inputs_files.js.erb diff --git a/app/controllers/inputs_controller.rb b/app/controllers/inputs_controller.rb index 2798af258..2f09f2cc4 100644 --- a/app/controllers/inputs_controller.rb +++ b/app/controllers/inputs_controller.rb @@ -69,9 +69,11 @@ def edit_inputs_files @files = DBFile.where(search_cond).select("id, file_name").page(params[:page]) end - render :update do |page| - page.replace_html :files_index_table, :partial => "edit_inputs_files_table" - page.replace_html :files_search_term, search + respond_to do |format| + format.js { + @search = search + render layout: false + } end end diff --git a/app/views/inputs/edit_inputs_files.js.erb b/app/views/inputs/edit_inputs_files.js.erb new file mode 100644 index 000000000..83e30a74c --- /dev/null +++ b/app/views/inputs/edit_inputs_files.js.erb @@ -0,0 +1,4 @@ +<% Rails.logger.debug "@files = #{@files.to_a}" %> +jQuery('#files_index_table').html("<%= escape_javascript(render 'edit_inputs_files_table', files: @files, input: @input, searchparam: @searchparam) %>") + +jQuery('#files_search_term').html("<%= @search %>") From d9aeea8d029ee98cbcf649b380c1fad99eb37781 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Fri, 9 Feb 2018 16:05:11 -0600 Subject: [PATCH 10/44] Continue modifying controllers to get rid of the "render :update do |page|" idiom. Added needed js.erb templates. Rewrote observe_field method (in application_helper.rb) to be more resilient. --- app/controllers/inputs_controller.rb | 3 +- app/controllers/models_controller.rb | 8 +++-- app/controllers/pfts_controller.rb | 7 +++-- app/helpers/application_helper.rb | 36 ++++++++++++++++------- app/views/inputs/edit_inputs_files.js.erb | 2 +- app/views/models/edit_models_files.js.erb | 3 ++ app/views/pfts/edit2_pfts_species.js.erb | 3 ++ 7 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 app/views/models/edit_models_files.js.erb create mode 100644 app/views/pfts/edit2_pfts_species.js.erb diff --git a/app/controllers/inputs_controller.rb b/app/controllers/inputs_controller.rb index 2f09f2cc4..e40fc6c63 100644 --- a/app/controllers/inputs_controller.rb +++ b/app/controllers/inputs_controller.rb @@ -71,8 +71,7 @@ def edit_inputs_files respond_to do |format| format.js { - @search = search - render layout: false + render layout: false, locals: { search: search } } end end diff --git a/app/controllers/models_controller.rb b/app/controllers/models_controller.rb index 48bea5e8a..3528f57e0 100644 --- a/app/controllers/models_controller.rb +++ b/app/controllers/models_controller.rb @@ -52,9 +52,11 @@ def edit_models_files @files = DBFile.where(search_cond).select("id,file_name").page(params[:page]) end - render :update do |page| - page.replace_html :files_index_table, :partial => "edit_models_files_table" - page.replace_html :files_search_term, search + + respond_to do |format| + format.js { + render layout: false, locals: { search: search } + } end end diff --git a/app/controllers/pfts_controller.rb b/app/controllers/pfts_controller.rb index 6ac964bd3..c2d874ea9 100644 --- a/app/controllers/pfts_controller.rb +++ b/app/controllers/pfts_controller.rb @@ -96,9 +96,10 @@ def edit2_pfts_species @species = Specie.where(search_cond).select("id, scientificname").page(params[:page]) end - render :update do |page| - page.replace_html :index_table, :partial => "edit2_pfts_species_table" - page.replace_html :search_term, search + respond_to do |format| + format.js { + render layout: false, locals: { search: search } + } end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 340f3d6a8..ee0012d6f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -158,17 +158,31 @@ def commit_date # Replacement for the Prototype method of this name. def observe_field(element_id, **options) - raw( - %Q{} - ).html_safe + # We assume options has either the key :url or the key :function (but not + # both). + if options.has_key? :url + url = url_for(options[:url]) + connector = url.match(/\?/) ? '&' : '?' + raw( + %Q{} + ).html_safe + else + raw( + %Q{} + ).html_safe + end end # Dummy method to replace Prototype method of this name. Needs to be fleshed diff --git a/app/views/inputs/edit_inputs_files.js.erb b/app/views/inputs/edit_inputs_files.js.erb index 83e30a74c..a870ce0c8 100644 --- a/app/views/inputs/edit_inputs_files.js.erb +++ b/app/views/inputs/edit_inputs_files.js.erb @@ -1,4 +1,4 @@ <% Rails.logger.debug "@files = #{@files.to_a}" %> jQuery('#files_index_table').html("<%= escape_javascript(render 'edit_inputs_files_table', files: @files, input: @input, searchparam: @searchparam) %>") -jQuery('#files_search_term').html("<%= @search %>") +jQuery('#files_search_term').html("<%= search %>") diff --git a/app/views/models/edit_models_files.js.erb b/app/views/models/edit_models_files.js.erb new file mode 100644 index 000000000..1ccf1d780 --- /dev/null +++ b/app/views/models/edit_models_files.js.erb @@ -0,0 +1,3 @@ +jQuery('#files_index_table').html("<%= escape_javascript(render 'edit_models_files_table', files: @files, model: @model, searchparam: @searchparam) %>") + +jQuery('#files_search_term').html("<%= search %>") diff --git a/app/views/pfts/edit2_pfts_species.js.erb b/app/views/pfts/edit2_pfts_species.js.erb new file mode 100644 index 000000000..8eb447348 --- /dev/null +++ b/app/views/pfts/edit2_pfts_species.js.erb @@ -0,0 +1,3 @@ +jQuery('#index_table').html("<%= escape_javascript(render 'edit2_pfts_species_table', species: @species, pft: @pft, search: @search) %>") + +jQuery('#search_term').html("<%= search %>") From 9cecbdda5bfa366942f45b4affb689854a817971 Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Mon, 12 Feb 2018 15:53:24 -0600 Subject: [PATCH 11/44] Make expectation to find the word 'planting' in the page more specific: It should be not just in the page, but in the list of managements relationships. --- spec/features/treatment_integration_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/treatment_integration_spec.rb b/spec/features/treatment_integration_spec.rb index 01ab0f585..d5dc634d1 100644 --- a/spec/features/treatment_integration_spec.rb +++ b/spec/features/treatment_integration_spec.rb @@ -58,7 +58,7 @@ click_link 'View Related Managements' page.select 'planting', from: 'management_id' click_button 'Select' - expect(page).to have_content 'planting' + expect(page.find('table table')).to have_content 'planting' # now do clean-up: page.find(:xpath, ".//table/tbody/tr[preceding-sibling::tr/th/text() = 'Type'][contains(td[last()], 'planting')]/td/a[text() = 'X']").click From 9544998f6ce39961b7f42c3f532abefab563148c Mon Sep 17 00:00:00 2001 From: Scott Rohde Date: Wed, 14 Feb 2018 16:27:23 -0600 Subject: [PATCH 12/44] Continue modifying controllers to get rid of the "render :update do |page|" idiom; added needed js.erb templates. Miscellaneous other minor changes. --- app/controllers/sitegroups_controller.rb | 7 ++- app/controllers/traits_controller.rb | 7 +-- app/controllers/treatments_controller.rb | 60 +++++++++---------- app/helpers/application_helper.rb | 6 -- app/views/inputs/edit_inputs_files.js.erb | 1 - .../sitegroups/edit_sitegroups_sites.js.erb | 3 + app/views/species/species_search.js.erb | 1 + app/views/traits/unlink_covariate.js.erb | 1 + app/views/treatments/_alert.html.erb | 8 +++ app/views/treatments/_new_management.html.erb | 4 +- .../treatments/create_new_management.js.erb | 15 +++++ app/views/treatments/edit.html.erb | 2 +- .../edit_managements_treatments.js.erb | 1 + spec/features/site_integration_spec.rb | 2 +- 14 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 app/views/sitegroups/edit_sitegroups_sites.js.erb create mode 100644 app/views/traits/unlink_covariate.js.erb create mode 100644 app/views/treatments/_alert.html.erb create mode 100644 app/views/treatments/create_new_management.js.erb create mode 100644 app/views/treatments/edit_managements_treatments.js.erb diff --git a/app/controllers/sitegroups_controller.rb b/app/controllers/sitegroups_controller.rb index 55ecf4666..722adb6eb 100644 --- a/app/controllers/sitegroups_controller.rb +++ b/app/controllers/sitegroups_controller.rb @@ -52,9 +52,10 @@ def edit_sitegroups_sites @sites = Site.where(search_cond).paginate :page => params[:page] end - render :update do |page| - page.replace_html :sites_index_table, :partial => "edit_sitegroups_sites_table" - page.replace_html :sites_search_term, search + respond_to do |format| + format.js { + render layout: false, locals: { search: search } + } end end diff --git a/app/controllers/traits_controller.rb b/app/controllers/traits_controller.rb index 6b873e369..5de806007 100644 --- a/app/controllers/traits_controller.rb +++ b/app/controllers/traits_controller.rb @@ -254,11 +254,10 @@ def unlink_covariate @trait = Trait.all_limited(current_user).find(params[:id]) Covariate.find(params[:covariate]).destroy respond_to do |format| - format.html { - render :action =>"edit" do |page| - page.replace_html "edit_covariates_traits", :partial =>"edit_covariates_traits" - end + format.js { + render layout: false } + format.html { head :ok } format.xml { head :ok } format.csv { head :ok } end diff --git a/app/controllers/treatments_controller.rb b/app/controllers/treatments_controller.rb index b9aac1311..75674870b 100644 --- a/app/controllers/treatments_controller.rb +++ b/app/controllers/treatments_controller.rb @@ -50,24 +50,17 @@ def create_new_management @management = Management.new(params[:management]) - render :update do |page| - if @management.save - @treatment.managements << @management - flash[:notice] = "Management was successfully created" - page.replace_html 'edit_managements_treatments', :partial => 'edit_managements_treatments' - - # After we've added the newly-created management to the collection, we - # have to pass an unsaved copy of it to the 'new_management' partial so the - # form it contains doesn't try to to a PUT instead of a POST: - @management = @management.dup - - page.replace_html 'new_management', :partial => 'new_management' - page.call 'showHide', 'new_management' - else - flash[:notice] = "Management was not created" - page.replace_html 'edit_managements_treatments', :partial => 'edit_managements_treatments' - page.replace_html 'new_management', :partial => 'new_management' - end + if @management.save + @treatment.managements << @management + flash.now[:notice] = "Management was successfully created" + else + flash.now[:error] = "Management was not created" + end + + respond_to do |format| + format.js { + render layout: false, locals: { flash: flash } + } end end @@ -75,12 +68,12 @@ def rem_managements_treatments @treatment = Treatment.find(params[:id]) @management = Management.find(params[:management]) - render :update do |page| - if @management.treatments.delete(@treatment) - page.replace_html 'edit_managements_treatments', :partial => 'edit_managements_treatments' - else - page.replace_html 'edit_managements_treatments', :partial => 'edit_managements_treatments' - end + @management.treatments.delete(@treatment) + + respond_to do |format| + format.js { + render "edit_managements_treatments", layout: false + } end end @@ -88,17 +81,18 @@ def edit_managements_treatments @treatment = Treatment.find(params[:id]) - render :update do |page| - if !params[:management].nil? - params[:management][:id].each do |c| - next if c.empty? - @treatment.managements << Management.find(c) - end - page.replace_html 'edit_managements_treatments', :partial => 'edit_managements_treatments' - else - page.replace_html 'edit_managements_treatments', :partial => 'edit_managements_treatments' + if !params[:management].nil? + params[:management][:id].each do |c| + next if c.empty? + @treatment.managements << Management.find(c) end end + + respond_to do |format| + format.js { + render layout: false + } + end end def flag_control diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ee0012d6f..132d37bef 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -184,9 +184,3 @@ def observe_field(element_id, **options) ).html_safe end end - -# Dummy method to replace Prototype method of this name. Needs to be fleshed -# out. -def submit_to_remote(a, b, c) - 'Create' -end diff --git a/app/views/inputs/edit_inputs_files.js.erb b/app/views/inputs/edit_inputs_files.js.erb index a870ce0c8..f52c48d5a 100644 --- a/app/views/inputs/edit_inputs_files.js.erb +++ b/app/views/inputs/edit_inputs_files.js.erb @@ -1,4 +1,3 @@ -<% Rails.logger.debug "@files = #{@files.to_a}" %> jQuery('#files_index_table').html("<%= escape_javascript(render 'edit_inputs_files_table', files: @files, input: @input, searchparam: @searchparam) %>") jQuery('#files_search_term').html("<%= search %>") diff --git a/app/views/sitegroups/edit_sitegroups_sites.js.erb b/app/views/sitegroups/edit_sitegroups_sites.js.erb new file mode 100644 index 000000000..eedf99105 --- /dev/null +++ b/app/views/sitegroups/edit_sitegroups_sites.js.erb @@ -0,0 +1,3 @@ +jQuery('#sites_index_table').html("<%= escape_javascript(render 'edit_sitegroups_sites_table', sitegroup: @sitegroup, sites: @sites, searchparam: @searchparam) %>") + +jQuery('#search_term').html("<%= search %>") diff --git a/app/views/species/species_search.js.erb b/app/views/species/species_search.js.erb index 6655a518b..88c0c5b79 100644 --- a/app/views/species/species_search.js.erb +++ b/app/views/species/species_search.js.erb @@ -1,6 +1,7 @@ <% options = escape_javascript options_from_collection_for_select(@species, :id, :select_default) %> <% if params[:cont] == "species" %> + // This branch may be obsolete. jQuery('#search_results').html(<%= @species %>) <% else %> <% if @species.empty? == true %> diff --git a/app/views/traits/unlink_covariate.js.erb b/app/views/traits/unlink_covariate.js.erb new file mode 100644 index 000000000..f14cf7488 --- /dev/null +++ b/app/views/traits/unlink_covariate.js.erb @@ -0,0 +1 @@ +jQuery('#edit_covariates_traits').html("<%= escape_javascript(render 'edit_covariates_traits', trait: @trait) %>") diff --git a/app/views/treatments/_alert.html.erb b/app/views/treatments/_alert.html.erb new file mode 100644 index 000000000..177dd0907 --- /dev/null +++ b/app/views/treatments/_alert.html.erb @@ -0,0 +1,8 @@ +<% flash.each do |key, value| %> + <%= content_tag(:div, class: "alert alert-#{key} fade in centered") do %> +
+ + <%= value %> +
+ <% end %> +<% end %> diff --git a/app/views/treatments/_new_management.html.erb b/app/views/treatments/_new_management.html.erb index cb34df395..f8140e648 100644 --- a/app/views/treatments/_new_management.html.erb +++ b/app/views/treatments/_new_management.html.erb @@ -1,6 +1,6 @@

New management

-<%= form_for(@management) do |f| %> +<%= form_for @management, :url => create_new_management_treatments_path, :remote => true do |f| %> <%= f.error_messages %> @@ -41,7 +41,7 @@

<%= f.hidden_field :id, :value => @treatment.id %> - <%= submit_to_remote f, 'Create', :url => { :action => "create_new_management" } %> + <%= button_tag 'Create', :type => 'submit' %>

diff --git a/app/views/treatments/create_new_management.js.erb b/app/views/treatments/create_new_management.js.erb new file mode 100644 index 000000000..8616301ed --- /dev/null +++ b/app/views/treatments/create_new_management.js.erb @@ -0,0 +1,15 @@ +jQuery('#edit_managements_treatments').html("<%= escape_javascript(render 'edit_managements_treatments', management: @management, treatment: @treatment) %>") + +<% if @management.persisted? %> + <% @management = @management.dup %> + jQuery('#new_management').slideUp(800); +<% end %> + +jQuery('#new_management').html("<%= escape_javascript(render 'new_management', management: @management, treatment: @treatment) %>"); + +jQuery('div.alert').detach(); +jQuery('div.nav').after("<%= escape_javascript(render 'alert', flash: flash) %>") + +jQuery('button.close').on('click', function(){ + jQuery(this).closest('div.alert').hide(); +}) diff --git a/app/views/treatments/edit.html.erb b/app/views/treatments/edit.html.erb index fd6f19d21..12d515bc5 100644 --- a/app/views/treatments/edit.html.erb +++ b/app/views/treatments/edit.html.erb @@ -11,7 +11,7 @@ <%= render 'edit_managements_treatments' %>
- [+] Create New Management + [+] Create New Management