mirror of
https://github.com/ENSL/ensl.org.git
synced 2024-11-15 01:11:23 +00:00
Merge branch 'refs/heads/develop' into bug-article-text
This commit is contained in:
commit
3b9c807594
14 changed files with 180 additions and 3 deletions
1
Gemfile
1
Gemfile
|
@ -5,6 +5,7 @@ ruby '2.1.1'
|
||||||
gem 'dotenv-rails', '~> 0.10.0'
|
gem 'dotenv-rails', '~> 0.10.0'
|
||||||
gem 'rails', '~> 3.2.17'
|
gem 'rails', '~> 3.2.17'
|
||||||
gem 'mysql2', '~> 0.3.15'
|
gem 'mysql2', '~> 0.3.15'
|
||||||
|
gem 'oj', '~> 2.5.5'
|
||||||
|
|
||||||
# Libraries
|
# Libraries
|
||||||
gem 'jquery-rails', '~> 2.0.2'
|
gem 'jquery-rails', '~> 2.0.2'
|
||||||
|
|
|
@ -127,6 +127,7 @@ GEM
|
||||||
mini_portile (~> 0.5.0)
|
mini_portile (~> 0.5.0)
|
||||||
nokogiri (1.6.1-x86-mingw32)
|
nokogiri (1.6.1-x86-mingw32)
|
||||||
mini_portile (~> 0.5.0)
|
mini_portile (~> 0.5.0)
|
||||||
|
oj (2.5.5)
|
||||||
poltergeist (1.5.0)
|
poltergeist (1.5.0)
|
||||||
capybara (~> 2.1)
|
capybara (~> 2.1)
|
||||||
cliver (~> 0.3.1)
|
cliver (~> 0.3.1)
|
||||||
|
@ -267,6 +268,7 @@ DEPENDENCIES
|
||||||
mysql2 (~> 0.3.15)
|
mysql2 (~> 0.3.15)
|
||||||
newrelic_rpm (~> 3.7.2.195)
|
newrelic_rpm (~> 3.7.2.195)
|
||||||
nokogiri (~> 1.6.1)
|
nokogiri (~> 1.6.1)
|
||||||
|
oj (~> 2.5.5)
|
||||||
poltergeist (~> 1.5.0)
|
poltergeist (~> 1.5.0)
|
||||||
pry-debugger (~> 0.2.2)
|
pry-debugger (~> 0.2.2)
|
||||||
rails (~> 3.2.17)
|
rails (~> 3.2.17)
|
||||||
|
|
3
app/controllers/api/v1/base_controller.rb
Normal file
3
app/controllers/api/v1/base_controller.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class Api::V1::BaseController < ActionController::Base
|
||||||
|
respond_to :json
|
||||||
|
end
|
5
app/controllers/api/v1/users_controller.rb
Normal file
5
app/controllers/api/v1/users_controller.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class Api::V1::UsersController < Api::V1::BaseController
|
||||||
|
def index
|
||||||
|
respond_with Api::V1::UsersCollection.as_json
|
||||||
|
end
|
||||||
|
end
|
5
app/services/api/v1/collection.rb
Normal file
5
app/services/api/v1/collection.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class Api::V1::Collection
|
||||||
|
def execute_query
|
||||||
|
ActiveRecord::Base.connection.execute(arel_query.to_sql)
|
||||||
|
end
|
||||||
|
end
|
58
app/services/api/v1/users_collection.rb
Normal file
58
app/services/api/v1/users_collection.rb
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
class Api::V1::UsersCollection < Api::V1::Collection
|
||||||
|
def self.as_json
|
||||||
|
self.new.data.to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
def data
|
||||||
|
{ users: map_query }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def users_table
|
||||||
|
User.arel_table
|
||||||
|
end
|
||||||
|
|
||||||
|
def teams_table
|
||||||
|
Team.arel_table
|
||||||
|
end
|
||||||
|
|
||||||
|
def joins
|
||||||
|
[
|
||||||
|
users_table[:team_id].eq(teams_table[:id])
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def columns
|
||||||
|
[
|
||||||
|
users_table[:username],
|
||||||
|
users_table[:steamid],
|
||||||
|
teams_table[:name],
|
||||||
|
teams_table[:tag],
|
||||||
|
teams_table[:logo]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def arel_query
|
||||||
|
users_table
|
||||||
|
.project(columns)
|
||||||
|
.join(teams_table, Arel::Nodes::OuterJoin)
|
||||||
|
.on(joins)
|
||||||
|
.where(users_table[:team_id].not_eq(nil))
|
||||||
|
.order(users_table[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def map_query
|
||||||
|
execute_query.map do |row|
|
||||||
|
{
|
||||||
|
username: row[0],
|
||||||
|
steamid: row[1],
|
||||||
|
team: {
|
||||||
|
name: row[2],
|
||||||
|
tag: row[3],
|
||||||
|
logo: row[4]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@ module Ensl
|
||||||
# -- all .rb files in that directory are automatically loaded.
|
# -- all .rb files in that directory are automatically loaded.
|
||||||
|
|
||||||
# Custom directories with classes and modules you want to be autoloadable.
|
# Custom directories with classes and modules you want to be autoloadable.
|
||||||
# config.autoload_paths += %W(#{config.root}/extras)
|
config.autoload_paths += Dir["#{config.root}/app/services/**/"]
|
||||||
|
|
||||||
# Only load the plugins named here, in the order given (default is alphabetical).
|
# Only load the plugins named here, in the order given (default is alphabetical).
|
||||||
# :all can be used as a placeholder for all plugins nowt explicitly named.
|
# :all can be used as a placeholder for all plugins nowt explicitly named.
|
||||||
|
|
|
@ -115,4 +115,10 @@ Ensl::Application.routes.draw do
|
||||||
match ':controller/:action/:id'
|
match ':controller/:action/:id'
|
||||||
match ':controller/:action/:id.:format'
|
match ':controller/:action/:id.:format'
|
||||||
match ':controller/:action/:id/:id2'
|
match ':controller/:action/:id/:id2'
|
||||||
|
|
||||||
|
namespace :api do
|
||||||
|
namespace :v1 do
|
||||||
|
resources :users
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
36
spec/controllers/api/v1/users_controller_spec.rb
Normal file
36
spec/controllers/api/v1/users_controller_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Api::V1::UsersController do
|
||||||
|
before do
|
||||||
|
request.accept = 'application/json'
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#index' do
|
||||||
|
before do
|
||||||
|
10.times { create(:user_with_team) }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns all users and associated teams' do
|
||||||
|
users = User.all
|
||||||
|
|
||||||
|
get :index
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
expect(json["users"].size).to eq(users.size)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns the excpected JSON keys' do
|
||||||
|
get :index
|
||||||
|
|
||||||
|
user_json = json["users"].first
|
||||||
|
nested_team_json = user_json["team"]
|
||||||
|
|
||||||
|
expect(user_json).to have_key("username")
|
||||||
|
expect(user_json).to have_key("steamid")
|
||||||
|
expect(user_json).to have_key("team")
|
||||||
|
expect(nested_team_json).to have_key("name")
|
||||||
|
expect(nested_team_json).to have_key("tag")
|
||||||
|
expect(nested_team_json).to have_key("logo")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
14
spec/factories/team.rb
Normal file
14
spec/factories/team.rb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
FactoryGirl.define do
|
||||||
|
sequence :name do |n|
|
||||||
|
"Team ##{n}"
|
||||||
|
end
|
||||||
|
|
||||||
|
factory :team do
|
||||||
|
name
|
||||||
|
irc "#team"
|
||||||
|
web "http://team.com"
|
||||||
|
tag "[TEAM]"
|
||||||
|
country "EU"
|
||||||
|
comment "We are a team"
|
||||||
|
end
|
||||||
|
end
|
|
@ -7,13 +7,23 @@ FactoryGirl.define do
|
||||||
"player#{n}@ensl.org"
|
"player#{n}@ensl.org"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sequence :steamid do |n|
|
||||||
|
"0:1:#{n}"
|
||||||
|
end
|
||||||
|
|
||||||
factory :user do
|
factory :user do
|
||||||
username
|
username
|
||||||
email
|
email
|
||||||
|
steamid
|
||||||
firstname "ENSL"
|
firstname "ENSL"
|
||||||
lastname "Player"
|
lastname "Player"
|
||||||
steamid "0:1:23456789"
|
|
||||||
country "EU"
|
country "EU"
|
||||||
raw_password "PasswordABC123"
|
raw_password "PasswordABC123"
|
||||||
|
|
||||||
|
factory :user_with_team do
|
||||||
|
after(:create) do |user|
|
||||||
|
create(:team, founder: user)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
29
spec/services/api/v1/users_collection_spec.rb
Normal file
29
spec/services/api/v1/users_collection_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
describe Api::V1::UsersCollection do
|
||||||
|
let(:collection) { Api::V1::UsersCollection.new }
|
||||||
|
|
||||||
|
describe '#execute_query' do
|
||||||
|
describe 'when there are users with no teams' do
|
||||||
|
before do
|
||||||
|
3.times { create(:user) }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns 0 results' do
|
||||||
|
expect(collection.execute_query.size).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when there are some users with teams' do
|
||||||
|
before do
|
||||||
|
3.times { create(:user_with_team) }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns 3 results' do
|
||||||
|
expect(collection.execute_query.size).to eq(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns 5 columns' do
|
||||||
|
expect(collection.execute_query.first.size).to eq(5)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -16,6 +16,7 @@ Capybara.javascript_driver = :poltergeist
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.include FactoryGirl::Syntax::Methods
|
config.include FactoryGirl::Syntax::Methods
|
||||||
|
config.include Controllers::JsonHelpers, :type => :controller
|
||||||
|
|
||||||
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
||||||
config.use_transactional_fixtures = true
|
config.use_transactional_fixtures = true
|
||||||
|
|
7
spec/support/mixins/controllers.rb
Normal file
7
spec/support/mixins/controllers.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module Controllers
|
||||||
|
module JsonHelpers
|
||||||
|
def json
|
||||||
|
@json ||= JSON.parse(response.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue