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:
parent
431ad8c1d6
commit
9037ff236f
21
app/controllers/tags_controller.rb
Normal file
21
app/controllers/tags_controller.rb
Normal 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
|
||||||
@ -12,8 +12,21 @@
|
|||||||
<div class="image-info">
|
<div class="image-info">
|
||||||
<ul class="image-tags">
|
<ul class="image-tags">
|
||||||
<% @image.tags.each do |tag| %>
|
<% @image.tags.each do |tag| %>
|
||||||
<li class="image-tag"><%= tag %></li>
|
<li class="image-tag" id="tag-<%= tag %>">
|
||||||
|
<%= tag %>
|
||||||
|
<%= link_to "×".html_safe,
|
||||||
|
user_image_tag_path(@image, tag),
|
||||||
|
method: :delete,
|
||||||
|
class: "delete-tag" %>
|
||||||
|
</li>
|
||||||
<% end %>
|
<% end %>
|
||||||
</ul>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -5,6 +5,8 @@ MyImages::Application.routes.draw do
|
|||||||
get "/auth/:provider/callback", to: "sessions#create", as: :create_session
|
get "/auth/:provider/callback", to: "sessions#create", as: :create_session
|
||||||
|
|
||||||
resource :user, only: [] do
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe "user manages images" do
|
feature "user manages images" do
|
||||||
include ActiveJob::TestHelper
|
include ActiveJob::TestHelper
|
||||||
|
|
||||||
it "uploads the image" do
|
it "uploads the image" do
|
||||||
@ -14,10 +14,13 @@ describe "user manages images" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "deletes the image" do
|
it "deletes the image" do
|
||||||
user = User.create(uid: 1)
|
user = User.create(uid: "1")
|
||||||
Image.create(user: user, image: "#{Rails.root}/spec/fixtures/spectrum.jpg")
|
Image.create(
|
||||||
|
user: user,
|
||||||
|
image: File.new("#{Rails.root}/spec/fixtures/spectrum.jpg"),
|
||||||
|
)
|
||||||
|
|
||||||
sign_in
|
sign_in(user)
|
||||||
click_on("Delete")
|
click_on("Delete")
|
||||||
|
|
||||||
expect(page).not_to have_css(".image")
|
expect(page).not_to have_css(".image")
|
||||||
|
|||||||
58
spec/features/user_manages_image_tags_spec.rb
Normal file
58
spec/features/user_manages_image_tags_spec.rb
Normal 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
|
||||||
@ -40,10 +40,3 @@ Shoulda::Matchers.configure do |config|
|
|||||||
end
|
end
|
||||||
|
|
||||||
OmniAuth.config.test_mode = true
|
OmniAuth.config.test_mode = true
|
||||||
|
|
||||||
OmniAuth.config.add_mock(
|
|
||||||
:github,
|
|
||||||
provider: "github",
|
|
||||||
uid: "1",
|
|
||||||
credentials: { token: "12345" },
|
|
||||||
)
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
module AuthHelpers
|
module AuthHelpers
|
||||||
def sign_in
|
def sign_in(user = fake_user)
|
||||||
|
mock_oauth(user.uid)
|
||||||
allow_any_instance_of(SessionsController).
|
allow_any_instance_of(SessionsController).
|
||||||
to receive(:in_organization?).
|
to receive(:in_organization?).
|
||||||
and_return(true)
|
and_return(true)
|
||||||
@ -10,4 +11,19 @@ module AuthHelpers
|
|||||||
click_link("Sign in with GitHub")
|
click_link("Sign in with GitHub")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user