diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb new file mode 100644 index 0000000..1ec27d6 --- /dev/null +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/app/views/images/show.html.erb b/app/views/images/show.html.erb index 7d3aa0e..ee8c6b9 100644 --- a/app/views/images/show.html.erb +++ b/app/views/images/show.html.erb @@ -12,8 +12,21 @@
+ +
+ <%= form_tag user_image_tags_path(@image), method: :post do %> + <%= text_field_tag :tag %> + <%= submit_tag "Add Tag" %> + <% end %> +
diff --git a/config/routes.rb b/config/routes.rb index 3b9e401..24d9b4f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/features/user_manages_image_spec.rb b/spec/features/user_manages_image_spec.rb index 1839913..66c7a46 100644 --- a/spec/features/user_manages_image_spec.rb +++ b/spec/features/user_manages_image_spec.rb @@ -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") diff --git a/spec/features/user_manages_image_tags_spec.rb b/spec/features/user_manages_image_tags_spec.rb new file mode 100644 index 0000000..44263b5 --- /dev/null +++ b/spec/features/user_manages_image_tags_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 90ea2ac..4c08939 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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" }, -) diff --git a/spec/support/auth_helpers.rb b/spec/support/auth_helpers.rb index c795aef..42cbd38 100644 --- a/spec/support/auth_helpers.rb +++ b/spec/support/auth_helpers.rb @@ -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