Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Task < ActiveRecord::Base
- belongs_to :user
- has_many :comments, dependent: :destroy
- has_many :tags, dependent: :destroy
- validates :description, presence: true
- def tagged_user_ids
- tags.map{|tag| tag.user_id}
- end
- def tag_exists_for(user)
- tagged_user_ids.include?user.id.to_s
- end
- def tag(user)
- tags.create(user_id: user.id)
- end
- def untag(user)
- tags.find_by(user_id: user.id).destroy
- end
- end
- class User < ActiveRecord::Base
- # Include default devise modules. Others available are:
- # :confirmable, :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
- has_many :tasks
- has_many :tags
- has_many :comments,through: :tasks
- def tagged_tasks
- tags.map{|tag| tag.task}
- end
- def all_tasks
- self.tasks + self.tagged_tasks
- end
- end
- class Tag < ActiveRecord::Base
- belongs_to :user
- belongs_to :task
- end
- describe "untag" do
- it "should untag user from task" do
- create_task_and_tag_user
- p @task2.tags
- @task2.untag(@user)
- p @task2.tags
- expect(@task2.tag_exists_for(@user)).to be false
- end
- end
- def create_task_and_tag_user
- @user = User.create(email:"asd@jaka.com",password: 123456,name: "user1")
- @task1 = @user.tasks.create(description: "some description")
- @user2 = User.create(email:"user2@jaka.com",password: 123456,name: "user2")
- @task2 = @user2.tasks.create(description: "some other task")
- @task2.tag(@user)
- end
- Task untag should untag user from task
- Failure/Error: expect(@task2.tag_exists_for(@user)).to be false
- expected false
- got true
- Tag.find_by(id: 39)
- Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id` = 71 LIMIT 1
- => nil
- 2.4.1 :042 > t1.tags
- => #<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">]>
- 2.4.1 :043 >
Add Comment
Please, Sign In to add comment