Mess things up a little

master
Zequez 2015-08-07 07:36:52 -03:00
parent d68e7d5062
commit bb48ae060d
7 changed files with 97 additions and 47 deletions

View File

@ -33,6 +33,7 @@ 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
@ -85,7 +86,10 @@ class ModsController < ApplicationController
]
]
(permitted << :owner_id) if can? :set_owner, Mod
if can? :set_owner, Mod
permitted << :owner_id
permitted << :author_name
end
(permitted << :visible) if can? :set_visibility, Mod
(permitted << :slug) if can? :set_slug, Mod

View File

@ -212,12 +212,26 @@ class Mod < ActiveRecord::Base
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
# author.first to generate the alternative slug, and friendly_id
# 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]
end
end
# add the #authors errors to #authors_list
after_validation do
if authors_list.present?
@ -294,6 +308,7 @@ class Mod < ActiveRecord::Base
attr_accessor :imgur_thumbnail
attr_accessor :imgur_normal
attr_accessor :authors_list
attr_accessor :author_name
alias_attribute :github_path, :github
alias_attribute :subforum_url, :forum_subforum_url
@ -364,6 +379,10 @@ class Mod < ActiveRecord::Base
@authors_list ||= authors.map(&:name).join(', ')
end
def author_name
@author_name ||= ( author = authors.first ) && author.name
end
private
def set_game_versions_string

View File

@ -6,8 +6,9 @@
= f.input :slug if can? :set_slug, mod
= f.input :categories, as: :categories_select
= 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, placeholder: mods_authors_list.sample(4).push('etc').join(', ')
= f.input :authors_list, as: :multi_datalist, collection: mods_authors_list
= f.input :github
= f.input :official_url
= f.input :contact

View File

@ -284,16 +284,17 @@ describe ModsController, type: :controller do
expect(Mod.first.visible).to eq false
end
# it 'should set the developer as the sole author' do
# user = create :dev_user
# sign_in user
# submit_basic
# 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
# end
it 'should set the developer as the sole author' do
user = create :dev_user
sign_in user
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
end
end
@ -304,7 +305,6 @@ describe ModsController, type: :controller do
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,17 +318,17 @@ describe ModsController, type: :controller do
expect(Mod.first.slug).to eq 'supermod'
end
# it 'should set the owner as the sole author' do
# admin = create :admin_user
# user = create :dev_user
# sign_in admin
# submit_basic owner_id: user
# 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
# end
it 'should set the owner as the sole author' do
admin = create :admin_user
user = create :dev_user
sign_in admin
submit_basic owner_id: user
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
end
end
end

View File

@ -11,13 +11,13 @@ feature 'Modder edits an existing mod' do
expect(page).to_not have_content('Edit Hey')
end
scenario 'user edits a mod with a list of authors' do
sign_in_dev
scenario 'admin edits a mod with an author' do
sign_in_admin
create_category 'potato'
authors = 5.times.map{ create :author }
create :mod, name: 'Hey', categories: [@category], owner: @user, authors: authors
author = create :author
create :mod, name: 'Hey', categories: [@category], owner: @user, authors: [author]
visit '/mods/hey/edit'
expect(find('#mod_authors_list').value).to eq authors.map(&:name).join(', ')
expect(find('#mod_author_name').value).to eq author.name
end
def submit_form

View File

@ -211,7 +211,7 @@ feature 'Modder creates a new mod' do
visit "/mods/new?forum_post_id=#{forum_post.id}"
expect(find('#mod_name').value).to eq '[0.11.x] Potato mod'
expect(find('#mod_authors_list').value).to eq 'SuperGuy'
expect(find('#mod_author_name').value).to eq 'SuperGuy'
expect(find('#mod_forum_url').value).to eq 'http://www.factorioforums.com/forum/viewtopic.php?f=14&t=6742'
expect(find('[id$=game_version_ids]').value).to eq [game_version.id.to_s]
expect(find('[id$=released_at]').value).to eq released_at.strftime('%Y-%m-%d')
@ -225,43 +225,32 @@ feature 'Modder creates a new mod' do
visit "/mods/new"
fill_in_minimum('SuperMod')
fill_in 'mod_authors_list', with: 'yeah'
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 valid names in the #authors_list' do
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_authors_list', with: 'Potato, SuperUser, Salad'
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 a mod with invalid names in the #authors_list' do
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_authors_list', with: 'Potato, ----, Salad'
fill_in 'mod_author_name', with: '----'
submit_form
expect(current_path).to eq '/mods'
expect(page).to have_css '#mod_authors_list_input .inline-errors'
expect(page).to have_css '#mod_author_name_input .inline-errors'
expect(page).to have_content(/---- is invalid/)
end
scenario 'user submits a mod too many authors in the #authors_list' do
sign_in_dev
visit '/mods/new'
fill_in_minimum
fill_in 'mod_authors_list', with: 'Potato, SuperUser, Salad, Tururu, Papapa, Aaaaa, Bbbbb, Ccccc, Ddddd'
submit_form
expect(current_path).to eq '/mods'
expect(page).to have_css '#mod_authors_list_input .inline-errors'
expect(page).to have_content(/too many/i)
end
describe 'visibility toggle' do
scenario 'should be hidden for non-dev, and false' do
sign_in

View File

@ -10,6 +10,7 @@ describe Mod do
it { is_expected.to respond_to :github_url }
it { is_expected.to respond_to :license }
it { is_expected.to respond_to :additional_contributors }
it { is_expected.to respond_to :author_name }
it { is_expected.to respond_to :first_version_date }
it { is_expected.to respond_to :last_version_date }
@ -351,6 +352,42 @@ describe Mod do
end
end
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'
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'
end
it 'should add author#name validation error to #author_name' do
mod = build :mod, author_name: '0-0'
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: ''
expect(mod).to be_valid
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'
mod.reload
mod.author_name = 'Galaxy'
mod.save!
mod.reload
expect(mod.authors.size).to eq 1
expect(mod.authors.first.name).to eq 'Galaxy'
end
end
describe '#authors_list' do
it { is_expected.to respond_to :authors_list }