Guest User

Untitled

a guest
Jan 30th, 2018
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. class Task < ActiveRecord::Base
  2. belongs_to :user
  3. has_many :comments, dependent: :destroy
  4. has_many :tags, dependent: :destroy
  5. validates :description, presence: true
  6.  
  7. def tagged_user_ids
  8. tags.map{|tag| tag.user_id}
  9. end
  10.  
  11. def tag_exists_for(user)
  12. tagged_user_ids.include?user.id.to_s
  13. end
  14.  
  15. def tag(user)
  16. tags.create(user_id: user.id)
  17. end
  18.  
  19. def untag(user)
  20. tags.find_by(user_id: user.id).destroy
  21. end
  22. end
  23.  
  24. class User < ActiveRecord::Base
  25. # Include default devise modules. Others available are:
  26. # :confirmable, :lockable, :timeoutable and :omniauthable
  27. devise :database_authenticatable, :registerable,
  28. :recoverable, :rememberable, :trackable, :validatable
  29. has_many :tasks
  30. has_many :tags
  31. has_many :comments,through: :tasks
  32.  
  33. def tagged_tasks
  34. tags.map{|tag| tag.task}
  35. end
  36.  
  37. def all_tasks
  38. self.tasks + self.tagged_tasks
  39. end
  40.  
  41. end
  42.  
  43. class Tag < ActiveRecord::Base
  44.  
  45. belongs_to :user
  46. belongs_to :task
  47.  
  48. end
  49.  
  50. describe "untag" do
  51. it "should untag user from task" do
  52. create_task_and_tag_user
  53. p @task2.tags
  54. @task2.untag(@user)
  55. p @task2.tags
  56. expect(@task2.tag_exists_for(@user)).to be false
  57. end
  58. end
  59.  
  60. def create_task_and_tag_user
  61. @user = User.create(email:"asd@jaka.com",password: 123456,name: "user1")
  62. @task1 = @user.tasks.create(description: "some description")
  63. @user2 = User.create(email:"user2@jaka.com",password: 123456,name: "user2")
  64. @task2 = @user2.tasks.create(description: "some other task")
  65. @task2.tag(@user)
  66. end
  67.  
  68. Task untag should untag user from task
  69. Failure/Error: expect(@task2.tag_exists_for(@user)).to be false
  70.  
  71. expected false
  72. got true
  73.  
  74. Tag.find_by(id: 39)
  75. Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id` = 71 LIMIT 1
  76. => nil
  77. 2.4.1 :042 > t1.tags
  78. => #<ActiveRecord::Associations::CollectionProxy [#<Tag id: 39, user_id: "3", task_id: "37", created_at: "2018-01-18 11:13:23", updated_at: "2018-01-18 11:13:23">, #<Tag id: 43, user_id: "9", task_id: "37", created_at: "2018-01-22 04:56:13", updated_at: "2018-01-22 04:56:13">, #<Tag id: 44, user_id: "5", task_id: "37", created_at: "2018-01-22 05:31:16", updated_at: "2018-01-22 05:31:16">]>
  79. 2.4.1 :043 >
Add Comment
Please, Sign In to add comment