1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Add and remove tags on an image

This commit is contained in:
Daniel Barber 2018-08-31 16:10:46 -04:00
parent 431ad8c1d6
commit 9037ff236f
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
7 changed files with 120 additions and 14 deletions

View File

@ -0,0 +1,21 @@
class TagsController < ApplicationController
before_action :authenticate_user!
def create
image = @current_user.images.find(params[:image_id])
tag = params[:tag]
image.tags << tag
image.save
redirect_to([:user, image])
end
def destroy
image = @current_user.images.find(params[:image_id])
tag = params[:id]
image.tags.delete(tag)
image.save
redirect_to([:user, image])
end
end

View File

@ -12,8 +12,21 @@
<div class="image-info">
<ul class="image-tags">
<% @image.tags.each do |tag| %>
<li class="image-tag"><%= tag %></li>
<li class="image-tag" id="tag-<%= tag %>">
<%= tag %>
<%= link_to "&times;".html_safe,
user_image_tag_path(@image, tag),
method: :delete,
class: "delete-tag" %>
</li>
<% end %>
</ul>
<div class="new-tag-form">
<%= form_tag user_image_tags_path(@image), method: :post do %>
<%= text_field_tag :tag %>
<%= submit_tag "Add Tag" %>
<% end %>
</div>
</div>
</div>

View File

@ -5,6 +5,8 @@ MyImages::Application.routes.draw do
get "/auth/:provider/callback", to: "sessions#create", as: :create_session
resource :user, only: [] do
resources :images, only: %i[index show create destroy]
resources :images, only: %i[index show create destroy] do
resources :tags, only: %i[create destroy]
end
end
end

View File

@ -1,6 +1,6 @@
require "rails_helper"
describe "user manages images" do
feature "user manages images" do
include ActiveJob::TestHelper
it "uploads the image" do
@ -14,10 +14,13 @@ describe "user manages images" do
end
it "deletes the image" do
user = User.create(uid: 1)
Image.create(user: user, image: "#{Rails.root}/spec/fixtures/spectrum.jpg")
user = User.create(uid: "1")
Image.create(
user: user,
image: File.new("#{Rails.root}/spec/fixtures/spectrum.jpg"),
)
sign_in
sign_in(user)
click_on("Delete")
expect(page).not_to have_css(".image")

View File

@ -0,0 +1,58 @@
require "rails_helper"
feature "user manages image tags by visitng images show page" do
scenario "and can see an X to link for deletion on a tag" do
tags = ["one", "two"]
user = User.create(uid: "123")
Image.create(
user: user,
tags: tags,
image: File.new("#{Rails.root}/spec/fixtures/spectrum.jpg"),
)
sign_in(user)
page.find(".image").click
within("#tag-#{tags.first}") do
expect(page).to have_css(".delete-tag")
end
end
scenario "and can click the link to delete a tag" do
tags = ["one", "two"]
user = User.create(uid: "123")
Image.create(
user: user,
tags: tags,
image: File.new("#{Rails.root}/spec/fixtures/spectrum.jpg"),
)
sign_in(user)
page.find(".image").click
within("#tag-#{tags.first}") do
page.find(".delete-tag").click
end
expect(page).to_not have_content(tags.first)
end
scenario "user can add a tag to the list of tags" do
tags = ["one", "two"]
user = User.create(uid: "123")
new_tag = "new tag"
Image.create(
user: user,
tags: tags,
image: File.new("#{Rails.root}/spec/fixtures/spectrum.jpg"),
)
sign_in(user)
page.find(".image").click
fill_in("tag", with: new_tag)
click_on("Add Tag")
expect(page).to have_content(new_tag)
end
end

View File

@ -40,10 +40,3 @@ Shoulda::Matchers.configure do |config|
end
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(
:github,
provider: "github",
uid: "1",
credentials: { token: "12345" },
)

View File

@ -1,5 +1,6 @@
module AuthHelpers
def sign_in
def sign_in(user = fake_user)
mock_oauth(user.uid)
allow_any_instance_of(SessionsController).
to receive(:in_organization?).
and_return(true)
@ -10,4 +11,19 @@ module AuthHelpers
click_link("Sign in with GitHub")
end
end
private
def fake_user
OpenStruct.new(uid: SecureRandom.hex(10))
end
def mock_oauth(uid)
OmniAuth.config.add_mock(
:github,
provider: "github",
uid: uid,
credentials: { token: "12345" },
)
end
end