From bbefc45b866defdc54d3973a81cc33080f2e013a Mon Sep 17 00:00:00 2001 From: "Hiren.Bhalani-BTC" Date: Fri, 15 May 2015 12:47:31 +0530 Subject: [PATCH 1/9] Add facility to sort links by added_on, updated_on, learn_count, recently_learned, refactor code in chart_controller. --- app/controllers/charts_controller.rb | 65 +++++++++++++++------------- app/controllers/links_controller.rb | 16 +++++++ app/helpers/application_helper.rb | 2 +- app/models/learn_time.rb | 5 +-- app/models/link.rb | 6 +-- app/models/user.rb | 12 +++-- app/views/layouts/_header.html.erb | 6 +++ app/views/links/sort_links.js.erb | 1 + config/routes.rb | 8 ++-- 9 files changed, 77 insertions(+), 44 deletions(-) create mode 100644 app/views/links/sort_links.js.erb diff --git a/app/controllers/charts_controller.rb b/app/controllers/charts_controller.rb index 8e281fb..10468ea 100644 --- a/app/controllers/charts_controller.rb +++ b/app/controllers/charts_controller.rb @@ -1,38 +1,12 @@ class ChartsController < ApplicationController before_filter :get_date + before_filter :chart_options # before_filter :google_chart_init def chart_by_learn - data_table = GoogleVisualr::DataTable.new - data_table.new_column('date', 'Day') - data_table.new_column('number', 'Learn Count') - data_table.new_column('number', 'Add Count') - - @chart_by_learn = LearnTime.report_of_learn_time(current_user.id, @date) - @total_links = User.get_all_link(current_user, @date) - - all = ((Date.today-30)..Date.today).inject([]) do |all, date | - learn_count = @chart_by_learn[date] || 0 - link_count = @total_links[date] || 0 - - all << [date, learn_count, link_count ] - end - - data_table.add_rows(all) - learn_opts = { :width => 450, :height => 400, :title => '', :legend => 'bottom' } - @chart = GoogleVisualr::Interactive::LineChart.new(data_table, learn_opts) - - tags_question_data = GoogleVisualr::DataTable.new - tags_question_data.new_column('string', 'Tag') - tags_question_data.new_column('number', 'Tag Count') - - @tags = ActsAsTaggableOn::Tag.all - tags_usage_data = @tags.map{|tag|[ tag.name, Link.where(:user_id => current_user.id).tagged_with(tag).count]} - tags_question_data.add_rows(tags_usage_data.sort {|a,b| a[1] <=> b[1]}.reverse.first(10)) - tag_opts = { :width => 450, :height => 400, :title => '', :legend => 'bottom' } - @tag_chart = GoogleVisualr::Interactive::PieChart.new(tags_question_data, tag_opts) - + @chart = learn_and_add_report_chart + @tag_chart = tag_usage_report_chart end private @@ -43,4 +17,37 @@ def get_date @date = start_date - @days end + def google_visular_init_table(table_fields) + data_table = GoogleVisualr::DataTable.new + table_fields.each do |field| + data_table.new_column(field[0], field[1]) + end + data_table + end + + def learn_and_add_report_chart + data_table = google_visular_init_table([ ['date', 'Day'], ['number', 'Learn Count'], ['number', 'Add Count'] ]) + @chart_by_learn = current_user.user_learn_count_till(@date) + @total_links = current_user.links_till(@date) + all = ((Date.today-30)..Date.today).inject([]) do |all, date | + learn_count = @chart_by_learn[date] || 0 + link_count = @total_links[date] || 0 + all << [date, learn_count, link_count ] + end + data_table.add_rows(all) + GoogleVisualr::Interactive::LineChart.new(data_table, @chart_options) + end + + def tag_usage_report_chart + tags_question_data = google_visular_init_table([['string', 'Tag'], ['number', 'Tag Count']]) + @tags = ActsAsTaggableOn::Tag.all + tags_usage_data = @tags.map{|tag|[ tag.name, current_user.links.tagged_with(tag).count]} + tags_question_data.add_rows(tags_usage_data.sort {|a,b| a[1] <=> b[1]}.reverse.first(10)) + GoogleVisualr::Interactive::PieChart.new(tags_question_data, @chart_options) + end + + def chart_options + @chart_options = { :width => 450, :height => 400, :title => '', :legend => 'bottom' } + end + end diff --git a/app/controllers/links_controller.rb b/app/controllers/links_controller.rb index 2f9d1a3..77ac03a 100644 --- a/app/controllers/links_controller.rb +++ b/app/controllers/links_controller.rb @@ -20,6 +20,22 @@ def index end end + def sort_links + case + when params[:added_on] + @links = current_user_links.order_by_created_at + when params[:updated_on] + @links = current_user_links.order_by_updated_at + when params[:recently_learned] + @links = current_user.user_learned_links + when params[:learn_count] + link_ids = current_user.learn_time.group_by(&:link_id).map {|link| [link.first, link.last.count] }.sort_by(&:last).reverse.map{|link| link.first} + @links = Link.find(link_ids).index_by(&:id).values_at(*link_ids) + end + end + + + def new @link = Link.new end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 36ec8e1..9b9be8b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,6 @@ module ApplicationHelper def links_count - current_user.current_user_links.count + current_user.links.count end def favourite_links_count diff --git a/app/models/learn_time.rb b/app/models/learn_time.rb index 9f09315..d770bf2 100644 --- a/app/models/learn_time.rb +++ b/app/models/learn_time.rb @@ -2,8 +2,5 @@ class LearnTime < ActiveRecord::Base belongs_to :user belongs_to :link - - def self.report_of_learn_time(user_id, date) - LearnTime.where("user_id = ? and created_at >= ?", user_id, date ).group('date(created_at)').count - end + end diff --git a/app/models/link.rb b/app/models/link.rb index 5ac94a4..10d139b 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -11,9 +11,9 @@ class Link < ActiveRecord::Base belongs_to :learning_status belongs_to :link_type - def self.learn_time(user) - LearnTime.create!(user_id: user.id, link_id: self.id) - end + scope :order_by_created_at, -> { order(:created_at => :asc) } + + scope :order_by_updated_at, -> { order(:updated_at => :asc) } def create_favourite(user_id, link_id) favourites.create!(user_id: user_id, link_id: link_id) diff --git a/app/models/user.rb b/app/models/user.rb index 4d3b859..9502fd4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,15 +11,19 @@ class User < ActiveRecord::Base has_many :links has_many :learn_time - def self.get_all_link(user, date) - user.links.where("created_at >= ?", date).group('date(created_at)').count + def links_till( date) + links.where("created_at >= ?", date).group('date(created_at)').count end def favourite_links self.links.where(favourite: true) end - def current_user_links - self.links + def user_learned_links + learn_time.order(:created_at => :desc).map { |link| link.link }.uniq + end + + def user_learn_count_till(date) + learn_time.where("created_at >= ?", date).group('date(created_at)').count end end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 2d537af..5c23e45 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -6,6 +6,12 @@
- - \ No newline at end of file + \ No newline at end of file diff --git a/app/views/links/index.html.erb b/app/views/links/index.html.erb index 4d2e0b9..f999f74 100644 --- a/app/views/links/index.html.erb +++ b/app/views/links/index.html.erb @@ -22,12 +22,4 @@ <% end %> - - \ No newline at end of file + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3a34ada..9a92eda 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ get 'links/favourites' => 'links#favourites', as: :favourite_links get 'links/import_form' => 'links#import_form', as: :links_import_form get 'links/sort_links' => 'links#sort_links', as: :sort_links - get 'links/search' => 'links#search', as: :links_search + get 'links/search' => 'links#search', as: :search_links resources :links do collection { post :import } end From 939c5dc471a1bd6b84743f13ae5adcfdeb302144 Mon Sep 17 00:00:00 2001 From: "Hiren.Bhalani-BTC" Date: Mon, 1 Jun 2015 12:27:12 +0530 Subject: [PATCH 8/9] change in learn_time_controller, as per active-record transactions. --- app/controllers/learn_time_controller.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/controllers/learn_time_controller.rb b/app/controllers/learn_time_controller.rb index 2502393..5d6fdf0 100644 --- a/app/controllers/learn_time_controller.rb +++ b/app/controllers/learn_time_controller.rb @@ -1,12 +1,9 @@ class LearnTimeController < ApplicationController def create @link = Link.find(params[:link_id]) - LearnTime.transaction do - Link.transaction do @learn_time = LearnTime.create!(user_id: current_user.id, link_id: params[:link_id]) @link.update_column(:last_learned_at, @learn_time.created_at) - end end respond_to do |format| format.html From dc2a956e24907e052c96b2024cbeb9d8ec30165e Mon Sep 17 00:00:00 2001 From: "Hiren.Bhalani-BTC" Date: Mon, 1 Jun 2015 14:25:38 +0530 Subject: [PATCH 9/9] code refactor. --- app/assets/javascripts/application.js.erb | 1 + app/controllers/learn_time_controller.rb | 3 --- app/controllers/links_controller.rb | 4 ++-- app/helpers/links_helper.rb | 2 +- app/models/link.rb | 4 ++-- app/models/user.rb | 6 +++--- app/views/layouts/_left_sidebar.html.erb | 4 ++-- app/views/links/_learn_count.html.erb | 2 +- app/views/links/{sort_links.js.erb => sort.js.erb} | 0 config/routes.rb | 13 ++++++++----- 10 files changed, 20 insertions(+), 19 deletions(-) rename app/views/links/{sort_links.js.erb => sort.js.erb} (100%) diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index d41c670..768eb10 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -17,6 +17,7 @@ //= require turbolinks //= require bootstrap //= require custom +//= require sort_links //= require chosen.proto //= require chosen.jquery //= require links.js.erb diff --git a/app/controllers/learn_time_controller.rb b/app/controllers/learn_time_controller.rb index 5d6fdf0..f102ef3 100644 --- a/app/controllers/learn_time_controller.rb +++ b/app/controllers/learn_time_controller.rb @@ -11,7 +11,4 @@ def create end end - def index - @links = current_user.learn_time - end end diff --git a/app/controllers/links_controller.rb b/app/controllers/links_controller.rb index 9b0b3e5..317e3aa 100644 --- a/app/controllers/links_controller.rb +++ b/app/controllers/links_controller.rb @@ -21,7 +21,7 @@ def index end end - def sort_links + def sort case params[:sort_by] when 'Added On' @sorted_links = search_string_is_empty? ? current_user_links.order_by_created_at : @@ -33,7 +33,7 @@ def sort_links @sorted_links = search_string_is_empty? ? current_user.user_learned_links : search_list.reorder('last_learned_at DESC').reject{|link| link.last_learned_at.nil? } when 'Learn Count' - @sorted_links = search_string_is_empty? ? current_user_links.sort_by{ |link| link.learn_time.count }.reverse : + @sorted_links = search_string_is_empty? ? current_user_links.sort_by{ |link| link.learn_times.count }.reverse : search_list.reorder('learn_times_count DESC') end @sorted_links = @sorted_links.paginate(page: page) diff --git a/app/helpers/links_helper.rb b/app/helpers/links_helper.rb index 1c6b74d..fd2b639 100644 --- a/app/helpers/links_helper.rb +++ b/app/helpers/links_helper.rb @@ -44,7 +44,7 @@ def set_tool_tip(link) end def learning_count(link) - link.learn_time.count + link.learn_times.count end def set_tooltip_on_count(number) diff --git a/app/models/link.rb b/app/models/link.rb index ca26fe8..7f79c4c 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -4,7 +4,7 @@ class Link < ActiveRecord::Base multisearchable against: [:title, :description] pg_search_scope :search, against: [:title, :description, :created_at, :updated_at], - associated_against: { link_type: :name, category: :name, learn_time: :created_at }, + associated_against: { link_type: :name, category: :name, learn_times: :created_at }, using: { tsearch: { prefix: true } } self.per_page = 20 @@ -13,7 +13,7 @@ class Link < ActiveRecord::Base validates :url, format: { with: URI::regexp(%w(http https)) } has_many :favourites belongs_to :user - has_many :learn_time + has_many :learn_times belongs_to :category belongs_to :learning_status belongs_to :link_type diff --git a/app/models/user.rb b/app/models/user.rb index fd1806f..a69b846 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,7 +9,7 @@ class User < ActiveRecord::Base attr_accessor :current_password has_many :favourites has_many :links - has_many :learn_time + has_many :learn_times def links_till( date) links.where("created_at >= ?", date).group('date(created_at)').count @@ -20,10 +20,10 @@ def favourite_links end def user_learned_links - learn_time.order(created_at: :desc).map { |learn_time| learn_time.link }.uniq + learn_times.order(created_at: :desc).map { |learn_time| learn_time.link }.uniq end def user_learn_count_till(date) - learn_time.where("created_at >= ?", date).group('date(created_at)').count + learn_times.where("created_at >= ?", date).group('date(created_at)').count end end diff --git a/app/views/layouts/_left_sidebar.html.erb b/app/views/layouts/_left_sidebar.html.erb index 07f0e5b..05d044f 100644 --- a/app/views/layouts/_left_sidebar.html.erb +++ b/app/views/layouts/_left_sidebar.html.erb @@ -8,7 +8,7 @@ <% end %>
  • - <%= nav_link_to favourite_links_path do %> + <%= nav_link_to favourites_links_path do %> Favourites (<%= favourite_links_count%>) <% end %> @@ -32,7 +32,7 @@ <% end %>
  • - <%= nav_link_to links_import_form_path do %> + <%= nav_link_to import_form_links_path do %> Import Links <% end %> diff --git a/app/views/links/_learn_count.html.erb b/app/views/links/_learn_count.html.erb index 4b10f88..be90faf 100644 --- a/app/views/links/_learn_count.html.erb +++ b/app/views/links/_learn_count.html.erb @@ -1 +1 @@ -<%= link.learn_time.count %> \ No newline at end of file +<%= link.learn_times.count %> \ No newline at end of file diff --git a/app/views/links/sort_links.js.erb b/app/views/links/sort.js.erb similarity index 100% rename from app/views/links/sort_links.js.erb rename to app/views/links/sort.js.erb diff --git a/config/routes.rb b/config/routes.rb index 9a92eda..3a148ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,15 @@ Rails.application.routes.draw do root 'links#index' - get 'links/favourites' => 'links#favourites', as: :favourite_links - get 'links/import_form' => 'links#import_form', as: :links_import_form - get 'links/sort_links' => 'links#sort_links', as: :sort_links - get 'links/search' => 'links#search', as: :search_links + resources :links do - collection { post :import } + collection do + post :import + get :favourites + get :import_form + get :sort + get :search + end end resources :tag resources :favourites