Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #19 from tsujigiri/known_phenotypes

Nice work, lots of unnecessary stuff is gone
  • Loading branch information...
commit ce41ad1ab3bc533e37563ac7fcf20ac267ebcb22 2 parents 9ead1af + b053992
Philipp Bayer philippbayer authored
4 app/controllers/phenotypes_controller.rb
@@ -39,10 +39,6 @@ def create
39 39 redirect_to :action => "new"
40 40 else
41 41
42   - if @phenotype.known_phenotypes.include?(params[:user_phenotype][:variation]) == false
43   - @phenotype.known_phenotypes << params[:user_phenotype][:variation]
44   - end
45   -
46 42 @phenotype.save
47 43 @phenotype = Phenotype.find_by_characteristic(params[:phenotype][:characteristic])
48 44 Resque.enqueue(Mailnewphenotype, @phenotype.id,current_user.id)
3  app/controllers/user_phenotypes_controller.rb
@@ -35,9 +35,6 @@ def create
35 35
36 36 @phenotype = Phenotype.find_by_id(params[:user_phenotype][:phenotype_id])
37 37
38   - if @phenotype.known_phenotypes.include?(params[:user_phenotype][:variation]) == false
39   - @phenotype.known_phenotypes << params[:user_phenotype][:variation]
40   - end
41 38 if @user_phenotype.save
42 39
43 40 #check for new achievements
88 app/controllers/users_controller.rb
@@ -46,19 +46,19 @@ def index
46 46 @title = "Listing all users"
47 47
48 48 @result = []
49   - begin
50   - @users = User.find(:all)
51   - @users.each do |u|
52   - @user = {}
53   - @user["name"] = u.name
54   - @user["id"] = u.id
55   - @user["genotypes"] = []
56   - Genotype.find_all_by_user_id(u.id).each do |g|
57   - @genotype = {}
58   - @genotype["id"] = g.id
59   - @genotype["filetype"] = g.filetype
60   - @genotype["download_url"] = 'http://opensnp.org/data/' + g.fs_filename
61   - @user["genotypes"] << @genotype
  49 + begin
  50 + @users = User.find(:all)
  51 + @users.each do |u|
  52 + @user = {}
  53 + @user["name"] = u.name
  54 + @user["id"] = u.id
  55 + @user["genotypes"] = []
  56 + Genotype.find_all_by_user_id(u.id).each do |g|
  57 + @genotype = {}
  58 + @genotype["id"] = g.id
  59 + @genotype["filetype"] = g.filetype
  60 + @genotype["download_url"] = 'http://opensnp.org/data/' + g.fs_filename
  61 + @user["genotypes"] << @genotype
62 62 end
63 63 @result << @user
64 64 end
@@ -149,22 +149,12 @@ def changepassword
149 149 def update
150 150 @user = User.find(params[:id])
151 151
152   - # check whether the user has deleted phenotypes and change known_phenotypes
153   - @pot_delete_phenotype_ids = []
154   - if params[:user][:user_phenotypes_attributes] != nil
155   -
  152 + if params[:user][:user_phenotypes_attributes].present?
156 153 params[:user][:user_phenotypes_attributes].each do |p|
157   - @phenotype = Phenotype.find(UserPhenotype.find(p[1]["id"]).phenotype_id)
158   -
  154 + @phenotype = UserPhenotype.find(p[1]["id"]).phenotype
159 155 @old_variation = UserPhenotype.find_by_id(p[1]["id"]).variation
160   - if UserPhenotype.find_all_by_phenotype_id_and_variation(@phenotype.id,@old_variation).length == 1
161   - @phenotype.known_phenotypes.delete_if { |entry| entry == @old_variation }
162   - end
163   -
164   -
165   - @pot_delete_phenotype_ids << @phenotype.id
  156 + # TODO: known_phenotypes compare different now
166 157 if @phenotype.known_phenotypes.include?(p[1]["variation"]) == false
167   - @phenotype.known_phenotypes << p[1]["variation"]
168 158 @phenotype.number_of_users = UserPhenotype.find_all_by_phenotype_id(@phenotype.id).length
169 159 @phenotype.save
170 160 end
@@ -175,16 +165,9 @@ def update
175 165 @empty_websites = Homepage.find_all_by_user_id_and_url(current_user.id,"")
176 166 @empty_websites.each do |ew| ew.delete end
177 167
178   - if @pot_delete_phenotype_ids != []
179   - @pot_delete_phenotype_ids.each do |pid|
180   - if UserPhenotype.find_all_by_phenotype_id(pid).length == 0
181   - Phenotype.delete(pid)
182   - end
183   - end
184   - end
185 168 Resque.enqueue(Recommendvariations)
186   - Resque.enqueue(Recommendphenotypes)
187   -
  169 + Resque.enqueue(Recommendphenotypes)
  170 +
188 171 flash[:notice] = "Successfully updated"
189 172
190 173 if params[:user][:password] or params[:user][:avatar]
@@ -216,7 +199,7 @@ def check_to_create_phenotype(characteristic, variation, user_id)
216 199 @phenotype = Phenotype.find_by_characteristic(characteristic)
217 200 if @phenotype == nil
218 201 # createphenotype if it doesn't exist
219   - @phenotype = Phenotype.create(:characteristic => characteristic, :number_of_users => 1, :known_phenotypes => [variation])
  202 + @phenotype = Phenotype.create(:characteristic => characteristic, :number_of_users => 1)
220 203 end
221 204 @user_phenotype = UserPhenotype.find_by_phenotype_id(@phenotype.id)
222 205 if @user_phenotype == nil
@@ -232,36 +215,13 @@ def destroy
232 215 @user = User.find(params[:id])
233 216 # delete the genotype(s)
234 217 if @user.genotypes.length != 0
235   - @user.genotypes.each do |ug|
236   - Resque.enqueue(Deletegenotype, ug)
237   - File.delete(::Rails.root.to_s+"/public/data/"+ ug.fs_filename)
238   - ug.delete
239   - end
240   - end
241   -
242   - @user.user_achievements.each do |ua|
243   - UserAchievement.delete(ua)
244   - end
245   -
246   - @messages = Message.find_all_by_user_id(@user_id)
247   -
248   - @messages.each do |mt|
249   - Message.delete(mt)
250   - end
251   -
252   - @user.user_phenotypes.each do |up|
253   - @phenotype = Phenotype.find_by_id(up.phenotype_id)
254   -
255   - if UserPhenotype.find_all_by_phenotype_id_and_variation(@phenotype.id,up.variation).length == 1
256   - @phenotype.known_phenotypes.delete_if { |entry| entry == up.variation }
257   - @phenotype.save
258   - end
259   -
260   - if @phenotype.user_phenotypes.length == 1
261   - Phenotype.delete(@phenotype)
  218 + @user.genotypes.each do |ug|
  219 + Resque.enqueue(Deletegenotype, ug)
  220 + File.delete(::Rails.root.to_s+"/public/data/"+ ug.fs_filename)
  221 + ug.delete
262 222 end
263   - UserPhenotype.delete(up)
264 223 end
  224 +
265 225 flash[:notice] = "Thank you for using openSNP. Goodbye!"
266 226 User.delete(@user)
267 227 redirect_to root_url
29 app/models/phenotype.rb
... ... @@ -1,18 +1,19 @@
1 1 class Phenotype < ActiveRecord::Base
2   - has_many :user_phenotypes
3   - has_many :phenotype_comments
4   - serialize :known_phenotypes
  2 + has_many :user_phenotypes, dependent: :destroy
  3 + has_many :phenotype_comments, dependent: :destroy
5 4
6   - validates_presence_of :characteristic
  5 + validates_presence_of :characteristic
7 6
8   - searchable do
9   - text :characteristic
10   - end
11   -
12   - after_create :default_array
13   -
14   - def default_array
15   - self.known_phenotypes ||= []
16   - end
17   -
  7 + searchable do
  8 + text :characteristic
  9 + end
  10 +
  11 + def known_phenotypes
  12 + @known_phenotypes ||=
  13 + user_phenotypes.map(&:variation).
  14 + map(&:downcase).
  15 + uniq.
  16 + map(&:camelize)
  17 + end
  18 +
18 19 end
20 app/models/user.rb
@@ -11,16 +11,16 @@ class User < ActiveRecord::Base
11 11 #after_create :make_standard_phenotypes
12 12
13 13 # dependent so stuff gets destroyed on delete
14   - has_many :user_phenotypes, :dependent => :destroy
15   - has_many :phenotypes, :through => :user_phenotypes
16   - has_many :genotypes, :dependent => :destroy
  14 + has_many :user_phenotypes, dependent: :destroy
  15 + has_many :phenotypes, through: :user_phenotypes
  16 + has_many :genotypes, dependent: :destroy
17 17 # user_snps needs some extra-logic to decrease the counters
18   - has_many :user_snps, :dependent => :destroy
19   - has_many :snps, :through => :user_snps
20   - has_many :homepages, :dependent => :destroy
21   - has_many :messages
22   - has_many :user_achievements, :dependent => :destroy
23   - has_many :achievements, :through => :user_achievements
  18 + has_many :user_snps, dependent: :destroy
  19 + has_many :snps, through: :user_snps
  20 + has_many :homepages, dependent: :destroy
  21 + has_many :messages, dependent: :destroy
  22 + has_many :user_achievements, dependent: :destroy
  23 + has_many :achievements, through: :user_achievements
24 24 has_many :snp_comments
25 25 has_many :phenotype_comments
26 26
@@ -53,7 +53,7 @@ def check_and_make_standard_phenotypes(charact)
53 53 @phen_id = Phenotype.find_by_characteristic(charact).id
54 54 UserPhenotype.create(:phenotype_id => @phen_id, :variation => '', :user_id => id)
55 55 else
56   - @phen_id = Phenotype.create(:characteristic => charact,:known_phenotypes => []).id
  56 + @phen_id = Phenotype.create(:characteristic => charact).id
57 57 UserPhenotype.create(:phenotype_id => @phen_id, :variation => '', :user_id => id)
58 58 end
59 59 end
2  app/models/user_phenotype.rb
... ... @@ -1,5 +1,5 @@
1 1 class UserPhenotype < ActiveRecord::Base
2   - belongs_to :phenotype
  2 + belongs_to :phenotype, dependent: :destroy
3 3 belongs_to :user
4 4 validates_presence_of :variation
5 5
1  test/factories.rb
@@ -28,7 +28,6 @@
28 28
29 29 Factory.define :phenotype do |p|
30 30 p.characteristic "Penis length"
31   - p.known_phenotypes [ "foo", "bar" ]
32 31 end
33 32
34 33 Factory.define :user_phenotype do |up|
17 test/unit/phenotype_test.rb
... ... @@ -0,0 +1,17 @@
  1 +require_relative '../test_helper'
  2 +
  3 +class PhenotypeTest < ActiveSupport::TestCase
  4 + context "Phenotype" do
  5 + setup do
  6 + Sunspot.stubs(:index)
  7 + @phenotype = Factory(:phenotype)
  8 + @phenotype.instance_variable_set(:@known_phenotypes, nil)
  9 + end
  10 +
  11 + should "know some phenotypes" do
  12 + @phenotype.user_phenotypes.create(variation: "Ping pong")
  13 + @phenotype.user_phenotypes.create(variation: "ping pong")
  14 + assert_equal ["Ping pong"], @phenotype.known_phenotypes
  15 + end
  16 + end
  17 +end

0 comments on commit ce41ad1

Please sign in to comment.
Something went wrong with that request. Please try again.