Yay! All tests passing. Now I just have to update the views.

master
Zequez 2015-08-07 10:30:41 -03:00
parent 1be2bfa491
commit 8c2d966fc8
14 changed files with 218 additions and 202 deletions

View File

@ -9,7 +9,7 @@ class Author < ActiveRecord::Base
belongs_to :user
has_many :authors_mods
has_many :mods, through: :authors_mods
has_many :mods
### Scopes and finders
#################
@ -22,6 +22,10 @@ class Author < ActiveRecord::Base
find_by_slug! normalize_friendly_id(name)
end
def self.find_or_initialize_by_slugged_name(name)
find_by_slug(normalize_friendly_id(name)) || Author.new(name: name)
end
# Since the author was autogenerated by scraping the forum,
# the forum_name == name
def self.find_for_forum_validation(forum_name)

View File

@ -15,6 +15,7 @@ class ForumValidation < ActiveRecord::Base
def associate_user_and_author!
author.user = user
author.save!
user.reload
update_column :validated, true
end

View File

@ -23,7 +23,7 @@ class Mod < ActiveRecord::Base
### Relationships
#################
belongs_to :author, validate: true
belongs_to :author, validate: true, autosave: true
belongs_to :owner, class_name: 'User'
belongs_to :game_version_start, class_name: 'GameVersion'
belongs_to :game_version_end, class_name: 'GameVersion'
@ -214,11 +214,15 @@ class Mod < ActiveRecord::Base
# Find or generate author from #author_name or from #owner.name
before_validation do
name = (owner.name.presence if owner_id_changed? && owner) || @author_name
if name.present?
author = Author.find_by_slugged_name(name) || Author.new(name: name)
author.user = owner if owner && author.new_record?
self.author = author
if owner
if owner.author
self.author = owner.author
else
self.author = Author.find_or_initialize_by_slugged_name(owner.name)
self.author.user = owner if self.author.new_record?
end
elsif @author_name.present?
self.author = Author.find_or_initialize_by_slugged_name(@author_name)
end
end

View File

@ -70,7 +70,7 @@ class User < ActiveRecord::Base
def give_ownership_of_authored!
if author
author.mods.each.each{ |am| am.update!(owner: self) unless am.owner.present? }
author.mods.each{ |am| am.update!(owner: self) unless am.owner.present? }
end
end

View File

@ -54,8 +54,8 @@ describe ForumValidationsController, type: :controller do
before(:each) do
@user = create :user
@author = create :author
create :mod, authors: [@author], owner: nil
create :mod, authors: [@author], owner: nil
create :mod, author: @author
create :mod, author: @author
@fv = create :forum_validation, user: @user, author: @author
end
@ -140,8 +140,8 @@ describe ForumValidationsController, type: :controller do
describe 'GET show' do
it 'should render the show template' do
@fv = create :forum_validation
@m1 = create :mod, authors: [@fv.author]
@m2 = create :mod, authors: [@fv.author]
@m1 = create :mod, author: @fv.author
@m2 = create :mod, author: @fv.author
@fv.associate_user_and_author!
@fv.user.give_ownership_of_authored!
sign_in @fv.user

View File

@ -1,7 +1,6 @@
FactoryGirl.define do
factory :mod do
sequence(:name) { |n| "Mod name #{n.to_s.rjust(6, '0')}" }
association :owner, factory: :user
categories { build_list :category, 1 }
description ''
forum_comments_count 12

View File

@ -15,7 +15,7 @@ feature 'Modder edits an existing mod' do
sign_in_admin
create_category 'potato'
author = create :author
create :mod, name: 'Hey', categories: [@category], owner: @user, authors: [author]
create :mod, name: 'Hey', categories: [@category], author: author
visit '/mods/hey/edit'
expect(find('#mod_author_name').value).to eq author.name
end

View File

@ -2,9 +2,9 @@ feature 'New user forum account validation' do
scenario 'user registers, submits the validation form, then visits the link' do
author = create :author, name: 'FactorioModsBot', forum_name: 'FactorioModsBot'
another_dude = create :user
m1 = create :mod, authors: [author], owner: nil
m2 = create :mod, authors: [author], owner: nil
create :mod, authors: [author], owner: another_dude
m1 = create :mod, author: author, owner: nil
m2 = create :mod, author: author, owner: nil
create :mod, author: author, owner: another_dude
create :mod, owner: nil
visit new_user_registration_path

View File

@ -14,7 +14,7 @@ feature 'The goddamn game version string should be updated after editing a mod'
fill_in 'mod_name', with: 'This Bug, Man'
fill_in 'mod_info_json_name', with: 'this-bug'
select 'CategoryOne', from: 'mod_category_ids'
fill_in 'mod_authors_list', with: 'Whatever'
fill_in 'mod_author_name', with: 'Whatever'
fill_in 'mod_versions_attributes_0_number', with: '1.2.3'
fill_in 'mod_versions_attributes_0_released_at', with: '2015-01-01'
select '0.10', from: 'mod_versions_attributes_0_game_version_ids'

View File

@ -55,8 +55,8 @@ describe ForumValidation, type: :model do
@u = create :user, name: 'Potato'
@a = create :author, name: 'Salatto', forum_name: 'Salatto' # Italian salad
@fv = create :forum_validation, user: @u, author: @a
@m1 = create :mod, authors: [@a], owner: nil, name: 'lalala'
@m2 = create :mod, authors: [@a], owner: nil, name: 'lelele'
@m1 = create :mod, author: @a, owner: nil, name: 'lalala'
@m2 = create :mod, author: @a, owner: nil, name: 'lelele'
end
it 'should create a new forum bot if not authenticated' do

View File

@ -168,13 +168,13 @@ describe Mod do
it 'should use the author name as the second part of the slug when clashing' do
mod1 = create :mod, name: 'Potato!'
mod2 = create :mod, name: 'Potato?', authors_list: 'Salad'
mod2 = create :mod, name: 'Potato?', author_name: 'Salad'
expect(mod1.slug).to eq 'potato'
expect(mod2.slug).to eq 'potato-by-salad'
end
it 'should not allow the "new" slug as it clashes with the controller action' do
mod = create :mod, name: 'New!', authors_list: 'Potato'
mod = create :mod, name: 'New!', author_name: 'Potato'
expect(mod.slug).to eq 'new-by-potato'
end
end
@ -354,175 +354,179 @@ describe Mod do
end
end
describe '#author_name' do
it 'should create new author with the name' do
mod = create :mod, author_name: 'Potato', owner: nil
expect(mod.author.name).to eq 'Potato'
describe 'before validation #author_name, #author and #owner' do
describe '#author_name' do
it 'should create new author with the name' do
mod = create :mod, author_name: 'Potato', owner: nil
expect(mod.author.name).to eq 'Potato'
end
it 'should use an existing author if it already exists' do
create :author, name: 'Potato Garch'
mod = create :mod, author_name: 'potato-garch', owner: nil
expect(mod.author.name).to eq 'Potato Garch'
end
it 'should add author#name validation error to #author_name' do
mod = build :mod, author_name: '0-0', owner: nil
expect(mod).to be_invalid
expect(mod.errors[:author_name].size).to be > 0
end
it 'should allow a blank #author_name' do
mod = build :mod, author_name: '', owner: nil
expect(mod).to be_valid
end
end
it 'should use an existing author if it already exists' do
create :author, name: 'Potato Garch'
mod = create :mod, author_name: 'potato-garch', owner: nil
expect(mod.author.name).to eq 'Potato Garch'
end
describe '#author' do
it 'should be generated automatically if the mod has an owner' do
user = create :user, name: 'PotatoGalaxy2015'
mod = create :mod, owner: user
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq user
end
it 'should add author#name validation error to #author_name' do
mod = build :mod, author_name: '0-0', owner: nil
expect(mod).to be_invalid
expect(mod.errors[:author_name].size).to be > 0
end
it 'should not associate the new author with the user if it already exists and has an user' do
previous_user = create :user
author = create :author, name: 'PotatoGalaxy2015', user: previous_user
user = create :user, name: 'PotatoGalaxy2015'
mod = create :mod, owner: user
expect(mod.author).to eq author
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq previous_user
end
it 'should allow a blank #author_name' do
mod = build :mod, author_name: '', owner: nil
expect(mod).to be_valid
it 'should be set to the new owner if we change the owner' do
u1 = create :user, name: 'PotatoGalaxy2015'
u2 = create :user, name: 'ChoripanCrudoDeLaCostanera'
mod = create :mod, owner: u1
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq u1
mod.update! owner: u2
mod.reload
expect(mod.author.name).to eq 'ChoripanCrudoDeLaCostanera'
expect(mod.author.user).to eq u2
end
it "should set it to the new owner even if author_name was also set" do
u1 = create :user, name: 'PotatoGalaxy2015'
u2 = create :user, name: 'ChoripanCrudoDeLaCostanera'
mod = create :mod, owner: u1
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq u1
mod.update! owner: u2, author_name: 'Potato'
mod.reload
expect(Author.find_by_slugged_name 'potato').to eq nil
expect(mod.author.name).to eq 'ChoripanCrudoDeLaCostanera'
expect(mod.author.user).to eq u2
end
end
end
describe '#author' do
it 'should be generated and associated automatically if the mod has an owner' do
user = create :user, name: 'PotatoGalaxy2015'
mod = create :mod, owner: user
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq user
end
it 'should not associate the new author if it already exists' do
create :author, name: 'PotatoGalaxy2015', user: nil
user = create :user, name: 'PotatoGalaxy2015'
mod = create :mod, owner: user
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq nil
end
it 'should be set to the new owner if we change the owner' do
u1 = create :user, name: 'PotatoGalaxy2015'
u2 = create :user, name: 'ChoripanCrudoDeLaCostanera'
mod = create :mod, owner: u1
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq u1
mod.update! owner: u2
mod.reload
expect(mod.author.name).to eq 'ChoripanCrudoDeLaCostanera'
expect(mod.author.user).to eq u2
end
it "should set it to the new owner even if author_name was also set" do
u1 = create :user, name: 'PotatoGalaxy2015'
u2 = create :user, name: 'ChoripanCrudoDeLaCostanera'
mod = create :mod, owner: u1
expect(mod.author.name).to eq 'PotatoGalaxy2015'
expect(mod.author.user).to eq u1
mod.update! owner: u2, author_name: 'Potato'
mod.reload
expect(Author.find_by_slugged_name 'potato').to eq nil
expect(mod.author.name).to eq 'ChoripanCrudoDeLaCostanera'
expect(mod.author.user).to eq u2
end
end
describe '#authors_list' do
it { is_expected.to respond_to :authors_list }
it 'associate the #authors by name separated by commas' do
create :author, name: 'Apple'
u2 = create :author, name: 'Potato'
u3 = create :author, name: 'Orange'
u4 = create :author, name: 'Banana'
mod.authors_list = 'Orange,Potato,Banana'
mod.save!
expect(mod.authors).to eq [u3, u2, u4]
end
it 'should order them correctly' do
create :author, name: 'Apple'
u2 = create :author, name: 'Potato'
u3 = create :author, name: 'Orange'
u4 = create :author, name: 'Banana'
mod.authors_list = 'Orange,Potato,Banana'
mod.save!
mod = Mod.first
expect(mod.authors).to eq [u3, u2, u4]
mod.authors_list = 'Potato,Banana,Orange'
mod.save!
mod = Mod.first
expect(mod.authors).to eq [u2, u4, u3]
end
it 'should work with random spaces everywhere' do
create :author, name: 'Apple'
u2 = create :author, name: 'Potato'
u3 = create :author, name: 'Orange'
u4 = create :author, name: 'Banana'
mod.authors_list = ' Orange , Potato , Banana '
mod.save!
expect(mod.authors).to eq [u3, u2, u4]
end
it 'should work with different cases' do
create :author, name: 'Apple'
u2 = create :author, name: 'Potato'
u3 = create :author, name: 'Orange'
u4 = create :author, name: 'Banana'
mod.authors_list = 'orange,potato,banana'
mod.save!
expect(mod.authors).to eq [u3, u2, u4]
end
it "should create a new author if it doesn't exist" do
u1 = create :author, name: 'Apple'
mod.authors_list = 'Apple,Watermelon'
mod.save!
u2 = Author.last
expect(u2.name).to eq 'Watermelon'
expect(u2.forum_name).to eq 'Watermelon'
expect(mod.authors).to eq [u1, u2]
end
it 'should be invalid if the user to be generated is invalid' do
mod.authors_list = 'Bi$cuit,1234'
expect(mod).to be_invalid
expect(mod.errors[:authors_list]).to eq ['1234 is invalid']
end
it 'should be invalid with more than 8 authors' do
mod.authors_list = 'Apple,Potato,Watermelon,Orange,Clementine,Fennel,Banana,Melon,Strawberry'
expect(mod).to be_invalid
expect(mod.errors[:authors_list].first).to match(/too many authors/i)
end
it 'should not create the users if the validation fails' do
mod = build :mod, authors_list: 'Apple,---,Fennel'
expect(mod.save).to eq false
expect(User.all).to eq [mod.owner]
end
it 'should ignore duplicated names and use the first apparition' do
mod = build :mod, authors_list: 'Apple, Potato, Apple'
expect(mod).to be_valid
mod.save!
expect(mod.authors.map(&:name)).to eq %w{Apple Potato}
end
it 'should ignore empty authors' do
mod = build :mod, authors_list: 'Apple,,Potato'
expect(mod).to be_valid
mod.save!
expect(mod.authors.map(&:name)).to eq %w{Apple Potato}
end
# This is some top-notch DDoS protection
it 'should disregard everything after the tenth author' do
mod = build :mod, authors_list: "Au0, Au1, Au2, Au4, Au5, Au6, Au7, Au8, Au9, Au10, Au11, Au12"
expect(mod).to be_invalid
expect(mod.authors.size).to eq 10
end
it 'return a list of names if called after the mod loads' do
authors = 5.times.map{ |i| create :author, name: "Au#{i}" }
mod = create :mod, authors: authors
expect(mod.authors_list).to eq 'Au0, Au1, Au2, Au3, Au4'
end
end
# describe '#authors_list' do
# it { is_expected.to respond_to :authors_list }
#
# it 'associate the #authors by name separated by commas' do
# create :author, name: 'Apple'
# u2 = create :author, name: 'Potato'
# u3 = create :author, name: 'Orange'
# u4 = create :author, name: 'Banana'
# mod.authors_list = 'Orange,Potato,Banana'
# mod.save!
# expect(mod.authors).to eq [u3, u2, u4]
# end
#
# it 'should order them correctly' do
# create :author, name: 'Apple'
# u2 = create :author, name: 'Potato'
# u3 = create :author, name: 'Orange'
# u4 = create :author, name: 'Banana'
# mod.authors_list = 'Orange,Potato,Banana'
# mod.save!
# mod = Mod.first
# expect(mod.authors).to eq [u3, u2, u4]
# mod.authors_list = 'Potato,Banana,Orange'
# mod.save!
# mod = Mod.first
# expect(mod.authors).to eq [u2, u4, u3]
# end
#
# it 'should work with random spaces everywhere' do
# create :author, name: 'Apple'
# u2 = create :author, name: 'Potato'
# u3 = create :author, name: 'Orange'
# u4 = create :author, name: 'Banana'
# mod.authors_list = ' Orange , Potato , Banana '
# mod.save!
# expect(mod.authors).to eq [u3, u2, u4]
# end
#
# it 'should work with different cases' do
# create :author, name: 'Apple'
# u2 = create :author, name: 'Potato'
# u3 = create :author, name: 'Orange'
# u4 = create :author, name: 'Banana'
# mod.authors_list = 'orange,potato,banana'
# mod.save!
# expect(mod.authors).to eq [u3, u2, u4]
# end
#
# it "should create a new author if it doesn't exist" do
# u1 = create :author, name: 'Apple'
# mod.authors_list = 'Apple,Watermelon'
# mod.save!
# u2 = Author.last
# expect(u2.name).to eq 'Watermelon'
# expect(u2.forum_name).to eq 'Watermelon'
# expect(mod.authors).to eq [u1, u2]
# end
#
# it 'should be invalid if the user to be generated is invalid' do
# mod.authors_list = 'Bi$cuit,1234'
# expect(mod).to be_invalid
# expect(mod.errors[:authors_list]).to eq ['1234 is invalid']
# end
#
# it 'should be invalid with more than 8 authors' do
# mod.authors_list = 'Apple,Potato,Watermelon,Orange,Clementine,Fennel,Banana,Melon,Strawberry'
# expect(mod).to be_invalid
# expect(mod.errors[:authors_list].first).to match(/too many authors/i)
# end
#
# it 'should not create the users if the validation fails' do
# mod = build :mod, authors_list: 'Apple,---,Fennel'
# expect(mod.save).to eq false
# expect(User.all).to eq [mod.owner]
# end
#
# it 'should ignore duplicated names and use the first apparition' do
# mod = build :mod, authors_list: 'Apple, Potato, Apple'
# expect(mod).to be_valid
# mod.save!
# expect(mod.authors.map(&:name)).to eq %w{Apple Potato}
# end
#
# it 'should ignore empty authors' do
# mod = build :mod, authors_list: 'Apple,,Potato'
# expect(mod).to be_valid
# mod.save!
# expect(mod.authors.map(&:name)).to eq %w{Apple Potato}
# end
#
# # This is some top-notch DDoS protection
# it 'should disregard everything after the tenth author' do
# mod = build :mod, authors_list: "Au0, Au1, Au2, Au4, Au5, Au6, Au7, Au8, Au9, Au10, Au11, Au12"
# expect(mod).to be_invalid
# expect(mod.authors.size).to eq 10
# end
#
# it 'return a list of names if called after the mod loads' do
# authors = 5.times.map{ |i| create :author, name: "Au#{i}" }
# mod = create :mod, authors: authors
# expect(mod.authors_list).to eq 'Au0, Au1, Au2, Au3, Au4'
# end
# end
end
describe 'builders' do
@ -541,7 +545,7 @@ describe Mod do
expect(mod.versions[0]).to be_kind_of ModVersion
expect(mod.versions[0].files[0]).to be_kind_of ModFile
expect(mod.name).to eq 'rsarsarsarsa'
expect(mod.authors_list).to eq 'GuyGuy'
expect(mod.author_name).to eq 'GuyGuy'
expect(mod.forum_url).to eq 'http://potatopotato.com.potato'
expect(mod.versions[0].released_at).to be_within(1.second).of 1.day.ago
expect(mod.versions[0].game_versions).to eq [gv]

View File

@ -195,8 +195,8 @@ describe User, :type => :model do
it 'should set the user as owner of the mods associated with his author' do
user = create :user
author = create :author, user: user
m1 = create :mod, authors: [author], owner: nil
m2 = create :mod, authors: [author], owner: nil
m1 = create :mod, author: author, owner: nil
m2 = create :mod, author: author, owner: nil
user.reload
user.give_ownership_of_authored!
m1.reload
@ -209,8 +209,8 @@ describe User, :type => :model do
user = create :user
author = create :author, user: user
other_guy = create :user
m1 = create :mod, authors: [author], owner: other_guy
m2 = create :mod, authors: [author], owner: nil
m1 = create :mod, author: author, owner: other_guy
m2 = create :mod, author: author, owner: nil
user.reload
user.give_ownership_of_authored!
m1.reload
@ -223,12 +223,12 @@ describe User, :type => :model do
describe '#non_owned_authored_mods' do
it 'should give the #authored_mods that arent #owned_mods' do
user = create :user
author = create :author, user: user
create :mod, authors: [author], owner: user
author = create :author, user: nil
create :mod, author: author, owner: user
create :mod, owner: user
m1 = create :mod, authors: [author]
m2 = create :mod, authors: [author]
user.reload
m1 = create :mod, author: author, owner: nil
m2 = create :mod, author: author, owner: nil
author.update! user: user
expect(user.non_owned_authored_mods).to match_array [m1, m2]
end
end

View File

@ -1,12 +1,14 @@
describe 'forum_validations/new', type: :view do
it 'should render the mods associated with the author' do
fv = create :forum_validation
create :mod, authors: [fv.author], name: 'Potato'
create :mod, authors: [fv.author], name: 'Galaxy'
create :mod, authors: [fv.author], name: 'Simulator'
create :mod, author: fv.author, name: 'Potato'
create :mod, author: fv.author, name: 'Galaxy'
create :mod, author: fv.author, name: 'Simulator'
assign(:forum_validation, fv)
render
expect(rendered).to match(/Potato.*Galaxy.*Simulator/m)
expect(rendered).to match(/Potato/)
expect(rendered).to match(/Galaxy/)
expect(rendered).to match(/Simulator/)
expect(rendered).to have_selector("#forum_validation_author_id[value=\"#{fv.author_id}\"]")
end
end

View File

@ -1,15 +1,17 @@
describe 'forum_validations/show', type: :view do
before :each do
@fv = create :forum_validation
@m1 = create :mod, authors: [@fv.author], name: 'Potato'
@m2 = create :mod, authors: [@fv.author], name: 'Salad'
@m3 = create :mod, authors: [@fv.author], name: 'Simulator'
@m1 = create :mod, author: @fv.author, name: 'Potato'
@m2 = create :mod, author: @fv.author, name: 'Salad'
@m3 = create :mod, author: @fv.author, name: 'Simulator'
end
it 'display the list of mods associated with the author' do
assign(:forum_validation, @fv)
render
expect(rendered).to match(/Potato.*Salad.*Simulator/m)
expect(rendered).to match(/Potato/)
expect(rendered).to match(/Salad/)
expect(rendered).to match(/Simulator/)
end
context '#validated = false' do