From 0bbdc1b979af52b7312cfd7fb58525358449390f Mon Sep 17 00:00:00 2001
From: Sybil <sybil.deboin@gmail.com>
Date: Mon, 23 Feb 2015 05:11:12 +0100
Subject: [PATCH] - Seeds compute the quantity of tracks per user ... - Order
 user, tag, channel by their size.

---
 app/controllers/channels_controller.rb |  1 +
 app/controllers/tags_controller.rb     |  1 +
 app/controllers/tracks_controller.rb   | 10 ++++++++--
 app/controllers/users_controller.rb    |  8 ++++++++
 app/models/track.rb                    |  2 +-
 app/serializers/channel_serializer.rb  |  2 +-
 app/serializers/tag_serializer.rb      |  2 +-
 app/serializers/track_serializer.rb    |  8 ++++----
 app/serializers/user_serializer.rb     |  2 +-
 config/routes.rb                       |  6 +++---
 db/seeds.rb                            |  3 +++
 11 files changed, 32 insertions(+), 13 deletions(-)
 create mode 100644 app/controllers/users_controller.rb

diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb
index 71ad191..727a98d 100644
--- a/app/controllers/channels_controller.rb
+++ b/app/controllers/channels_controller.rb
@@ -1,6 +1,7 @@
 class ChannelsController < ApplicationController
 
   def index
+    @channels = Channel.all.order(quantity: :desc)
     render json: @channels, status: 200
   end
 end
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 68bf520..a01b30e 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -1,6 +1,7 @@
 class TagsController < ApplicationController
 
   def index
+    @tags = Tag.all.order(quantity: :desc)
     render json: @tags, status: 200
   end
 
diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb
index be966f0..f592f42 100644
--- a/app/controllers/tracks_controller.rb
+++ b/app/controllers/tracks_controller.rb
@@ -11,9 +11,15 @@ class TracksController < ApplicationController
     #@tracks = Track.includes(irc_posts: [:channel, :user], tag_assignations: :tag).all #includes(irc_posts: [{ :channels, :users}]) #includes(:channels,:tags,:users)
     #t = Track.all.joins('inner join irc_posts i ON tracks.track_id = i.track_id inner join channels c on i.channel_id = c.channel_id inner join gnations ti on tracks.track_id = ti.track_id inner join tags ta on ta.tag_id = ti.tag_id').select('tracks.name as t_name, u.name as u_name, c.name as c_name, ta.name as t_name')
     #t.first.c_name
-    @tracks = Track.page params[:page]
+    @tracks = Track.includes(:tags).page params[:page]
     filters :tag, :channel, :user
-    render json: @tracks, status: 200
+    render json: @tracks, 
+      status: 200, 
+      meta: {
+        current_page: @tracks.current_page,
+        per_page: @tracks.default_per_page,
+        total_pages: @tracks.num_pages
+      } 
   end
 
   def show 
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
new file mode 100644
index 0000000..475d710
--- /dev/null
+++ b/app/controllers/users_controller.rb
@@ -0,0 +1,8 @@
+class UsersController < ApplicationController
+
+  def index
+    @users = User.all.order(quantity: :desc)
+    render json: @users, status: 200
+  end
+
+end
diff --git a/app/models/track.rb b/app/models/track.rb
index da2a931..36db292 100644
--- a/app/models/track.rb
+++ b/app/models/track.rb
@@ -1,5 +1,5 @@
 class Track < ActiveRecord::Base
-  paginates_per 50  
+  paginates_per 51  
   #default_scope includes(:channels,:users,:tags)
 
   has_many :irc_posts
diff --git a/app/serializers/channel_serializer.rb b/app/serializers/channel_serializer.rb
index 3eb7ae6..8a115a5 100644
--- a/app/serializers/channel_serializer.rb
+++ b/app/serializers/channel_serializer.rb
@@ -1,5 +1,5 @@
 class ChannelSerializer < ActiveModel::Serializer
-  attributes :id, :name
+  attributes :id, :name, :quantity
 
   #has_many :tracks
   #has_many :users
diff --git a/app/serializers/tag_serializer.rb b/app/serializers/tag_serializer.rb
index 7589b05..6dfba5a 100644
--- a/app/serializers/tag_serializer.rb
+++ b/app/serializers/tag_serializer.rb
@@ -1,5 +1,5 @@
 class TagSerializer < ActiveModel::Serializer
-  attributes :id, :name
+  attributes :id, :name, :quantity
 
   #belongs_to :track
 end
diff --git a/app/serializers/track_serializer.rb b/app/serializers/track_serializer.rb
index 91b9647..a15b86e 100644
--- a/app/serializers/track_serializer.rb
+++ b/app/serializers/track_serializer.rb
@@ -1,10 +1,10 @@
 class TrackSerializer < ActiveModel::Serializer
   attributes :id, :name, :url, :provider, :author, :channel
 
-  embed :ids, include: true
+  #embed :ids, include: true
 
-  has_many :channels
-  has_many :tags
-  has_many :users
+  #has_many :channels
+  has_many :tags, embed: :ids, include: true
+  #has_many :users
 
 end
diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb
index 2ec17e8..0c38406 100644
--- a/app/serializers/user_serializer.rb
+++ b/app/serializers/user_serializer.rb
@@ -1,5 +1,5 @@
 class UserSerializer < ActiveModel::Serializer
-  attributes :id, :name
+  attributes :id, :name, :quantity
 
   #belongs_to :track
 end
diff --git a/config/routes.rb b/config/routes.rb
index e3d8c4a..0febdc2 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -5,9 +5,9 @@ Rails.application.routes.draw do
   concern :tracks do
     resources :tracks, only: [:index]
   end
-  #resources :tags, concerns: :tracks, only: [:index, :show]
-  #resources :channels, concerns: :tracks, only: [:index, :show]
-  #resources :users, concerns: :tracks, only: [:index, :show]
+  resources :tags, concerns: :tracks, only: [:index, :show]
+  resources :channels, concerns: :tracks, only: [:index, :show]
+  resources :users, concerns: :tracks, only: [:index, :show]
   resources :tracks, only: [:index, :show]
 
   root to: 'tracks#index'
diff --git a/db/seeds.rb b/db/seeds.rb
index 57a2555..4e9f9ac 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -15,13 +15,16 @@ OldDatabase::PlaybotChan.find_each do |p_chan|
   end
 
   user = User.find_or_create_by(name: p_chan.sender_irc)
+  user.increment!(:quantity)
   channel = Channel.find_or_create_by(name: p_chan.chan)
+  channel.increment!(:quantity)
 
   IrcPost.create(track_id: track.id, channel_id: channel.id, user_id: user.id, posted_at: p_chan.date)
 end
 
 OldDatabase::PlaybotTag.all.each do |p_tag|
   tag = Tag.find_or_create_by(name: p_tag.tag)
+  tag.increment!(:quantity)
   TagAssignation.create(track_id: p_tag.id, tag_id: tag.id)
 end
 
-- 
GitLab