Skip to content

Commit

Permalink
Merge c2be725 into 4cd42f0
Browse files Browse the repository at this point in the history
  • Loading branch information
chargio committed Feb 5, 2018
2 parents 4cd42f0 + c2be725 commit 0ce6133
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@

.byebug_history
/.yardoc/

/doc/
8 changes: 5 additions & 3 deletions app/models/spin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
###
# Spin Class
#
# Spin(id: integer,
# Spin
# id: integer,
# published: boolean,
# name: string,
# full_name: text,
Expand Down Expand Up @@ -33,13 +34,14 @@
# user_id: integer,
# company: text,
# created_at: datetime,
# updated_at: datetime)
# updated_at: datetime
#
class Spin < ApplicationRecord
belongs_to :user
has_many :taggings, dependent: :destroy
has_many :taggings, dependent: :destroy, inverse_of: :spin
has_many :tags, through: :taggings

# A JSON Schema to check the format of the metadata file
SPIN_SCHEMA = Rails.application.config.spin_schema.freeze

# Show if the spin is visible or not
Expand Down
16 changes: 11 additions & 5 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
###
# Tag Class
# Includes all tags
# Includes all tags in the system (lowercase)
#
# Tag(id: integer,
#
# Tag
# id: integer,
# name: string,
# created_at: datetime,
# updated_at: datetime)
# updated_at: datetime
##
class Tag < ApplicationRecord
# Represents the tags in the system
has_many :taggings, dependent: :destroy
has_many :spins, through: :taggings

validates :name, presence: true, uniqueness: true
before_save :name_to_lower

def validate?
# Returns similar tags if found or nil
# @return [formated String] Similar tags
# @return [nil] no match
def find_similar
seed_data.each do |tag, candidates|
candidates.each do |candidate|
return "Maybe the tag #{name} is wrong. Did you mean #{tag}?. " if name.match(/#{candidate}/) && name!=tag
return "Maybe the tag #{name} is wrong. Did you mean #{tag}?" if name.match(/#{candidate}/) && name!=tag
end
end
nil
Expand Down
12 changes: 7 additions & 5 deletions app/models/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# Tagging class
#
# A class that associates spins to tags
# Tagging(id: integer,
# spin_id: integer,
# tag_id: integer,
# created_at: datetime,
# updated_at: datetime)
#
# Tagging
# id: integer,
# spin_id: integer,
# tag_id: integer,
# created_at: datetime,
# updated_at: datetime
# #
class Tagging < ApplicationRecord
belongs_to :tag
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/taggings.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Factory to create a tagging.
# It will create spins and tags as needed or be associated to them
FactoryBot.define do
factory :tagging do
spin
Expand Down
5 changes: 4 additions & 1 deletion spec/factories/tags.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Factory for tags
# Creates an object with a random name

FactoryBot.define do
factory :tag do
sequence :name do |n|
"#{Faker::StarWars.specie}_#{n}"
"#{Faker::StarWars.specie}_#{n}"
end
end
end
12 changes: 6 additions & 6 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
it 'is not valid without a name' do
tag.name = nil
tag.valid?
expect(tag.errors[:name]).to include("can't be blank")
expect(tag.errors.details[:name]).to include(error: :blank)
end

it 'is not valid with a repeated name' do
Expand All @@ -22,19 +22,19 @@

it 'stores names in downcase' do
name = 'MixOfUpper and DOWNS and spaces'
tag.name = 'MixOfUpper and DOWNS and spaces'
tag.name = name
tag.save
tag.reload
expect(tag.name).to eq(name.parameterize)
end

it 'validate a wrong tag' do
it 'find similar with a wrong tag' do
wrong_tag = FactoryBot.build(:tag, name: 'zemo')
expect(wrong_tag.validate?). to eq 'Maybe the tag zemo is wrong. Did you mean demo?. '
expect(wrong_tag.find_similar). to eq 'Maybe the tag zemo is wrong. Did you mean demo?'
end

it 'validate a correct tag' do
it 'find similar with a correct tag' do
correct_tag = FactoryBot.build(:tag, name: 'demo')
expect(correct_tag.validate?).to be_nil
expect(correct_tag.find_similar).to be_nil
end
end
27 changes: 26 additions & 1 deletion spec/models/tagging_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
require 'rails_helper'

RSpec.describe Tagging, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:tagging) { FactoryBot.create(:tagging) }

it 'is not valid without a spin' do
tagging.spin = nil
tagging.valid?
expect(tagging.errors.details[:spin]).to include(error: :blank)
end
it 'is always associated to a tag' do
tagging.tag = nil
tagging.valid?
expect(tagging.errors.details[:tag]).to include(error: :blank)
end

it 'is destroyed when the spin is destroyed' do
tagging
expect(Tagging.count).to eq(1)
expect{ tagging.spin.destroy }.to change(Tagging, :count).from(1).to(0)
expect{ tagging.reload }.to raise_exception ActiveRecord::RecordNotFound
end

it 'is destroyed when the tag is destroyed' do
tagging
expect(Tagging.count).to eq(1)
expect{ tagging.tag.destroy }.to change(Tagging, :count).from(1).to(0)
expect{ tagging.reload }.to raise_exception ActiveRecord::RecordNotFound
end
end

0 comments on commit 0ce6133

Please sign in to comment.