diff --git a/app/controllers/mods_controller.rb b/app/controllers/mods_controller.rb index 87e991e..17dfd85 100644 --- a/app/controllers/mods_controller.rb +++ b/app/controllers/mods_controller.rb @@ -33,7 +33,6 @@ class ModsController < ApplicationController def create @mod = Mod.new mod_params - # @mod.author_name = @mod.owner.name if @mod.owner && cannot?(:set_owner, Mod) if @mod.save redirect_to mod_url(@mod) else diff --git a/app/models/mod.rb b/app/models/mod.rb index 5eaf8c7..2cdb41b 100644 --- a/app/models/mod.rb +++ b/app/models/mod.rb @@ -11,7 +11,7 @@ class Mod < ActiveRecord::Base def slug_candidates [ :name, - authors.first && [:name, 'by', authors.first.name] + author && [:name, 'by', author.name] ] end @@ -23,7 +23,7 @@ class Mod < ActiveRecord::Base ### Relationships ################# - # belongs_to :author, class_name: 'User' # Deprecated, but don't remove it yet tons of tests break + belongs_to :author, validate: true belongs_to :owner, class_name: 'User' belongs_to :game_version_start, class_name: 'GameVersion' belongs_to :game_version_end, class_name: 'GameVersion' @@ -162,7 +162,7 @@ class Mod < ActiveRecord::Base forum_post = ForumPost.find forum_post_id mod.name = forum_post.title - mod.authors_list = forum_post.author_name + mod.author_name = forum_post.author_name mod.forum_url = forum_post.url if forum_post.published_at @@ -212,6 +212,15 @@ class Mod < ActiveRecord::Base end end + # 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 + end + end # Yes, I have to move the authors_list parser to another file # and move this again to the header. But for now, we need to access @@ -219,16 +228,9 @@ class Mod < ActiveRecord::Base # adds the slug generation also before_validation friendly_id :slug_candidates, use: [:slugged, :finders] - before_validation do - if author_name.present? - author = Author.find_by_slugged_name(author_name) || Author.new(name: author_name) - self.authors = [author] - end - end - after_validation do if author_name.present? - errors[:author_name].concat errors[:authors] + errors[:author_name].concat author.errors[:name] end end @@ -380,7 +382,7 @@ class Mod < ActiveRecord::Base end def author_name - @author_name ||= ( author = authors.first ) && author.name + @author_name || (author && author.name) end private diff --git a/app/views/mods/_form.html.haml b/app/views/mods/_form.html.haml index 3167050..c743054 100644 --- a/app/views/mods/_form.html.haml +++ b/app/views/mods/_form.html.haml @@ -8,7 +8,6 @@ = f.input :owner if can? :set_owner, mod = f.input :author_name, as: :datalist, collection: mods_authors_list if can? :set_owner, mod = f.input :additional_contributors - = f.input :authors_list, as: :multi_datalist, collection: mods_authors_list = f.input :github = f.input :official_url = f.input :contact diff --git a/db/migrate/20150807104820_add_author_to_mods.rb b/db/migrate/20150807104820_add_author_to_mods.rb new file mode 100644 index 0000000..1050eaa --- /dev/null +++ b/db/migrate/20150807104820_add_author_to_mods.rb @@ -0,0 +1,5 @@ +class AddAuthorToMods < ActiveRecord::Migration + def change + add_reference :mods, :author, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index ad7d6df..96dc226 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150807095122) do +ActiveRecord::Schema.define(version: 20150807104820) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -251,8 +251,10 @@ ActiveRecord::Schema.define(version: 20150807095122) do t.string "info_json_name", default: "", null: false t.integer "bookmarks_count", default: 0, null: false t.string "additional_contributors", default: "", null: false + t.integer "author_id" end + add_index "mods", ["author_id"], name: "index_mods_on_author_id", using: :btree add_index "mods", ["category_id"], name: "index_mods_on_category_id", using: :btree add_index "mods", ["forum_post_id"], name: "index_mods_on_forum_post_id", using: :btree add_index "mods", ["info_json_name"], name: "index_mods_on_info_json_name", using: :btree diff --git a/spec/controllers/mods_controller_spec.rb b/spec/controllers/mods_controller_spec.rb index c4cb03f..783ee6d 100644 --- a/spec/controllers/mods_controller_spec.rb +++ b/spec/controllers/mods_controller_spec.rb @@ -290,21 +290,20 @@ describe ModsController, type: :controller do submit_basic expect(response).to have_http_status :redirect mod = Mod.first - LA mod - expect(mod.authors.size).to eq 1 - expect(mod.authors.first.name).to eq user.name - expect(mod.authors.first.user).to eq user + expect(mod.author.name).to eq user.name + expect(mod.author.user).to eq user end end context 'user is an admin' do - it 'should allow it set #visible, #owner or #slug' do + it 'should allow it set #visible, #owner or #slug', focus: true do first_user = create :admin_user second_user = create :user sign_in first_user submit_basic visible: true, owner_id: second_user.id, slug: 'rsarsarsa' expect(response).to have_http_status :redirect + mod = Mod.first expect(mod.visible).to eq true expect(mod.owner).to eq second_user expect(mod.slug).to eq 'rsarsarsa' @@ -318,16 +317,15 @@ describe ModsController, type: :controller do expect(Mod.first.slug).to eq 'supermod' end - it 'should set the owner as the sole author' do + it 'should set the owner as the author' do admin = create :admin_user user = create :dev_user sign_in admin - submit_basic owner_id: user + submit_basic owner_id: user.id expect(response).to have_http_status :redirect mod = Mod.first - expect(mod.authors.size).to eq 1 - expect(mod.authors.first.name).to eq user.name - expect(mod.authors.first.user).to eq user + expect(mod.author.name).to eq user.name + expect(mod.author.user).to eq user end end end diff --git a/spec/features/mods_new_spec.rb b/spec/features/mods_new_spec.rb index 02abdbd..a2ca211 100644 --- a/spec/features/mods_new_spec.rb +++ b/spec/features/mods_new_spec.rb @@ -191,11 +191,11 @@ feature 'Modder creates a new mod' do sign_in_admin visit '/mods/new' fill_in_minimum 'Mod Name' - fill_in 'mod_authors_list', with: 'MangoDev' + fill_in 'mod_author_name', with: 'MangoDev' submit_form mod = Mod.first expect(current_path).to eq '/mods/mod-name' - expect(mod.authors.first.name).to eq 'MangoDev' + expect(mod.author.name).to eq 'MangoDev' end scenario 'admin tries to create a mod from the forum_posts dashboard, so it has pre-filled attributes' do @@ -225,32 +225,10 @@ feature 'Modder creates a new mod' do visit "/mods/new" fill_in_minimum('SuperMod') - fill_in 'mod_author_name', with: 'yeah' submit_form expect(current_path).to eq '/mods/supermod-by-yeah' end - scenario 'user submits a mod with a valid name in the #author_name' do - sign_in_dev - visit '/mods/new' - fill_in_minimum - fill_in 'mod_author_name', with: 'Potato, SuperUser, Salad' - submit_form - expect(current_path).to eq '/mods/supermod' - expect(Mod.first.authors.map(&:name)).to eq %w{Potato SuperUser Salad} - end - - scenario 'user submits an mod with invalid names in the #author_name' do - sign_in_dev - visit '/mods/new' - fill_in_minimum - fill_in 'mod_author_name', with: '----' - submit_form - expect(current_path).to eq '/mods' - expect(page).to have_css '#mod_author_name_input .inline-errors' - expect(page).to have_content(/---- is invalid/) - end - describe 'visibility toggle' do scenario 'should be hidden for non-dev, and false' do sign_in @@ -294,41 +272,28 @@ feature 'Modder creates a new mod' do it_behaves_like 'admin or dev' do let(:sign_in_admin_or_dev){ sign_in_admin } end + + scenario 'admin submits a mod with a valid name in the #author_name' do + sign_in_admin + visit '/mods/new' + fill_in_minimum + fill_in 'mod_author_name', with: 'SuperUser' + submit_form + expect(current_path).to eq '/mods/supermod' + expect(Mod.first.author.name).to eq 'SuperUser' + end + + scenario 'admin submits an mod with invalid names in the #author_name' do + sign_in_admin + visit '/mods/new' + fill_in_minimum + fill_in 'mod_author_name', with: '----' + submit_form + expect(current_path).to eq '/mods' + expect(page).to have_css '#mod_author_name_input .inline-errors' + expect(page).to have_content(/is invalid/) + end end - - # context 'dev or admin submits a mod' do - # scenario 'should be visible and ON by default' do - # sign_in_dev - # visit '/mods/new' - # expect(page).to have_css '#mod_visible_input' - # expect(find('#mod_visible_input').value).to eq true - # fill_in_minimum - # submit_form - # expect(Mod.first.visible).to eq true - # end - - # scenario 'should be visible it should be changeable' do - # sign_in_dev - # visit '/mods/new' - # expect(page).to have_css '#mod_visible_input' - # expect(find('#mod_visible_input').value).to eq true - # uncheck 'mod_visible_input' - # fill_in_minimum - # submit_form - # expect(Mod.first.visible).to eq false - # end - # end - - - # scenario 'should be visible if an admin visits mods#new, and it should be ON by default' do - # sign_in - # visit '/mods/new' - # expect(page).to have_css '#mod_visible_input' - # expect(find('#mod_visible_input').value).to eq true - # fill_in_minimum - # submit_form - # expect(Mod.first.visible).to eq true - # end end def fill_in_minimum(*name) diff --git a/spec/lib/authors_users_separation_updater_spec.rb b/spec/lib/authors_users_separation_updater_spec.rb index ed154b2..b27a8a9 100644 --- a/spec/lib/authors_users_separation_updater_spec.rb +++ b/spec/lib/authors_users_separation_updater_spec.rb @@ -1,195 +1,198 @@ +# Deprecated spec, this was a temporal migration helper + describe AuthorsUsersSeparationUpdater do - subject(:updater){ AuthorsUsersSeparationUpdater.new } - - # This will ensure us that IDs start at 1 - before :each do - DatabaseCleaner.clean_with(:truncation) - end - - # users and mods are currently associated with the authors_mods table - # now the authors_mods table is used by the authors table instead of the users - - # To run the migration we need to: - # - All the autogenerated users should be *removed* from the users table - # and should be added to the authors table, using the user name as name - # and as forum_name. To do this we need create a new author for each user - # and after we create the author, we should get the all the authors_mods - # entries with that user.id and update them with the author id. - # - All the non-autogenerated users should be *copied* to the authors table - # and update all the authors_mods entries from the user.id to the newly - # created author.id - # After this we need to check if the mods associated with the author - # are also owned by the user. If so, then we validate the user by - # associating the author with the user. + # subject(:updater){ AuthorsUsersSeparationUpdater.new } + # + # # This will ensure us that IDs start at 1 # before :each do + # DatabaseCleaner.clean_with(:truncation) + # end + # + # # users and mods are currently associated with the authors_mods table + # # now the authors_mods table is used by the authors table instead of the users + # + # # To run the migration we need to: + # # - All the autogenerated users should be *removed* from the users table + # # and should be added to the authors table, using the user name as name + # # and as forum_name. To do this we need create a new author for each user + # # and after we create the author, we should get the all the authors_mods + # # entries with that user.id and update them with the author id. + # # - All the non-autogenerated users should be *copied* to the authors table + # # and update all the authors_mods entries from the user.id to the newly + # # created author.id + # # After this we need to check if the mods associated with the author + # # are also owned by the user. If so, then we validate the user by + # # associating the author with the user. + # + # # before :each do + # # u1 = User.autogenerate(name: 'Potato') + # # u2 = User.autogenerate(name: 'Galaxy') + # # u3 = create :user, name: 'Simulator' + # # u1.save! + # # u2.save! + # # + # # m1 = create :mod, owner: nil, authors: [] + # # m2 = create :mod, owner: u3, authors: [] + # # + # # create :authors_mod, mod_id: m1.id, author_id: u1.id + # # create :authors_mod, mod_id: m1.id, author_id: u2.id + # # + # # create :authors_mod, mod_id: m2.id, author_id: u2.id + # # end + # + # it 'should move all the autogenerated users to the authors table' do # u1 = User.autogenerate(name: 'Potato') # u2 = User.autogenerate(name: 'Galaxy') - # u3 = create :user, name: 'Simulator' # u1.save! # u2.save! # # m1 = create :mod, owner: nil, authors: [] - # m2 = create :mod, owner: u3, authors: [] + # m2 = create :mod, owner: nil, authors: [] # # create :authors_mod, mod_id: m1.id, author_id: u1.id # create :authors_mod, mod_id: m1.id, author_id: u2.id + # create :authors_mod, mod_id: m2.id, author_id: u1.id # - # create :authors_mod, mod_id: m2.id, author_id: u2.id + # # Mod1 has 2 autogenerated users, user1 and user2 that need to be moved + # # Mod2 has 1 autogenerated user1 that need to be moved + # + # expect(m1.authors).to eq [] + # expect(m2.authors).to eq [] + # + # expect(User.pluck(:name)).to eq %w{Potato Galaxy} + # expect(Author.pluck(:name)).to eq [] + # updater.run + # expect(User.pluck(:name)).to eq [] + # expect(Author.pluck(:name)).to eq %w{Potato Galaxy} + # + # m1.reload + # m2.reload + # + # expect(m1.authors.map(&:name)).to eq %w{Potato Galaxy} + # expect(m2.authors.map(&:name)).to eq %w{Potato} + # + # expect(User.find_by_id u1.id).to eq nil + # expect(User.find_by_id u2.id).to eq nil + # end + # + # it 'should not remove users that were not autogenerated' do + # u1 = User.autogenerate(name: 'Potato') + # u1.save! + # u2 = create :user, name: 'Galaxy' + # + # m1 = create :mod, owner: nil, authors: [] + # m2 = create :mod, owner: nil, authors: [] + # + # create :authors_mod, mod_id: m1.id, author_id: u1.id + # create :authors_mod, mod_id: m1.id, author_id: u2.id + # create :authors_mod, mod_id: m2.id, author_id: u1.id + # + # # Mod1 has 2 autogenerated users, user1 that needs to be moved, and user2 that need to copied + # # Mod2 has 1 autogenerated user1 that need to be moved + # + # expect(m1.authors).to eq [] + # expect(m2.authors).to eq [] + # + # expect(User.pluck(:name)).to eq %w{Potato Galaxy} + # expect(Author.pluck(:name)).to eq [] + # updater.run + # expect(User.pluck(:name)).to eq %w{Galaxy} + # expect(Author.pluck(:name)).to eq %w{Potato Galaxy} + # + # m1.reload + # m2.reload + # + # expect(m1.authors.map(&:name)).to eq %w{Potato Galaxy} + # expect(m2.authors.map(&:name)).to eq %w{Potato} + # + # expect(User.find_by_id u1.id).to eq nil + # expect(User.find_by_id u2.id).to eq u2 + # end + # + # it "should not create an author for a user that doesn't have any associated mod" do + # create :user, name: 'Potato' + # updater.run + # expect(Author.all).to be_empty + # end + # + # it 'should reset the authors #mods_count' do + # u1 = create :user, name: 'Potato' + # m1 = create :mod, owner: u1 + # create :authors_mod, mod_id: m1.id, author_id: u1.id + # + # updater.run + # + # a = Author.first + # expect(a.name).to eq 'Potato' + # expect(a.mods_count).to eq 1 + # end + # + # it 'should associate the user and the author if the user is owner of the mod' do + # u1 = create :user, name: 'Potato' + # u2 = create :user, name: 'Galaxy' + # u3 = User.autogenerate name: 'Simulator' + # u3.save! + # + # m1 = create :mod, owner: u1, authors: [] + # m2 = create :mod, owner: u3, authors: [] + # m3 = create :mod, owner: nil, authors: [] + # + # create :authors_mod, mod_id: m1.id, author_id: u1.id + # create :authors_mod, mod_id: m1.id, author_id: u2.id + # create :authors_mod, mod_id: m2.id, author_id: u1.id + # create :authors_mod, mod_id: m3.id, author_id: u3.id + # + # expect(User.pluck(:name)).to match_array %w{Potato Galaxy Simulator} + # expect(Author.pluck(:name)).to match_array [] + # updater.run + # expect(User.pluck(:name)).to match_array %w{Potato Galaxy} + # expect(Author.pluck(:name)).to match_array %w{Potato Galaxy Simulator} + # + # m1.reload + # m2.reload + # m3.reload + # + # expect(m1.authors.map(&:name)).to eq %w{Potato Galaxy} + # expect(m2.authors.map(&:name)).to eq %w{Potato} + # expect(m3.authors.map(&:name)).to eq %w{Simulator} + # expect(m1.owner).to eq u1 + # expect(m2.owner).to eq nil + # expect(m3.owner).to eq nil + # + # u1.reload + # u2.reload + # expect(User.find_by_id u3.id).to eq nil + # + # a1 = Author.find_by_name('Potato') + # a2 = Author.find_by_name('Galaxy') + # a3 = Author.find_by_name('Simulator') + # + # expect(a1.user).to eq u1 + # expect(a2.user).to eq nil + # expect(a3.user).to eq nil + # + # expect(a1.mods).to match_array [m1, m2] + # expect(a2.mods).to match_array [m1] + # expect(a3.mods).to match_array [m3] + # end + # + # it 'should not mess up with newly created AuthorsMod' do + # u1 = create :user, name: 'Potato' + # u2 = create :user, name: 'Galaxy' + # + # # We create and destroy a new author so the next one starts with ID=2 + # # and we can break this thing! + # Author.create!(name: 'Whatever').destroy! + # + # m1 = create :mod, owner: nil, authors: [] + # + # create :authors_mod, mod_id: m1.id, author_id: u1.id + # + # updater.run + # + # expect(Author.count).to eq 1 + # a1 = Author.last + # expect(a1.mods).to match_array [m1] # end - - it 'should move all the autogenerated users to the authors table' do - u1 = User.autogenerate(name: 'Potato') - u2 = User.autogenerate(name: 'Galaxy') - u1.save! - u2.save! - - m1 = create :mod, owner: nil, authors: [] - m2 = create :mod, owner: nil, authors: [] - - create :authors_mod, mod_id: m1.id, author_id: u1.id - create :authors_mod, mod_id: m1.id, author_id: u2.id - create :authors_mod, mod_id: m2.id, author_id: u1.id - - # Mod1 has 2 autogenerated users, user1 and user2 that need to be moved - # Mod2 has 1 autogenerated user1 that need to be moved - - expect(m1.authors).to eq [] - expect(m2.authors).to eq [] - - expect(User.pluck(:name)).to eq %w{Potato Galaxy} - expect(Author.pluck(:name)).to eq [] - updater.run - expect(User.pluck(:name)).to eq [] - expect(Author.pluck(:name)).to eq %w{Potato Galaxy} - - m1.reload - m2.reload - - expect(m1.authors.map(&:name)).to eq %w{Potato Galaxy} - expect(m2.authors.map(&:name)).to eq %w{Potato} - - expect(User.find_by_id u1.id).to eq nil - expect(User.find_by_id u2.id).to eq nil - end - - it 'should not remove users that were not autogenerated' do - u1 = User.autogenerate(name: 'Potato') - u1.save! - u2 = create :user, name: 'Galaxy' - - m1 = create :mod, owner: nil, authors: [] - m2 = create :mod, owner: nil, authors: [] - - create :authors_mod, mod_id: m1.id, author_id: u1.id - create :authors_mod, mod_id: m1.id, author_id: u2.id - create :authors_mod, mod_id: m2.id, author_id: u1.id - - # Mod1 has 2 autogenerated users, user1 that needs to be moved, and user2 that need to copied - # Mod2 has 1 autogenerated user1 that need to be moved - - expect(m1.authors).to eq [] - expect(m2.authors).to eq [] - - expect(User.pluck(:name)).to eq %w{Potato Galaxy} - expect(Author.pluck(:name)).to eq [] - updater.run - expect(User.pluck(:name)).to eq %w{Galaxy} - expect(Author.pluck(:name)).to eq %w{Potato Galaxy} - - m1.reload - m2.reload - - expect(m1.authors.map(&:name)).to eq %w{Potato Galaxy} - expect(m2.authors.map(&:name)).to eq %w{Potato} - - expect(User.find_by_id u1.id).to eq nil - expect(User.find_by_id u2.id).to eq u2 - end - - it "should not create an author for a user that doesn't have any associated mod" do - create :user, name: 'Potato' - updater.run - expect(Author.all).to be_empty - end - - it 'should reset the authors #mods_count' do - u1 = create :user, name: 'Potato' - m1 = create :mod, owner: u1 - create :authors_mod, mod_id: m1.id, author_id: u1.id - - updater.run - - a = Author.first - expect(a.name).to eq 'Potato' - expect(a.mods_count).to eq 1 - end - - it 'should associate the user and the author if the user is owner of the mod' do - u1 = create :user, name: 'Potato' - u2 = create :user, name: 'Galaxy' - u3 = User.autogenerate name: 'Simulator' - u3.save! - - m1 = create :mod, owner: u1, authors: [] - m2 = create :mod, owner: u3, authors: [] - m3 = create :mod, owner: nil, authors: [] - - create :authors_mod, mod_id: m1.id, author_id: u1.id - create :authors_mod, mod_id: m1.id, author_id: u2.id - create :authors_mod, mod_id: m2.id, author_id: u1.id - create :authors_mod, mod_id: m3.id, author_id: u3.id - - expect(User.pluck(:name)).to match_array %w{Potato Galaxy Simulator} - expect(Author.pluck(:name)).to match_array [] - updater.run - expect(User.pluck(:name)).to match_array %w{Potato Galaxy} - expect(Author.pluck(:name)).to match_array %w{Potato Galaxy Simulator} - - m1.reload - m2.reload - m3.reload - - expect(m1.authors.map(&:name)).to eq %w{Potato Galaxy} - expect(m2.authors.map(&:name)).to eq %w{Potato} - expect(m3.authors.map(&:name)).to eq %w{Simulator} - expect(m1.owner).to eq u1 - expect(m2.owner).to eq nil - expect(m3.owner).to eq nil - - u1.reload - u2.reload - expect(User.find_by_id u3.id).to eq nil - - a1 = Author.find_by_name('Potato') - a2 = Author.find_by_name('Galaxy') - a3 = Author.find_by_name('Simulator') - - expect(a1.user).to eq u1 - expect(a2.user).to eq nil - expect(a3.user).to eq nil - - expect(a1.mods).to match_array [m1, m2] - expect(a2.mods).to match_array [m1] - expect(a3.mods).to match_array [m3] - end - - it 'should not mess up with newly created AuthorsMod' do - u1 = create :user, name: 'Potato' - u2 = create :user, name: 'Galaxy' - - # We create and destroy a new author so the next one starts with ID=2 - # and we can break this thing! - Author.create!(name: 'Whatever').destroy! - - m1 = create :mod, owner: nil, authors: [] - - create :authors_mod, mod_id: m1.id, author_id: u1.id - - updater.run - - expect(Author.count).to eq 1 - a1 = Author.last - expect(a1.mods).to match_array [m1] - end end diff --git a/spec/models/mod_spec.rb b/spec/models/mod_spec.rb index b3154ee..0575bdb 100644 --- a/spec/models/mod_spec.rb +++ b/spec/models/mod_spec.rb @@ -61,6 +61,8 @@ describe Mod do it { expect(mod.authors.build).to be_kind_of Author } it { is_expected.to respond_to :authors_mods } it { expect(mod.authors_mods.build).to be_kind_of AuthorsMod } + it { is_expected.to respond_to :author } + its(:build_author) { is_expected.to be_kind_of Author } describe 'validation' do it 'should be valid by default from the factory' do @@ -354,37 +356,67 @@ describe Mod do describe '#author_name' do it 'should create new author with the name' do - mod = create :mod, author_name: 'Potato' - expect(mod.authors.first.name).to eq 'Potato' + 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' - expect(mod.authors.first.name).to eq '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' + 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: '' + mod = build :mod, author_name: '', owner: nil expect(mod).to be_valid end + end - it 'should always leave #authors with just 1 author' do - mod = create :mod, author_name: 'Potato' - expect(mod.authors.size).to eq 1 - expect(mod.authors.first.name).to eq 'Potato' + 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 - mod.author_name = 'Galaxy' - mod.save! + 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(mod.authors.size).to eq 1 - expect(mod.authors.first.name).to eq 'Galaxy' + 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