Skip to content
Extraits de code Groupes Projets
Valider 3c57de4a rédigé par Sybil's avatar Sybil
Parcourir les fichiers

O.K. : Tracks contain every useful data.

 - Tags weren't displayed. Solution : composite_primary_keys gem to manage the shi*** database design.
 - Json unreadable. Solution : active_model_serializers to select json fields.
 - SQL requests too slow. Solution : includes in place of joins (3x faster).
 PS : TY Zar.
parent 50b0b0f6
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -16,11 +16,18 @@ gem 'rails', '4.1.5' ...@@ -16,11 +16,18 @@ gem 'rails', '4.1.5'
#Gem to produce a lightweight json API #Gem to produce a lightweight json API
gem 'rails-api' gem 'rails-api'
#Authorize API usage from different domains
gem 'rack-cors'
#Add Serializer class to choose json fields
gem 'active_model_serializers'
#Because we're using a shitty database designed by a fuckin' drug addict
gem 'composite_primary_keys'
# Use jquery as the JavaScript library # Use jquery as the JavaScript library
#gem 'jquery-rails' #gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks' # gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0' gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api. # bundle exec rake doc:rails generates the API under doc/api.
...@@ -36,7 +43,7 @@ gem 'mysql2' ...@@ -36,7 +43,7 @@ gem 'mysql2'
#gem 'soundcloud' #gem 'soundcloud'
#Gem for pagination #Gem for pagination
#gem 'kaminari' gem 'kaminari'
#Gem for Ruby web server #Gem for Ruby web server
gem 'thin' gem 'thin'
......
...@@ -14,6 +14,8 @@ GEM ...@@ -14,6 +14,8 @@ GEM
activesupport (= 4.1.5) activesupport (= 4.1.5)
builder (~> 3.1) builder (~> 3.1)
erubis (~> 2.7.0) erubis (~> 2.7.0)
active_model_serializers (0.9.2)
activemodel (>= 3.2)
activemodel (4.1.5) activemodel (4.1.5)
activesupport (= 4.1.5) activesupport (= 4.1.5)
builder (~> 3.1) builder (~> 3.1)
...@@ -30,23 +32,20 @@ GEM ...@@ -30,23 +32,20 @@ GEM
arel (5.0.1.20140414130214) arel (5.0.1.20140414130214)
builder (3.2.2) builder (3.2.2)
coderay (1.1.0) coderay (1.1.0)
coffee-rails (4.0.1) composite_primary_keys (7.0.10)
coffee-script (>= 2.2.0) activerecord (~> 4.1.4)
railties (>= 4.0.0, < 5.0)
coffee-script (2.3.0)
coffee-script-source
execjs
coffee-script-source (1.7.1)
daemons (1.1.9) daemons (1.1.9)
erubis (2.7.0) erubis (2.7.0)
eventmachine (1.0.3) eventmachine (1.0.3)
execjs (2.2.1)
hike (1.2.3) hike (1.2.3)
i18n (0.6.11) i18n (0.6.11)
jbuilder (2.1.3) jbuilder (2.1.3)
activesupport (>= 3.0.0, < 5) activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2) multi_json (~> 1.2)
json (1.8.1) json (1.8.1)
kaminari (0.16.1)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
mail (2.5.4) mail (2.5.4)
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) treetop (~> 1.4.8)
...@@ -61,6 +60,7 @@ GEM ...@@ -61,6 +60,7 @@ GEM
method_source (~> 0.8.1) method_source (~> 0.8.1)
slop (~> 3.4) slop (~> 3.4)
rack (1.5.2) rack (1.5.2)
rack-cors (0.2.9)
rack-test (0.6.2) rack-test (0.6.2)
rack (>= 1.0) rack (>= 1.0)
rails (4.1.5) rails (4.1.5)
...@@ -108,8 +108,6 @@ GEM ...@@ -108,8 +108,6 @@ GEM
treetop (1.4.15) treetop (1.4.15)
polyglot polyglot
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
turbolinks (2.3.0)
coffee-rails
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
...@@ -117,12 +115,15 @@ PLATFORMS ...@@ -117,12 +115,15 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
active_model_serializers
composite_primary_keys
jbuilder (~> 2.0) jbuilder (~> 2.0)
kaminari
mysql2 mysql2
pry pry
rack-cors
rails (= 4.1.5) rails (= 4.1.5)
rails-api rails-api
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
spring spring
thin thin
turbolinks
class ApplicationController < ActionController::API class ApplicationController < ActionController::API
# Prevent CSRF attacks by raising an exception. include ActionController::Serialization
# For APIs, you may want to use :null_session instead.
before_filter :get_tags, :get_users, :get_channels before_filter :get_tags, :get_users, :get_channels
......
...@@ -10,12 +10,12 @@ class ChannelsController < ApplicationController ...@@ -10,12 +10,12 @@ class ChannelsController < ApplicationController
end end
def show_user def show_user
@musics = Music.joins(:channel).where(playbot_chan: {sender_irc: params[:user]}) @tracks = Track.joins(:channel).where(playbot_chan: {sender_irc: params[:user]})
render json: @user, status: 200 render json: @user, status: 200
end end
def show_channel def show_channel
@musics = Music.joins(:channel).where(playbot_chan: {chan: "#"+params[:channel]}) @tracks = Track.joins(:channel).where(playbot_chan: {chan: "#"+params[:channel]})
render json: @channel, status:200 render json: @channel, status:200
end end
......
...@@ -5,7 +5,7 @@ class TagsController < ApplicationController ...@@ -5,7 +5,7 @@ class TagsController < ApplicationController
end end
def show def show
@musics = Music.joins(:tags).where(playbot_tags: {tag: params[:tag]}) @tracks = Track.joins(:tags).where(playbot_tags: {tag: params[:tag]})
render json: @tag, status: 200 render json: @tag, status: 200
end end
end end
class MusicsController < ApplicationController class TracksController < ApplicationController
def filters( *filters ) def filters( *filters )
filters.each do | filter | filters.each do | filter |
if param = params[filter] || params["#{filter}_id"] if param = params[filter] || params["#{filter}_id"]
@musics = @musics.send( "with_#{filter}", param ) @tracks = @tracks.send( "with_#{filter}", param )
end end
end end
end end
def index def index
@musics = Music.all @tracks = Track.includes(:channel, :tags)
filters :tag, :channel, :user filters :tag, :channel, :user
render json: @tracks, status: 200
render json: @musics, status: 200
end end
def show def show
@music = Music.find(params[:id]) @track = Track.find(params[:id])
render json: @music, status: 200 render json: @track, status: 200
end end
end end
class Channel < ActiveRecord::Base class Channel < ActiveRecord::Base
self.table_name = "playbot_chan" self.table_name = "playbot_chan"
belongs_to :music belongs_to :track
def self.with_channel(channel) def self.with_channel(channel)
where("chan = ?", channel) where("chan = ?", channel)
......
class Tag < ActiveRecord::Base class Tag < ActiveRecord::Base
self.table_name = "playbot_tags" self.table_name = :playbot_tags
belongs_to :music self.primary_keys = :id, :tag
belongs_to :track
end end
class Music < ActiveRecord::Base class Track < ActiveRecord::Base
self.table_name = "playbot" self.table_name = :playbot
self.inheritance_column = "inheritance_type" self.inheritance_column = :inheritance_type
has_many :tags, foreign_key: "id" has_many :tags, primary_key: :id, foreign_key: :id
has_one :channel, primary_key: "id", foreign_key: "content" has_one :channel, primary_key: :id, foreign_key: :content
def self.with_tag(tag) def self.with_tag(tag)
self.joins(:tags).where("tag = ?", tag ) self.joins(:tags).where("tag = ?", tag )
#self.includes(:tags)
#self.where("tag = ?", tag)
end end
def self.with_channel(channel) def self.with_channel(channel)
......
class ChannelSerializer < ActiveModel::Serializer
attributes :sender_irc, :chan
end
class TagSerializer < ActiveModel::Serializer
attributes :tag
#belongs_to :track
end
class TrackSerializer < ActiveModel::Serializer
attributes :id, :title, :url, :type
has_one :channel
has_many :tags
end
...@@ -24,5 +24,13 @@ module WebPlayBot ...@@ -24,5 +24,13 @@ module WebPlayBot
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de # config.i18n.default_locale = :de
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end end
end end
Rails.application.routes.draw do Rails.application.routes.draw do
constraints subdomain: 'api' do
get '/users', to: "channels#index_users" get '/users', to: "channels#index_users"
concern :tracks do concern :tracks do
resources :musics, only: [:index] resources :tracks, only: [:index]
end end
resources :tags, concerns: :tracks, only: [:index, :show] resources :tags, concerns: :tracks, only: [:index, :show]
resources :channels, concerns: :tracks, only: [:index, :show] resources :channels, concerns: :tracks, only: [:index, :show]
resources :users, concerns: :tracks, only: [:index, :show] resources :users, concerns: :tracks, only: [:index, :show]
resources :tracks, only: [:index, :show]
root to: 'musics#index' root to: 'tracks#index'
end
end end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter