From 4423d6c42a25275589350eb3b15ee4d8be2e140d Mon Sep 17 00:00:00 2001 From: Helge Rausch Date: Thu, 7 Feb 2013 17:14:53 +0100 Subject: [PATCH] upgrading Rails to 3.1 --- Gemfile | 2 +- Gemfile.lock | 146 ++++++++++-------- app/controllers/genotypes_controller.rb | 2 +- app/controllers/phenotypes_controller.rb | 98 ++++-------- app/models/genotype.rb | 5 +- .../genotypes/{rss.rxml => rss.xml.builder} | 0 .../{paper_rss.rxml => paper_rss.xml.builder} | 0 .../phenotypes/{rss.rxml => rss.xml.builder} | 0 .../{rss.rxml => rss.xml.builder} | 0 config/environments/development.rb | 1 - lib/phenotype_recommender.rb | 6 + lib/variation_recommender.rb | 6 + test/fixtures/.gitkeep | 0 test/functional/genotypes_controller_test.rb | 19 ++- test/functional/phentoypes_controller_test.rb | 6 +- test/test_helper.rb | 2 + test/unit/genotype_test.rb | 15 ++ 17 files changed, 160 insertions(+), 148 deletions(-) rename app/views/genotypes/{rss.rxml => rss.xml.builder} (100%) rename app/views/news/{paper_rss.rxml => paper_rss.xml.builder} (100%) rename app/views/phenotypes/{rss.rxml => rss.xml.builder} (100%) rename app/views/picture_phenotypes/{rss.rxml => rss.xml.builder} (100%) create mode 100644 lib/phenotype_recommender.rb create mode 100644 lib/variation_recommender.rb create mode 100644 test/fixtures/.gitkeep create mode 100644 test/unit/genotype_test.rb diff --git a/Gemfile b/Gemfile index 1d67af0..f71e9d4 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'http://rubygems.org' -gem 'rails', '~> 3.0.20' +gem 'rails', '~> 3.1.0' gem 'authlogic' # lots of user-related magic gem 'rails3-generators' gem "jquery-rails" diff --git a/Gemfile.lock b/Gemfile.lock index b93e6d1..27ad5f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,7 +14,7 @@ GIT GIT remote: git://github.com/ryanb/nested_form.git - revision: f6744578d25a73c23608668fde2a20f7ae9d7d77 + revision: 8655e352c8f8dd26a6918e72f6d152954b65676a specs: nested_form (0.3.1) @@ -38,43 +38,44 @@ GIT GEM remote: http://rubygems.org/ specs: - abstract (1.0.0) - actionmailer (3.0.20) - actionpack (= 3.0.20) - mail (~> 2.2.19) - actionpack (3.0.20) - activemodel (= 3.0.20) - activesupport (= 3.0.20) - builder (~> 2.1.2) - erubis (~> 2.6.6) - i18n (~> 0.5.0) - rack (~> 1.2.5) - rack-mount (~> 0.6.14) - rack-test (~> 0.5.7) - tzinfo (~> 0.3.23) - activemodel (3.0.20) - activesupport (= 3.0.20) - builder (~> 2.1.2) - i18n (~> 0.5.0) - activerecord (3.0.20) - activemodel (= 3.0.20) - activesupport (= 3.0.20) - arel (~> 2.0.10) - tzinfo (~> 0.3.23) + actionmailer (3.1.10) + actionpack (= 3.1.10) + mail (~> 2.3.3) + actionpack (3.1.10) + activemodel (= 3.1.10) + activesupport (= 3.1.10) + builder (~> 3.0.0) + erubis (~> 2.7.0) + i18n (~> 0.6) + rack (~> 1.3.6) + rack-cache (~> 1.2) + rack-mount (~> 0.8.2) + rack-test (~> 0.6.1) + sprockets (~> 2.0.4) + activemodel (3.1.10) + activesupport (= 3.1.10) + builder (~> 3.0.0) + i18n (~> 0.6) + activerecord (3.1.10) + activemodel (= 3.1.10) + activesupport (= 3.1.10) + arel (~> 2.2.3) + tzinfo (~> 0.3.29) activerecord-import (0.2.11) activerecord (~> 3.0) activerecord (~> 3.0) - activeresource (3.0.20) - activemodel (= 3.0.20) - activesupport (= 3.0.20) - activesupport (3.0.20) - arel (2.0.10) + activeresource (3.1.10) + activemodel (= 3.1.10) + activesupport (= 3.1.10) + activesupport (3.1.10) + multi_json (>= 1.0, < 1.3) + arel (2.2.3) authlogic (3.2.0) activerecord (>= 3.0.0) activesupport (>= 3.0.0) bartt-ssl_requirement (1.4.2) bcrypt-ruby (3.0.1) - builder (2.1.2) + builder (3.0.4) capistrano (2.14.1) highline net-scp (>= 1.0.0) @@ -83,27 +84,26 @@ GEM net-ssh-gateway (>= 1.1.0) cocaine (0.4.2) columnize (0.3.6) - debugger (1.3.0) + debugger (1.3.1) columnize (>= 0.3.1) debugger-linecache (~> 1.1.1) - debugger-ruby_core_source (~> 1.1.7) + debugger-ruby_core_source (~> 1.1.8) debugger-linecache (1.1.2) debugger-ruby_core_source (>= 1.1.1) - debugger-ruby_core_source (1.1.7) - erubis (2.6.6) - abstract (>= 1.0.0) + debugger-ruby_core_source (1.1.8) + erubis (2.7.0) factory_girl (4.2.0) activesupport (>= 3.0.0) fitgem (0.5.2) oauth highline (1.6.15) - i18n (0.5.0) + hike (1.2.1) + i18n (0.6.1) jquery-rails (2.2.0) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) json (1.7.6) - mail (2.2.19) - activesupport (>= 2.3.6) + mail (2.3.3) i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) @@ -111,19 +111,19 @@ GEM activesupport rest-client (>= 1.3.0) metaclass (0.0.1) - mime-types (1.19) + mime-types (1.20.1) minitest (4.3.3) mocha (0.13.2) metaclass (~> 0.0.1) - multi_json (1.5.0) - net-scp (1.0.4) - net-ssh (>= 1.99.1) - net-sftp (2.0.5) - net-ssh (>= 2.0.9) - net-ssh (2.6.3) - net-ssh-gateway (1.1.0) - net-ssh (>= 1.99.1) - newrelic_rpm (3.5.5.38) + multi_json (1.2.0) + net-scp (1.1.0) + net-ssh (>= 2.6.5) + net-sftp (2.1.1) + net-ssh (>= 2.6.5) + net-ssh (2.6.5) + net-ssh-gateway (1.2.0) + net-ssh (>= 2.6.5) + newrelic_rpm (3.5.6.46) nokogiri (1.5.6) oauth (0.4.7) paperclip (3.4.0) @@ -135,29 +135,36 @@ GEM pg (0.14.1) polyglot (0.3.3) pr_geohash (1.0.0) - rack (1.2.7) - rack-mount (0.6.14) + rack (1.3.9) + rack-cache (1.2) + rack (>= 0.4) + rack-mount (0.8.3) rack (>= 1.0.0) - rack-test (0.5.7) + rack-protection (1.3.2) + rack + rack-ssl (1.3.3) + rack + rack-test (0.6.2) rack (>= 1.0) - rails (3.0.20) - actionmailer (= 3.0.20) - actionpack (= 3.0.20) - activerecord (= 3.0.20) - activeresource (= 3.0.20) - activesupport (= 3.0.20) + rails (3.1.10) + actionmailer (= 3.1.10) + actionpack (= 3.1.10) + activerecord (= 3.1.10) + activeresource (= 3.1.10) + activesupport (= 3.1.10) bundler (~> 1.0) - railties (= 3.0.20) + railties (= 3.1.10) rails3-generators (1.0.0) railties (>= 3.0.0) - railties (3.0.20) - actionpack (= 3.0.20) - activesupport (= 3.0.20) + railties (3.1.10) + actionpack (= 3.1.10) + activesupport (= 3.1.10) + rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) - thor (~> 0.14.4) + thor (~> 0.14.6) rake (10.0.3) - rdoc (3.12) + rdoc (3.12.1) json (~> 1.4) recaptcha (0.3.4) redis (3.0.2) @@ -184,9 +191,14 @@ GEM multi_json (~> 1.0) simplecov-html (~> 0.7.1) simplecov-html (0.7.1) - sinatra (1.2.8) - rack (~> 1.1) - tilt (>= 1.2.2, < 2.0) + sinatra (1.3.3) + rack (~> 1.3, >= 1.3.6) + rack-protection (~> 1.2) + tilt (~> 1.3, >= 1.3.3) + sprockets (2.0.4) + hike (~> 1.2) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) sunspot (1.3.3) pr_geohash (~> 1.0) rsolr (~> 1.0.7) @@ -227,7 +239,7 @@ DEPENDENCIES newrelic_rpm paperclip (~> 3.0) pg - rails (~> 3.0.20) + rails (~> 3.1.0) rails3-generators recaptcha recommendify! diff --git a/app/controllers/genotypes_controller.rb b/app/controllers/genotypes_controller.rb index fb4657c..5bc8681 100644 --- a/app/controllers/genotypes_controller.rb +++ b/app/controllers/genotypes_controller.rb @@ -34,7 +34,7 @@ class GenotypesController < ApplicationController @award = Achievement.find_by_award("Published genotyping") user_achievement_attrs = { achievement_id: @award.id, user_id: current_user.id } - if UserAchievement.where(user_achievement_attrs).count == 0 + if UserAchievement.where(user_achievement_attrs).count.zero? UserAchievement.create(user_achievement_attrs) flash[:achievement] = "Congratulations! You've unlocked an achievement:" + " #{@award.award}" diff --git a/app/controllers/phenotypes_controller.rb b/app/controllers/phenotypes_controller.rb index cb918fc..668af9b 100644 --- a/app/controllers/phenotypes_controller.rb +++ b/app/controllers/phenotypes_controller.rb @@ -74,9 +74,9 @@ class PhenotypesController < ApplicationController check_and_award_additional_phenotypes(20, "Entered 20 additional phenotypes") check_and_award_additional_phenotypes(50, "Entered 50 additional phenotypes") check_and_award_additional_phenotypes(100, "Entered 100 additional phenotypes") - + Resque.enqueue(Recommendvariations) - Resque.enqueue(Recommendphenotypes) + Resque.enqueue(Recommendphenotypes) redirect_to current_user else @@ -90,68 +90,28 @@ class PhenotypesController < ApplicationController end end - class PhenotypeRecommender < Recommendify::Base - - max_neighbors 50 - - input_matrix :users_to_phenotypes, - :similarity_func => :jaccard, - :weight => 5.0 - - end - - class VariationRecommender < Recommendify::Base - - max_neighbors 50 - - input_matrix :users_to_variations, - :similarity_func => :jaccard, - :weight => 5.0 - - end - def show - #@phenotypes = Phenotype.where(:user_id => current_user.id).all - #@title = "Phenotypes" @phenotype = Phenotype.find(params[:id]) || not_found @comments = PhenotypeComment.where(:phenotype_id => params[:id]).all(:order => "created_at ASC") @phenotype_comment = PhenotypeComment.new @user_phenotype = UserPhenotype.new - - - @recommender = PhenotypeRecommender.new - - @similar_ids = @recommender.for(params[:id]) - @similar_phenotypes = [] - @it_counter = 0 - - @similar_ids.each do |s| - if @it_counter < 6 - @similar_phenotypes << Phenotype.find(s.item_id) - @it_counter += 1 - else - break - end - end - - respond_to do |format| - format.html - format.xml - end + recommender = PhenotypeRecommender.new + similar_ids = recommender.for(params[:id]) + @similar_phenotypes = Phenotype.where(['id in (?)', similar_ids]).limit(6) end def recommend_phenotype # init the recommendation-engines @phenotype_recommender = PhenotypeRecommender.new @variation_recommender = VariationRecommender.new - + # get up to three similar phenotypes regardless of variation - + @similar_ids = @phenotype_recommender.for(params[:id]) @similar_phenotypes = [] @item_counter = 0 - + @similar_ids.each do |s| if @item_counter < 3 @phenotype = Phenotype.find(s.item_id) @@ -163,7 +123,7 @@ class PhenotypesController < ApplicationController break end end - + # get up to three similar combinations of phenotype and variation @user_phenotype = UserPhenotype.find_by_phenotype_id_and_user_id(params[:id],current_user.id) if @user_phenotype != nil @@ -172,11 +132,11 @@ class PhenotypesController < ApplicationController else @variation_recommend_request = "" end - + @similar_combinations = @phenotype_recommender.for(@variation_recommend_request) @similar_variations = [] @combination_counter = 0 - + @similar_combinations.each do |s| if @combination_counter < 3 @phenotype = Phenotype.find_by_id(s.item_id.split("=>")[0]) @@ -188,9 +148,9 @@ class PhenotypesController < ApplicationController break end end - + @phenotype = Phenotype.find_by_id(params[:id]) - + if @similar_phenotypes == [] and @similar_variations == [] redirect_to :action => "index" else @@ -242,37 +202,37 @@ class PhenotypesController < ApplicationController rescue @result["error"] = "Sorry, this phenotype doesn't exist" end - + respond_to do |format| format.json { render :json => @result } end - + end def json if params[:user_id].index(",") @user_ids = params[:user_id].split(",") - @results = [] - @user_ids.each do |id| - @new_param = {} - @new_param[:user_id] = id + @results = [] + @user_ids.each do |id| + @new_param = {} + @new_param[:user_id] = id @results << json_element(@new_param) end - + elsif params[:user_id].index("-") @results = [] @id_array = params[:user_id].split("-") @user_ids = (@id_array[0].to_i..@id_array[1].to_i).to_a @user_ids.each do |id| @new_param = {} - @new_param[:user_id] = id - @results << json_element(@new_param) + @new_param[:user_id] = id + @results << json_element(@new_param) end - - else + + else @results = json_element(params) end - + respond_to do |format| format.json { render :json => @results } end @@ -283,19 +243,19 @@ class PhenotypesController < ApplicationController @user = User.find_by_id(params[:user_id]) @result = {} @user_phenotypes = UserPhenotype.find_all_by_user_id(@user.id) - + @result["user"] = {} @result["user"]["name"] = @user.name @result["user"]["id"] = @user.id - + @phenotype_hash = {} - + @user_phenotypes.each do |up| @phenotype_hash[up.phenotype.characteristic] = {} @phenotype_hash[up.phenotype.characteristic]["phenotype_id"] = up.phenotype.id @phenotype_hash[up.phenotype.characteristic]["variation"] = up.variation end - + @result["phenotypes"] = @phenotype_hash rescue @result = {} diff --git a/app/models/genotype.rb b/app/models/genotype.rb index ef35226..cce88f6 100644 --- a/app/models/genotype.rb +++ b/app/models/genotype.rb @@ -8,7 +8,10 @@ class Genotype < ActiveRecord::Base has_attached_file :genotype, url: '/data/:fs_filename', path: "#{Rails.root}/public/data/:fs_filename" before_post_process :is_image? - validates_attachment_presence :genotype + validates_attachment :genotype, + presence: true, + content_type: { content_type: 'text/plain' }, + size: { in: 0..100.megabytes } attr_accessible :genotype, :filetype, :user_id after_create :parse_genotype diff --git a/app/views/genotypes/rss.rxml b/app/views/genotypes/rss.xml.builder similarity index 100% rename from app/views/genotypes/rss.rxml rename to app/views/genotypes/rss.xml.builder diff --git a/app/views/news/paper_rss.rxml b/app/views/news/paper_rss.xml.builder similarity index 100% rename from app/views/news/paper_rss.rxml rename to app/views/news/paper_rss.xml.builder diff --git a/app/views/phenotypes/rss.rxml b/app/views/phenotypes/rss.xml.builder similarity index 100% rename from app/views/phenotypes/rss.rxml rename to app/views/phenotypes/rss.xml.builder diff --git a/app/views/picture_phenotypes/rss.rxml b/app/views/picture_phenotypes/rss.xml.builder similarity index 100% rename from app/views/picture_phenotypes/rss.rxml rename to app/views/picture_phenotypes/rss.xml.builder diff --git a/config/environments/development.rb b/config/environments/development.rb index e1d0b13..6415280 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -12,7 +12,6 @@ Snpr::Application.configure do # Show full error reports and disable caching config.consider_all_requests_local = true - config.action_view.debug_rjs = true config.action_controller.perform_caching = false # Don't care if the mailer can't send diff --git a/lib/phenotype_recommender.rb b/lib/phenotype_recommender.rb new file mode 100644 index 0000000..5004918 --- /dev/null +++ b/lib/phenotype_recommender.rb @@ -0,0 +1,6 @@ +class PhenotypeRecommender < Recommendify::Base + max_neighbors 50 + input_matrix :users_to_phenotypes, + :similarity_func => :jaccard, + :weight => 5.0 +end diff --git a/lib/variation_recommender.rb b/lib/variation_recommender.rb new file mode 100644 index 0000000..0860adb --- /dev/null +++ b/lib/variation_recommender.rb @@ -0,0 +1,6 @@ +class VariationRecommender < Recommendify::Base + max_neighbors 50 + input_matrix :users_to_variations, + :similarity_func => :jaccard, + :weight => 5.0 +end diff --git a/test/fixtures/.gitkeep b/test/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/functional/genotypes_controller_test.rb b/test/functional/genotypes_controller_test.rb index 1f43691..0be8181 100644 --- a/test/functional/genotypes_controller_test.rb +++ b/test/functional/genotypes_controller_test.rb @@ -25,7 +25,7 @@ class GenotypesControllerTest < ActionController::TestCase end should "get the rss feed" do - get :feed + get :feed, format: 'xml' assert_response :success assert_equal [@genotype], assigns(:genotypes) end @@ -41,7 +41,8 @@ class GenotypesControllerTest < ActionController::TestCase activate_authlogic @user = FactoryGirl.create(:user) UserSession.create(@user) - @publishing_award = FactoryGirl.create(:achievement, award: "Published genotyping") + @publishing_award = FactoryGirl. + create(:achievement, award: "Published genotyping") end should "see the upload form" do @@ -50,16 +51,20 @@ class GenotypesControllerTest < ActionController::TestCase end should "be able to upload genotypes" do + FileUtils.cp("#{Rails.root}/testdata/testdatensatz1_23andme.txt", + "#{Rails.root}/test/fixtures") + genotype_file = fixture_file_upload('testdatensatz1_23andme.txt') + genotype_file.content_type = 'text/plain' Resque.expects(:enqueue).with(Preparsing, is_a(Fixnum)) - genotype_file_upload = ActionDispatch::Http::UploadedFile.new( - filename: '23andme.txt', content_type: 'text/plain', - tempfile: File.new("#{Rails.root}/test/data/23andMe_test.csv")) assert_difference 'UserAchievement.count' do - put :create, commit: "Upload", genotype: - { genotype: genotype_file_upload, filetype: "23andme"} + assert_difference 'Genotype.count' do + put :create, commit: "Upload", genotype: + { genotype: genotype_file, filetype: "23andme"} + end end assert_redirected_to user_path(@user) assert_equal @publishing_award.id, UserAchievement.last.achievement_id + FileUtils.rm("#{Rails.root}/test/fixtures/testdatensatz1_23andme.txt") end end end diff --git a/test/functional/phentoypes_controller_test.rb b/test/functional/phentoypes_controller_test.rb index 51ab344..4637f7b 100644 --- a/test/functional/phentoypes_controller_test.rb +++ b/test/functional/phentoypes_controller_test.rb @@ -39,13 +39,17 @@ class PhenotypesControllerTest < ActionController::TestCase end should "see them" do + similar_phenotype = FactoryGirl.create(:phenotype) + PhenotypeRecommender.any_instance.expects(:for). + with(@phenotype.id.to_s).returns([similar_phenotype.id]) get :show, id: @phenotype.id assert_response :success assert_equal @phenotype, assigns(:phenotype) + assert_equal [similar_phenotype], assigns(:similar_phenotypes) end should "get the feed" do - get :feed, id: @phenotype.id + get :feed, id: @phenotype.id, format: 'xml' assert_response :success end diff --git a/test/test_helper.rb b/test/test_helper.rb index 65d8fa0..d568970 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,6 +11,8 @@ SunspotTest.solr_startup_timeout = 30 require 'sunspot_test/test_unit' require 'factory_girl' FactoryGirl.find_definitions +require 'paperclip/matchers' class ActiveSupport::TestCase + extend Paperclip::Shoulda::Matchers end diff --git a/test/unit/genotype_test.rb b/test/unit/genotype_test.rb new file mode 100644 index 0000000..03d7cce --- /dev/null +++ b/test/unit/genotype_test.rb @@ -0,0 +1,15 @@ +require_relative '../test_helper' + +class GenotypeTest < ActiveSupport::TestCase + context "Genotype" do + setup do + Genotype.any_instance.stubs(:fs_filename).returns("1.23andme.1") + end + + should have_attached_file(:genotype) + should validate_attachment_presence(:genotype) + should validate_attachment_content_type(:genotype). + allowing('text/plain').rejecting('image/png', 'image/gif', 'text/xml') + should validate_attachment_size(:genotype).less_than(100.megabytes) + end +end