Added the API to get list of mods in crafted JSON

master
Zequez 2015-07-28 01:52:50 -03:00
parent beddcbe4cb
commit 4ef20af276
24 changed files with 417 additions and 186 deletions

View File

@ -173,8 +173,9 @@ $mods-list-size: 12-$filter-size
right: space()
z-index: 999
&.mod-install-button
bottom: rhythm(1.6) // THE HORROR. No vertical rhythm compliant!
// Add some spacing when the download button is shown
& + .mod-info-tag
margin-bottom: rhythm(1)
margin-bottom: rhythm(2.5)

View File

@ -52,6 +52,7 @@
.btn-download
float: right
margin-left: space()
margin-top: rhythm(1, $font-x-larger)
> a
+font-x-larger

View File

@ -11,6 +11,11 @@ class ModsController < ApplicationController
.visible
.page(params[:page]).per(20)
if params[:names].present?
@mods = @mods.filter_by_names(params[:names])
end
# This #to_sym is not a DDoS concern since the value is constrained in the router
@sort = params[:sort].to_sym
case @sort
when :alpha
@ -27,7 +32,7 @@ class ModsController < ApplicationController
@mods = @mods.sort_by_alpha
end
unless params[:v].blank?
if params[:v].present?
@game_version = GameVersion.find_by_number(params[:v])
if @game_version
@mods = @mods.filter_by_game_version @game_version
@ -36,14 +41,14 @@ class ModsController < ApplicationController
end
end
unless params[:q].blank?
if params[:q].present?
@query = params[:q][0..30]
@mods = @mods.filter_by_search_query(@query)
end
@uncategorized_mods_total_count = @mods.total_count
@all_mods_count = Mod.count
if params[:category_id]
if params[:category_id].present?
@category = Category.find_by_slug params[:category_id]
if @category
@mods = @mods.filter_by_category @category
@ -115,6 +120,8 @@ class ModsController < ApplicationController
:summary,
:imgur,
:authors_list,
:contact,
:info_json_name,
category_ids: [],
versions_attributes: [
:id,

View File

@ -1,5 +1,5 @@
class ModDecorator < Draper::Decorator
delegate :id, :name, :forum_url, :subforum_url
delegate :id, :name, :forum_url, :subforum_url, :as_json
def authors_count; mod.authors.size end
@ -140,6 +140,10 @@ class ModDecorator < Draper::Decorator
end
end
def install_protocol_url
"factoriomods://#{Base64.encode64(mod.to_json)}"
end
### Download button
###################

View File

@ -71,6 +71,11 @@ class Mod < ActiveRecord::Base
where('mods.name ILIKE ? OR mods.summary ILIKE ? OR mods.description ILIKE ?', "%#{query}%", "%#{query}%", "%#{query}%")
end
scope :filter_by_names, ->(names_list) do
names = names_list.split(',').map(&:strip)
where(info_json_name: names)
end
# def self.filter_by_search_query(query)
# s1 = s2 = s3 = self
@ -161,6 +166,7 @@ class Mod < ActiveRecord::Base
validates :forum_url, allow_blank: true, format: { with: /\Ahttps?:\/\/.*\Z/ }
validates :summary, length: { maximum: 1000 }
validates :slug, uniqueness: true
validates :info_json_name, presence: true # we shouldn't validate uniqueness though
# #categories.count limit
validate do
@ -274,6 +280,36 @@ class Mod < ActiveRecord::Base
@authors_list ||= authors.map(&:name).join(', ')
end
# We should eventually rename the mod attributes to match the ones in the API,
# we use these different attributes to roughly match the ones used in
# the mods info.json files
def as_json(options = {})
{
title: name,
name: info_json_name,
url: Rails.application.routes.url_helpers.mod_url(self), # Eww
description: summary,
homepage: official_url,
contact: contact,
authors: authors.map(&:name),
releases: versions.map do |version|
{
version: version.number,
released_at: version.released_at,
game_versions: version.game_versions_string.split('-'), # Ideally we should load the #game_versions
dependencies: [],
files: version.files.map do |file|
{
name: file.name,
url: file.download_url,
mirror: file.attachment.present? ? file.attachment.url : ''
}
end
}
end
}
end
private
def set_game_versions_string

View File

@ -65,11 +65,7 @@ class ModVersion < ActiveRecord::Base
#################
def game_versions_string
if precise_game_versions_string.blank?
read_attribute(:game_versions_string) || set_game_versions_string
else
precise_game_versions_string
end
end
def to_label

View File

@ -1,7 +1,11 @@
- if mod.last_version_has_downloads?
.mod-install-button.btn.btn-download{title: t('.install.title')}
%a{href: mod.install_protocol_url}
= icon 'download'
= t(".install.#{params[:action]}")
.mod-download-button.btn.btn-download
%a{href: mod.first_available_download_url}
= icon 'download'
= icon 'cloud-download'
= t('.download')
%ul.btn-download-versions
- mod.download_files(2) do |version, file|

View File

@ -9,12 +9,14 @@
= semantic_form_for @mod do |f|
= f.inputs do
= f.input :name
= f.input :info_json_name
= f.input :slug if can? :set_slug, @mod
= f.input :categories, as: :categories_select
= f.input :owner if can? :set_owner, @mod
= f.input :authors_list, as: :multi_datalist, collection: @existing_authors_names, placeholder: @existing_authors_names.sample(4).push('etc').join(', ')
= f.input :github
= f.input :official_url
= f.input :contact
= f.input :forum_url
= f.input :forum_subforum_url
= f.input :imgur

View File

@ -40,4 +40,5 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
end

View File

@ -36,4 +36,6 @@ Rails.application.configure do
# Raises error for missing translations
config.action_view.raise_on_missing_translations = true
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
end

View File

@ -50,6 +50,9 @@ en:
formtastic:
labels:
mod:
name: Title
info_json_name: Name
contact: Contact information
forum_url: 'Forum post URL'
official_url: 'Official URL'
media_links_string: 'Pictures or gifs links'
@ -65,6 +68,8 @@ en:
hints:
mod:
info_json_name: The name in info.json
contact: How to get with you
description: Markdown
media_links_string: '<a href="http://imgur.com">Imgur.com</a> URLs of pictures of animated gifs. Maximum: 6 links.'
forum_subforum_url: Big mods have their own subforum

View File

@ -66,6 +66,10 @@ en:
download_button:
download: Download
install:
title: Install with the Factorio Mod Manager, or any mod manager that can read the protocol
show: Install with FMM
index: Install
filter_bar:
any_game_version: Any Factorio version

View File

@ -0,0 +1,6 @@
class AddContactAndInfoJsonNameToMods < ActiveRecord::Migration
def change
add_column :mods, :contact, :string, default: '', null: false
add_column :mods, :info_json_name, :string, default: '', null: false
end
end

View File

@ -0,0 +1,5 @@
class AddInfoJsonNameIndex < ActiveRecord::Migration
def change
add_index :mods, :info_json_name
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150724155022) do
ActiveRecord::Schema.define(version: 20150727221452) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -213,11 +213,14 @@ ActiveRecord::Schema.define(version: 20150724155022) do
t.integer "last_version_id"
t.datetime "last_release_date"
t.boolean "visible", default: true, null: false
t.string "contact", default: "", null: false
t.string "info_json_name", default: "", null: false
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
add_index "mods", ["last_version_id"], name: "index_mods_on_last_version_id", using: :btree
add_index "mods", ["slug"], name: "index_mods_on_slug", unique: true, using: :btree

View File

@ -68,6 +68,7 @@ class FakeDataGenerator
rand(30..50).times do |i|
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: users.sample(8),
owner: [nil, nil].concat(users).sample,
categories: categories.sample(rand(1..4)),

View File

@ -27,11 +27,11 @@ describe ModsController, type: :controller do
get 'index', options
end
describe "get_index" do
describe 'GET index' do
context 'pagination, 25 mods' do
it 'should load the first 20 mods on the first page' do
first_page_mods = 20.times.map{ create :mod }
second_page_mods = 5.times.map{ create :mod }
5.times.map{ create :mod }
get_index sort: :alpha
expect(response).to be_success
expect(response).to render_template 'index'
@ -39,7 +39,7 @@ describe ModsController, type: :controller do
end
it 'should load the last 5 mods on the second page' do
first_page_mods = 20.times.map{ create :mod }
20.times.map{ create :mod }
second_page_mods = 5.times.map{ create :mod }
get_index sort: :alpha, page: 2
expect(response).to be_success
@ -141,9 +141,9 @@ describe ModsController, type: :controller do
gv1 = create :game_version, number: '1.1.x'
gv2 = create :game_version, number: '1.2.x'
gv3 = create :game_version, number: '1.3.x'
mv1 = create :mod_version, game_versions: [gv1, gv2], mod: @m1
mv2 = create :mod_version, game_versions: [gv2, gv3], mod: @m2
mv3 = create :mod_version, game_versions: [gv3], mod: @m3
create :mod_version, game_versions: [gv1, gv2], mod: @m1
create :mod_version, game_versions: [gv2, gv3], mod: @m2
create :mod_version, game_versions: [gv3], mod: @m3
end
context 'version 1.1.x' do
@ -182,9 +182,33 @@ describe ModsController, type: :controller do
expect(assigns(:mods)).to match_array mods
end
end
context 'searching for specific mods in an API-wise way' do
before :each do
@category = create :category
@mods = []
@mods << create(:mod, name: 'ccc', info_json_name: 'banana', categories: [@category])
@mods << create(:mod, name: 'bbb', info_json_name: 'potato')
@mods << create(:mod, name: 'aaa', info_json_name: 'cabbage', categories: [@category])
@mods << create(:mod, name: 'ddd', info_json_name: 'watermelon')
@mods << create(:mod, name: 'kkk', info_json_name: 'orange')
@mods << create(:mod, name: 'zzz', info_json_name: 'pomegranate')
@mods << create(:mod, name: 'yyy', info_json_name: 'machine-gun')
end
describe "GET 'show'" do
it 'should get the correct list of mods' do
get_index names: 'banana,orange,machine-gun'
expect(assigns(:mods)).to match_array [@mods[0], @mods[4], @mods[6]]
end
it 'should with the other filter' do
get_index names: 'banana,cabbage,machine-gun', category_id: @category.to_param, sort: :alpha
expect(assigns(:mods)).to eq [@mods[2], @mods[0]]
end
end
end
describe 'GET show' do
context 'finds the mod' do
before(:each) { get 'show', category_id: mod.categories.first.to_param, id: mod.to_param }
@ -202,7 +226,7 @@ describe ModsController, type: :controller do
describe 'POST create' do
def submit_basic(extra_params = {})
post :create, mod: {name: 'SuperMod', category_ids: [create(:category).id]}.merge(extra_params)
post :create, mod: {name: 'SuperMod', info_json_name: 'supermod', category_ids: [create(:category).id]}.merge(extra_params)
end
def submit_blank(params = {})

View File

@ -86,9 +86,18 @@ describe ModDecorator do
mod = build :mod, versions: []
mod.save!
mod_version = create :mod_version, mod: mod
mod_file = create :mod_file, mod_version: mod_version
create :mod_file, mod_version: mod_version
mod = Mod.first.decorate
expect(mod.has_files?).to eq true
end
end
describe '#install_protocol_url' do
it 'should return the factoriomods:// protocol with the JSON-encoded mod' do
include ActionView::Helpers::TextHelper
mod = create(:mod).decorate
encoded_json = Base64.encode64 mod.to_json
expect(mod.install_protocol_url).to eq "factoriomods://#{encoded_json}"
end
end
end

View File

@ -6,6 +6,7 @@ FactoryGirl.define do
description ''
forum_comments_count 12
downloads_count 15
sequence(:info_json_name) { |n| "mod-name-#{n}" }
end
factory :category do

View File

@ -81,7 +81,7 @@ feature 'Display an index of mods in certain order' do
context 'when requesting JSON', driver: :rack_test_json do
it_behaves_like 'mod index' do
def mod_names
JSON.parse(last_response).map { |hash| hash["name"] }
JSON.parse(last_response).map { |hash| hash["title"] }
end
end
end
@ -91,7 +91,7 @@ feature 'Display an index of mods in certain order' do
authors = 5.times.map{ |i| create :user, name: "Au#{i}" }
create :mod, name: 'SuperMod', authors: authors
visit '/'
expect(page).to have_content /Au0.*Au1.*Au2.*Au3.*Au4/
expect(page).to have_content(/Au0.*Au1.*Au2.*Au3.*Au4/)
expect(page).to have_link 'Au0', '/users/au0'
expect(page).to have_link 'Au1', '/users/au1'
expect(page).to have_link 'Au2', '/users/au2'
@ -103,7 +103,7 @@ feature 'Display an index of mods in certain order' do
authors = 5.times.map{ |i| create :user, name: "Au#{i}" }
create :mod, name: 'SuperMod', authors: authors, owner: authors[1]
visit '/'
expect(page).to have_content /Au0.*Au1.*(maintainer).*Au2.*Au3.*Au4/
expect(page).to have_content(/Au0.*Au1.*(maintainer).*Au2.*Au3.*Au4/)
end
scenario 'Mod with multiple authors with reversed sorting order' do
@ -115,7 +115,7 @@ feature 'Display an index of mods in certain order' do
mod.authors_mods[3].update_column :sort_order, 2
mod.authors_mods[4].update_column :sort_order, 1
visit '/'
expect(page).to have_content /Au4.*Au3.*Au2.*Au1.*Au0/
expect(page).to have_content(/Au4.*Au3.*Au2.*Au1.*Au0/)
end
end
end

View File

@ -53,13 +53,12 @@ feature 'Modder creates a new mod' do
scenario 'user submits a barebones form' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'Super Mod'
select 'Terrain', from: 'Categories'
fill_in_first_version_and_file
fill_in_minimum 'Super Mod'
submit_form
expect(current_path).to eq '/mods/super-mod'
mod = Mod.first
expect(mod.name).to eq 'Super Mod'
expect(mod.info_json_name).to eq 'super mod'
expect(mod.categories).to match_array [@category]
expect(mod.author).to eq @user
end
@ -67,8 +66,7 @@ feature 'Modder creates a new mod' do
scenario 'user submits a form without any mod version', js: true do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'Super Mod'
select 'Terrain', from: 'Categories'
fill_in_no_versions_minimum 'Super Mod'
first('.remove_fields').click
submit_form
expect(current_path).to eq '/mods/super-mod'
@ -85,6 +83,7 @@ feature 'Modder creates a new mod' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'ModName'
fill_in 'mod_info_json_name', with: 'mod-name'
9.times{ |i| select "Cat#{i}", from: 'Categories' }
fill_in_first_version_and_file
submit_form
@ -97,6 +96,7 @@ feature 'Modder creates a new mod' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'ModName'
fill_in 'mod_info_json_name', with: 'mod-name'
7.times{ |i| select "Cat#{i}", from: 'Categories' }
fill_in_first_version_and_file
submit_form
@ -125,8 +125,7 @@ feature 'Modder creates a new mod' do
scenario 'user submits a mod with a mod_version with a perfectly fine number' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'ModName'
select 'Terrain', from: 'Categories'
fill_in_no_versions_minimum 'ModName'
within('.mod-version:nth-child(1)') do
fill_in 'Number', with: '1.2.3_5-potato'
fill_in 'Release day', with: 3.weeks.ago
@ -142,17 +141,16 @@ feature 'Modder creates a new mod' do
scenario 'user submits a mod with all the data but no versions' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'Mah super mod'
select 'Terrain', from: 'Categories'
fill_in_minimum 'SuperMod'
fill_in 'Github', with: 'http://github.com/factorio-mods/mah-super-mod'
fill_in 'Forum post URL', with: 'http://www.factorioforums.com/forum/viewtopic.php?f=14&t=5971&sid=1786856d6a687e92f6a12ad9425aeb9e'
fill_in 'Official URL', with: 'http://www.factorioforums.com/'
fill_in 'Summary', with: 'This is a small mod for testing'
fill_in_first_version_and_file
submit_form
expect(current_path).to eq '/mods/mah-super-mod'
expect(current_path).to eq '/mods/supermod'
mod = Mod.first
expect(mod.name).to eq 'Mah super mod'
expect(mod.name).to eq 'SuperMod'
expect(mod.info_json_name).to eq 'supermod'
expect(mod.categories).to match_array [Category.first]
expect(mod.github).to eq 'factorio-mods/mah-super-mod'
expect(mod.forum_url).to eq 'http://www.factorioforums.com/forum/viewtopic.php?f=14&t=5971&sid=1786856d6a687e92f6a12ad9425aeb9e'
@ -168,8 +166,7 @@ feature 'Modder creates a new mod' do
create :game_version, number: '1.2.x'
attachment = File.new(Rails.root.join('spec', 'fixtures', 'test.zip'))
visit '/mods/new'
fill_in 'mod_name', with: 'Valid mod name'
select 'Terrain', from: 'Categories'
fill_in_no_versions_minimum
within('.mod-version:nth-child(1)') do
fill_in 'Number', with: '123'
fill_in 'Release day', with: '2014-11-09'
@ -182,8 +179,8 @@ feature 'Modder creates a new mod' do
end
submit_form
mod = Mod.first
expect(current_path).to eq '/mods/valid-mod-name'
expect(page).to have_content 'Valid mod name'
expect(current_path).to eq '/mods/supermod'
expect(page).to have_content 'SuperMod'
expect(mod.versions[0].number).to eq '123'
expect(mod.versions[0].released_at).to eq Time.zone.parse('2014-11-09')
expect(mod.versions[0].game_versions[0].number).to eq '1.1.x'
@ -194,10 +191,8 @@ feature 'Modder creates a new mod' do
scenario 'admin user submits a mod selecting an owner' do
sign_in_admin
visit '/mods/new'
fill_in 'mod_name', with: 'Mod Name'
select 'Terrain', from: 'Categories'
fill_in_minimum 'Mod Name'
fill_in 'mod_authors_list', with: 'MangoDev'
fill_in_first_version_and_file
submit_form
mod = Mod.first
expect(current_path).to eq '/mods/mod-name'
@ -230,9 +225,7 @@ feature 'Modder creates a new mod' do
mod = create :mod, name: 'SuperMod'
visit "/mods/new"
fill_in 'mod_name', with: 'SuperMod'
select 'Terrain', from: 'Categories'
fill_in_first_version_and_file
fill_in_minimum('SuperMod')
submit_form
expect(current_path).to eq '/mods/supermod-by-yeah'
end
@ -240,10 +233,8 @@ feature 'Modder creates a new mod' do
scenario 'user submits a mod with valid names in the #authors_list' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'SuperMod'
select 'Terrain', from: 'Categories'
fill_in_minimum
fill_in 'mod_authors_list', with: 'Potato, SuperUser, Salad'
fill_in_first_version_and_file
submit_form
expect(current_path).to eq '/mods/supermod'
expect(Mod.first.authors.map(&:name)).to eq %w{Potato SuperUser Salad}
@ -252,10 +243,8 @@ feature 'Modder creates a new mod' do
scenario 'user submits a mod with invalid names in the #authors_list' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'SuperMod'
select 'Terrain', from: 'Categories'
fill_in_minimum
fill_in 'mod_authors_list', with: 'Potato(), SuperUser, Salad'
fill_in_first_version_and_file
submit_form
expect(current_path).to eq '/mods'
expect(page).to have_css '#mod_authors_list_input .inline-errors'
@ -265,10 +254,8 @@ feature 'Modder creates a new mod' do
scenario 'user submits a mod too many authors in the #authors_list' do
sign_in_dev
visit '/mods/new'
fill_in 'mod_name', with: 'SuperMod'
select 'Terrain', from: 'Categories'
fill_in_minimum
fill_in 'mod_authors_list', with: 'Potato, SuperUser, Salad, Tururu, Papapa, Aaaaa, Bbbbb, Ccccc, Ddddd'
fill_in_first_version_and_file
submit_form
expect(current_path).to eq '/mods'
expect(page).to have_css '#mod_authors_list_input .inline-errors'
@ -355,12 +342,17 @@ feature 'Modder creates a new mod' do
# end
end
def fill_in_minimum
fill_in 'mod_name', with: 'SuperMod'
select 'Terrain', from: 'Categories'
def fill_in_minimum(*name)
fill_in_no_versions_minimum(*name)
fill_in_first_version_and_file
end
def fill_in_no_versions_minimum(name = 'SuperMod')
fill_in 'mod_name', with: name
fill_in 'mod_info_json_name', with: name.downcase
select 'Terrain', from: 'Categories'
end
def fill_in_first_version_and_file
attachment = File.new(Rails.root.join('spec', 'fixtures', 'test.zip'))
within('.mod-version:nth-child(1)') do

View File

@ -7,25 +7,26 @@ feature 'The goddamn game version string should be updated after editing a mod'
string is the correct one, but then you mods#edit the mod and you reduce the Factorio
versions of the mod_version that is compatible with, then you look at mods#show again and
the string should be updated' do
cat1 = create :category, name: 'CategoryOne'
create :category, name: 'CategoryOne'
gv1 = create :game_version, number: '0.10', sort_order: 1
gv2 = create :game_version, number: '0.11', sort_order: 2
sign_in_admin
visit '/mods/new'
nfind('mod[name]').set 'This Bug, Man'
nfind(:select, 'mod[category_ids][]').select 'CategoryOne'
nfind('mod[authors_list]').set 'Whatever'
nfind('mod[versions_attributes][0][number]').set '1.2.3'
nfind('mod[versions_attributes][0][released_at]').set '2015-01-01'
nfind(:select, 'mod[versions_attributes][0][game_version_ids][]').select '0.10'
nfind(:select, 'mod[versions_attributes][0][game_version_ids][]').select '0.11'
nfind('mod[versions_attributes][0][files_attributes][0][download_url]').set 'http://potato.com'
nfind('commit').click()
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_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'
select '0.11', from: 'mod_versions_attributes_0_game_version_ids'
fill_in 'mod_versions_attributes_0_files_attributes_0_download_url', with: 'http://potato.com'
find('#mod_submit_action input').click()
expect(current_path).to eq '/mods/this-bug-man'
expect(page).to have_content '0.10-0.11'
click_link 'Edit mod'
nfind(:select, 'mod[versions_attributes][0][game_version_ids][]').unselect '0.10'
nfind('commit').click()
unselect '0.10', from: 'mod_versions_attributes_0_game_version_ids'
find('#mod_submit_action input').click()
expect(ModGameVersion.all.size).to eq 1
expect(Mod.first.versions.first.game_versions).to match_array [gv2]
expect(Mod.first.game_versions_string).to eq '0.11'

View File

@ -152,6 +152,11 @@ RSpec.describe Mod, :type => :model do
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
end
end
describe '#author_name' do
@ -476,6 +481,32 @@ RSpec.describe Mod, :type => :model do
end
describe 'scopes' do
describe '.filter_by_names' do
it 'should return a list of mods by #info_json_name' do
mods = []
mods.push create :mod, info_json_name: 'potato'
create :mod, info_json_name: 'potato-2'
mods.push create :mod, info_json_name: 'banana-stream'
create :mod, info_json_name: 'i love this keyboard'
mods.push create :mod, info_json_name: 'Atom by github rocks'
found = Mod.filter_by_names 'potato,banana-stream, Atom by github rocks'
expect(found).to match_array mods
end
it 'should be case sensitive' do
create :mod, info_json_name: 'potato'
mod = create :mod, info_json_name: 'PoTaTo'
expect(Mod.filter_by_names('PoTaTo')).to match_array [mod]
end
it 'should return all the mods with the same #info_json_name' do
mod1 = create :mod, info_json_name: 'potato'
mod2 = create :mod, info_json_name: 'potato'
create :mod, info_json_name: 'banana'
expect(Mod.filter_by_names('potato')).to match_array [mod1, mod2]
end
end
describe '.filter_by_category' do
it 'should filter results by category' do
mod1 = create(:mod)
@ -709,4 +740,99 @@ RSpec.describe Mod, :type => :model do
end
end
end
describe '#as_json' do
it 'should return the public API structure' do
authors = [create(:user, name: 'John Snow Zombie'), create(:user, name: 'THAT Guy')]
mod = create :mod,
name: 'Potato Galaxy',
info_json_name: 'potato-galaxy-mod',
authors: authors,
contact: 'Send a homing pigeon to Castle Black',
official_url: 'http://castleblack.com',
summary: 'This mod adds the ability to farm potatoes on Factorio.'
gv1 = create :game_version, number: '0.10.x'
gv2 = create :game_version, number: '0.11.x'
gv3 = create :game_version, number: '0.12.x'
one_month_ago = 1.month.ago
two_weeks_ago = 2.weeks.ago
one_week_ago = 1.week.ago
create :mod_version,
number: '1.2.1',
released_at: one_month_ago,
game_versions: [gv1],
mod: mod,
files: [
build(:mod_file, name: '', download_url: 'http://thepotatoexperience.com/1.2.1', attachment: nil)
]
create :mod_version,
number: '1.2.2',
released_at: two_weeks_ago,
game_versions: [gv2, gv3],
mod: mod,
files: [
build(:mod_file, name: 'win', download_url: 'http://thepotatoexperience.com/1.2.2', attachment: nil),
build(:mod_file, name: 'mac', download_url: 'http://thepotatoexperience.com/1.2.2', attachment: nil)
]
mv3 = create :mod_version,
number: '1.2.3',
released_at: one_week_ago,
game_versions: [gv3],
mod: mod,
files: [
build(:mod_file,
name: '',
download_url: 'http://thepotatoexperience.com/1.2.3',
attachment: File.new(Rails.root.join('spec', 'fixtures', 'test.zip'))
)
]
expect(mod.as_json).to eq({
title: 'Potato Galaxy',
name: 'potato-galaxy-mod',
url: 'http://localhost:3000/mods/potato-galaxy',
description: 'This mod adds the ability to farm potatoes on Factorio.',
homepage: 'http://castleblack.com',
contact: 'Send a homing pigeon to Castle Black',
authors: ['John Snow Zombie', 'THAT Guy'],
releases: [
{
version: '1.2.3',
released_at: one_week_ago,
game_versions: ['0.12.x'],
dependencies: [],
files: [
{
name: '',
url: 'http://thepotatoexperience.com/1.2.3',
mirror: mv3.files.first.attachment.url
}
]
},
{
version: '1.2.2',
released_at: two_weeks_ago,
game_versions: ['0.11.x', '0.12.x'],
dependencies: [],
files: [
{ name: 'mac', url: 'http://thepotatoexperience.com/1.2.2', mirror: '' },
{ name: 'win', url: 'http://thepotatoexperience.com/1.2.2', mirror: '' }
]
},
{
version: '1.2.1',
released_at: one_month_ago,
game_versions: ['0.10.x'],
dependencies: [],
files: [
{ name: '', url: 'http://thepotatoexperience.com/1.2.1', mirror: '' }
]
}
]
})
end
end
end