-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature of tagging users in photos (#415)
* Add feature of tagging users in photos * Fix lint * Fix tests * Increase coverage * Fix lint
- Loading branch information
Showing
23 changed files
with
366 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class V1::PhotoTagsController < V1::ApplicationController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class PhotoTag < ApplicationRecord | ||
belongs_to :photo, touch: true | ||
counter_culture :photo, column_name: :tags_count | ||
belongs_to :author, class_name: 'User' | ||
belongs_to :tagged_user, class_name: 'User' | ||
|
||
validates :x, inclusion: { in: 0.0..100.0 } | ||
validates :y, inclusion: { in: 0.0..100.0 } | ||
|
||
validate :user_not_already_tagged | ||
|
||
private | ||
|
||
def user_not_already_tagged | ||
if PhotoTag.exists?(photo_id: photo_id, tagged_user_id: tagged_user_id) | ||
errors.add(:tagged_user, 'has already been tagged in this photo') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class PhotoTagPolicy < ApplicationPolicy | ||
def update? | ||
user_is_owner? || user_is_tagged? || super | ||
end | ||
|
||
def destroy? | ||
user_is_owner? || user_is_tagged? || super | ||
end | ||
|
||
def create_with_photo?(_photo) | ||
true | ||
end | ||
|
||
def replace_photo?(_photo) | ||
true | ||
end | ||
|
||
def create_with_tagged_user?(_user) | ||
true | ||
end | ||
|
||
def replace_tagged_user?(_user) | ||
true | ||
end | ||
|
||
private | ||
|
||
def user_is_owner? | ||
record.author == user | ||
end | ||
|
||
def user_is_tagged? | ||
record.tagged_user == user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class V1::PhotoTagResource < V1::ApplicationResource | ||
attributes :x | ||
attributes :y | ||
|
||
has_one :photo, always_include_linkage_data: true | ||
has_one :author, always_include_linkage_data: true | ||
has_one :tagged_user, always_include_linkage_data: true | ||
|
||
before_create do | ||
@model.author_id = current_user.id | ||
end | ||
|
||
def self.creatable_fields(_context) | ||
%i[x y photo tagged_user] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class CreatePhotoTags < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :photo_tags do |t| | ||
t.decimal :x, precision: 5, scale: 2 | ||
t.float :y, precision: 5, scale: 2 | ||
t.integer :author_id, null: false | ||
t.integer :tagged_user_id, null: false | ||
t.integer :photo_id, null: false | ||
t.datetime :deleted_at | ||
|
||
t.timestamps | ||
end | ||
add_column :photos, :tags_count, :integer, null: false, default: 0 | ||
end | ||
|
||
Permission.create(name: 'photo_tag.create') | ||
Permission.create(name: 'photo_tag.read') | ||
Permission.create(name: 'photo_tag.update') | ||
Permission.create(name: 'photo_tag.destroy') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FactoryBot.define do | ||
factory :photo_tag do | ||
x { rand(0.0..100.0) } | ||
y { rand(0.0..100.0) } | ||
association :author, factory: :user | ||
association :tagged_user, factory: :user | ||
|
||
photo | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe PhotoTag, type: :model do | ||
subject(:photo_tag) { build(:photo_tag) } | ||
|
||
describe '#valid?' do | ||
it { expect(photo_tag).to be_valid } | ||
|
||
context 'when without an x position' do | ||
subject(:photo_tag) { build(:photo_tag, x: nil) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
|
||
context 'when without an y position' do | ||
subject(:photo_tag) { build(:photo_tag, y: nil) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
|
||
context 'when with a too large x position' do | ||
subject(:photo_tag) { build(:photo_tag, x: 101) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
|
||
context 'when with a too small x position' do | ||
subject(:photo_tag) { build(:photo_tag, x: -1) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
|
||
context 'when with a too large y position' do | ||
subject(:photo_tag) { build(:photo_tag, y: 101) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
|
||
context 'when with a too small y position' do | ||
subject(:photo_tag) { build(:photo_tag, y: -1) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
|
||
context 'when with a valid x position' do | ||
subject(:photo_tag) { build(:photo_tag, x: 50) } | ||
|
||
it { expect(photo_tag).to be_valid } | ||
end | ||
|
||
context 'when with a valid y position' do | ||
subject(:photo_tag) { build(:photo_tag, y: 50) } | ||
|
||
it { expect(photo_tag).to be_valid } | ||
end | ||
|
||
context 'when with a user that is already tagged' do | ||
let(:photo) { create(:photo) } | ||
let(:tagged_user) { create(:user) } | ||
|
||
before { create(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id) } | ||
|
||
subject(:photo_tag) { build(:photo_tag, photo_id: photo.id, tagged_user_id: tagged_user.id) } | ||
|
||
it { expect(photo_tag).not_to be_valid } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe PhotoTagPolicy, type: :policy do | ||
subject(:policy) { described_class } | ||
|
||
let(:user) { build(:user) } | ||
|
||
permissions :update? do | ||
describe 'when photo tag is not owned or tagged' do | ||
it { expect(policy).not_to permit(user, build(:photo_tag)) } | ||
end | ||
|
||
describe 'when photo tag is owned' do | ||
it { expect(policy).to permit(user, build(:photo_tag, author: user)) } | ||
end | ||
|
||
describe 'when photo tag is tagged' do | ||
it { expect(policy).to permit(user, build(:photo_tag, tagged_user: user)) } | ||
end | ||
end | ||
|
||
describe '#create_with_photo?' do | ||
it { expect(policy.new(nil, nil).create_with_photo?(nil)).to be true } | ||
end | ||
|
||
describe '#replace_photo?' do | ||
it { expect(policy.new(nil, nil).replace_photo?(nil)).to be true } | ||
end | ||
|
||
describe '#create_with_tagged_user?' do | ||
it { expect(policy.new(nil, nil).create_with_tagged_user?(nil)).to be true } | ||
end | ||
|
||
describe '#replace_tagged_user?' do | ||
it { expect(policy.new(nil, nil).replace_tagged_user?(nil)).to be true } | ||
end | ||
end |
Oops, something went wrong.