Mod#authors it's officially un-needed!

master
Zequez 2015-08-07 10:57:21 -03:00
parent d76b57b91e
commit 30aaf43b0d
9 changed files with 65 additions and 148 deletions

View File

@ -6,7 +6,7 @@ class API::ModsController < API::APIController
def index
@mods = Mod
.includes([:categories, :authors, versions: :files])
.includes([:categories, :author, versions: :files])
.sort_by(:alpha)
.filter_by_search_query(params[:q])
.filter_by_category(params[:category])

View File

@ -5,7 +5,7 @@ class BookmarksController < ApplicationController
def index
@mods = current_user
.bookmarked_mods
.includes([:categories, :authors, :owner, :forum_post, versions: :files])
.includes([:categories, :author, :owner, :forum_post, versions: :files])
.decorate
end

View File

@ -64,7 +64,6 @@ class ModsController < ApplicationController
:forum_subforum_url,
:summary,
:imgur,
:authors_list,
:contact,
:info_json_name,
:additional_contributors,

View File

@ -1,8 +1,6 @@
class ModDecorator < Draper::Decorator
delegate :id, :name, :forum_url, :subforum_url, :additional_contributors
def authors_count; mod.authors.size end
def game_versions_string
return na if mod.game_versions_string.blank?
"v" + mod.game_versions_string
@ -57,19 +55,19 @@ class ModDecorator < Draper::Decorator
h.link_to(mod.author.name, mod.author)
end
def authors_links_list
return na if mod.authors.empty?
mod.authors.map do |author, i|
link = h.link_to(author.name, author)
if mod.owner_id and (author.user_id == mod.owner_id) and mod.authors.size > 1
maintainer = h.t('helpers.mods.maintainer')
link + " (#{maintainer})"
else
link
end
end.join(', ').html_safe
end
# def authors_links_list
# return na if mod.authors.empty?
#
# mod.authors.map do |author, i|
# link = h.link_to(author.name, author)
# if mod.owner_id and (author.user_id == mod.owner_id) and mod.authors.size > 1
# maintainer = h.t('helpers.mods.maintainer')
# link + " (#{maintainer})"
# else
# link
# end
# end.join(', ').html_safe
# end
def categories_links
mod.categories.map{ |cat| category_tag_link(cat) }.join.html_safe

View File

@ -1,10 +1,3 @@
# AutoHtml.add_filter(:simple_format_fix).with({}) do |text, html_options|
# require 'action_view'
# # text_array = text.match(/<div.*<\/div>/)
# ActionView::Base.new.simple_format(text, {class: 'p'}, sanitize: false, wrapper_tag: 'div' )
# end
class Mod < ActiveRecord::Base
extend FriendlyId
@ -43,8 +36,8 @@ class Mod < ActiveRecord::Base
has_many :game_versions, -> { uniq.sort_by_older_to_newer }, through: :mod_game_versions
has_many :categories, through: :categories_mods
has_many :categories_mods, dependent: :destroy
has_many :authors, ->{ includes(:authors_mods).order('authors_mods.sort_order') }, through: :authors_mods
has_many :authors_mods, dependent: :destroy
# has_many :authors, ->{ includes(:authors_mods).order('authors_mods.sort_order') }, through: :authors_mods
# has_many :authors_mods, dependent: :destroy
# has_one :latest_version, -> { sort_by_newer_to_older.limit(1) }, class_name: 'ModVersion'
# has_one :second_latest_version, -> { sort_by_newer_to_older.limit(1).offset(1) }, class_name: 'ModVersion'
@ -197,21 +190,6 @@ class Mod < ActiveRecord::Base
end
end
# find or generate users from #authors_list
before_validation do
if authors_list.present?
authors_names = authors_list.split(',')
.map(&:strip)
.reject(&:blank?)
.take(10)
.uniq{ |name| Author.normalize_friendly_id(name) }
self.authors = @reordered_authors = authors_names.map do |name|
Author.find_by_slugged_name(name) || Author.new(name: name, forum_name: name)
end
end
end
# Find or generate author from #author_name or from #owner.name
before_validation do
if owner
@ -226,9 +204,9 @@ class Mod < ActiveRecord::Base
end
end
# Yes, I have to move the authors_list parser to another file
# Yes, I have to move the author_name 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
# author to generate the alternative slug, and friendly_id
# adds the slug generation also before_validation
friendly_id :slug_candidates, use: [:slugged, :finders]
@ -238,33 +216,6 @@ class Mod < ActiveRecord::Base
end
end
# add the #authors errors to #authors_list
after_validation do
if authors_list.present?
authors.each do |author|
author.errors[:name].each do |error|
self.errors[:authors_list].push(author.name + ' ' + error)
end
end
# We can't do this because errors[:authors] also holds
# the individual authors errors without any information
# about the user
# errors[:authors_list].concat errors[:authors]
end
end
# Set the #sort_order of the #authors_mods
# We cannot use self.authors because it you cannot change
# the order of the association
after_save do
if @reordered_authors
@reordered_authors.each_with_index do |author, i|
authors_mods.where(author: author).update_all(sort_order: i)
end
end
end
### Validations
#################
@ -284,15 +235,6 @@ class Mod < ActiveRecord::Base
end
end
# #authors.count limit
validate do
if authors.size > 8
error_msg = I18n.t('activerecord.errors.models.mod.attributes.authors.too_many')
errors[:authors].push error_msg
errors[:authors_list].push error_msg if authors_list.present?
end
end
# Github URL
validate do
if github.present? and not extract_github_path(github)
@ -313,7 +255,6 @@ class Mod < ActiveRecord::Base
attr_accessor :imgur_url
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
@ -381,10 +322,6 @@ class Mod < ActiveRecord::Base
super(val.present? ? val : nil)
end
def authors_list
@authors_list ||= authors.map(&:name).join(', ')
end
def author_name
@author_name || (author && author.name)
end

View File

@ -78,7 +78,7 @@ class FakeDataGenerator
github_url = Forgery(:lorem_ipsum).words(1, random: true) + '/' + Forgery(:lorem_ipsum).words(1, random: true)
mod = Mod.create! name: Forgery(:lorem_ipsum).words(rand(3..6), random: true),
info_json_name: Forgery(:lorem_ipsum).words(rand(1..2), random: true),
authors: authors.sample(rand(0..3)).uniq,
author: authors.sample,
owner: [nil, nil].concat(users).sample,
categories: categories.sample(rand(1..4)),
github: rand > 50 ? nil : github_url,

View File

@ -69,7 +69,7 @@ describe API::ModsController, type: :controller do
end
context 'from a specific author' do
end
end

View File

@ -15,50 +15,50 @@ describe ModDecorator do
end
end
describe '#authors_links_list' do
it 'should return a comma separated authors list links' do
mod = create_decorated :mod, authors: 3.times.map{ |i| create :author, name: "Au#{i}" }
expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>, <a href="/authors/au1">Au1</a>, <a href="/authors/au2">Au2</a>'
end
it 'should add the (maintainer) text if the author is also the owner' do
authors = 3.times.map{ |i| create :author, name: "Au#{i}" }
user = create :user
authors[1].update! user: user
mod = create_decorated :mod, authors: authors, owner: user
expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>, <a href="/authors/au1">Au1</a> (maintainer), <a href="/authors/au2">Au2</a>'
end
it 'should not add the (maintainer) text if there is only 1 author' do
user = create :user
author = create :author, name: "Au0", user: user
mod = create_decorated :mod, authors: [author], owner: user
expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>'
end
it 'should return N/A if the mod has no authors associated' do
mod = create_decorated :mod, authors: []
expect(mod.authors_links_list).to eq 'N/A'
end
it 'should show them in the correct sorting order' do
authors = 3.times.map{ |i| create :author, name: "Au#{i}" }
mod = create :mod, name: 'SuperMod', authors: authors
mod.authors_mods[0].update_column :sort_order, 3
mod.authors_mods[1].update_column :sort_order, 2
mod.authors_mods[2].update_column :sort_order, 1
mod.reload
expect(mod.decorate.authors_links_list).to eq '<a href="/authors/au2">Au2</a>, <a href="/authors/au1">Au1</a>, <a href="/authors/au0">Au0</a>'
end
it 'should not add maintainer if both the author user and the mod owner are nil' do
authors = 3.times.map{ |i| create :author, name: "Au#{i}" }
create :user
mod = create_decorated :mod, authors: authors, owner: nil
expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>, <a href="/authors/au1">Au1</a>, <a href="/authors/au2">Au2</a>'
end
end
# describe '#authors_links_list' do
#
# it 'should return a comma separated authors list links' do
# mod = create_decorated :mod, authors: 3.times.map{ |i| create :author, name: "Au#{i}" }
# expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>, <a href="/authors/au1">Au1</a>, <a href="/authors/au2">Au2</a>'
# end
#
# it 'should add the (maintainer) text if the author is also the owner' do
# authors = 3.times.map{ |i| create :author, name: "Au#{i}" }
# user = create :user
# authors[1].update! user: user
# mod = create_decorated :mod, authors: authors, owner: user
# expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>, <a href="/authors/au1">Au1</a> (maintainer), <a href="/authors/au2">Au2</a>'
# end
#
# it 'should not add the (maintainer) text if there is only 1 author' do
# user = create :user
# author = create :author, name: "Au0", user: user
# mod = create_decorated :mod, authors: [author], owner: user
# expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>'
# end
#
# it 'should return N/A if the mod has no authors associated' do
# mod = create_decorated :mod, authors: []
# expect(mod.authors_links_list).to eq 'N/A'
# end
#
# it 'should show them in the correct sorting order' do
# authors = 3.times.map{ |i| create :author, name: "Au#{i}" }
# mod = create :mod, name: 'SuperMod', authors: authors
# mod.authors_mods[0].update_column :sort_order, 3
# mod.authors_mods[1].update_column :sort_order, 2
# mod.authors_mods[2].update_column :sort_order, 1
# mod.reload
# expect(mod.decorate.authors_links_list).to eq '<a href="/authors/au2">Au2</a>, <a href="/authors/au1">Au1</a>, <a href="/authors/au0">Au0</a>'
# end
#
# it 'should not add maintainer if both the author user and the mod owner are nil' do
# authors = 3.times.map{ |i| create :author, name: "Au#{i}" }
# create :user
# mod = create_decorated :mod, authors: authors, owner: nil
# expect(mod.authors_links_list).to eq '<a href="/authors/au0">Au0</a>, <a href="/authors/au1">Au1</a>, <a href="/authors/au2">Au2</a>'
# end
# end
describe '#forum_link' do
context 'only has forum post URL' do

View File

@ -57,10 +57,6 @@ describe Mod do
# it { is_expected.to respond_to :tags }
it { is_expected.to respond_to :favorites }
it { is_expected.to respond_to :favorites_count }
it { is_expected.to respond_to :authors }
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 }
@ -141,19 +137,6 @@ describe Mod do
expect(mod2.errors[:slug].size).to eq 1
end
it 'should be invalid with more than 8 authors' do
authors = 9.times.map{ create :author }
mod = build :mod, authors: authors
expect(mod).to be_invalid
expect(mod.errors[:authors].size).to eq 1
end
it 'should be valid with 8 authors' do
authors = 8.times.map{ create :author }
mod = build :mod, authors: authors
expect(mod).to be_valid
end
it 'should be invalid without #info_json_name' do
mod = build :mod, info_json_name: ''
expect(mod).to be_invalid