diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index ca90ac4..768eb10 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -16,6 +16,8 @@ //= require bootstrap-sprockets //= require turbolinks //= require bootstrap +//= require custom +//= require sort_links //= require chosen.proto //= require chosen.jquery //= require links.js.erb diff --git a/app/assets/javascripts/custom.js b/app/assets/javascripts/custom.js new file mode 100644 index 0000000..83d9966 --- /dev/null +++ b/app/assets/javascripts/custom.js @@ -0,0 +1,5 @@ +$(document).ready(function(){ + $('#search').submit(function(){ + $('.links').empty(); + }); +}); \ No newline at end of file diff --git a/app/assets/javascripts/sort_links.js b/app/assets/javascripts/sort_links.js new file mode 100644 index 0000000..ac6fd38 --- /dev/null +++ b/app/assets/javascripts/sort_links.js @@ -0,0 +1,6 @@ +$(document).ready(function(){ + $('.select_box').change(function(){ + $('#sorting_form').submit(); + $('.links').empty(); + }); +}); \ No newline at end of file diff --git a/app/assets/stylesheets/custom.css b/app/assets/stylesheets/custom.css index 2598ff4..c0635c8 100644 --- a/app/assets/stylesheets/custom.css +++ b/app/assets/stylesheets/custom.css @@ -205,4 +205,36 @@ a:focus, a:active { background-color: transparent; border: none; margin-left: -5px; +} + +.sort_links { + font-size: 14px; + padding: 5px 15px; + margin-right: 15px; + margin-top: 15px; +} + +.links { + float: left; + clear: left; +} + +.select_box { display: block; + float: right; + width: auto; + margin-bottom: 5px; + height: 34px; + padding: 6px 12px; + font-size: 14px; + line-height: 1.42857143; + color: #555; + background-color: #fff; + background-image: none; + border: 1px solid #ccc; + border-radius: 4px; + -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075); + box-shadow: inset 0 1px 1px rgba(0,0,0,.075); + -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s; + -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s; + transition: border-color ease-in-out .15s,box-shadow ease-in-out .15 } \ No newline at end of file diff --git a/app/controllers/charts_controller.rb b/app/controllers/charts_controller.rb index 8e281fb..0fffcc0 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/learn_time_controller.rb b/app/controllers/learn_time_controller.rb index 5e8bc69..f102ef3 100644 --- a/app/controllers/learn_time_controller.rb +++ b/app/controllers/learn_time_controller.rb @@ -1,14 +1,14 @@ class LearnTimeController < ApplicationController def create @link = Link.find(params[:link_id]) - @learn_time = LearnTime.create!(user_id: current_user.id, link_id: params[:link_id]) + LearnTime.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 respond_to do |format| format.html format.js 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 2110932..317e3aa 100644 --- a/app/controllers/links_controller.rb +++ b/app/controllers/links_controller.rb @@ -1,3 +1,4 @@ +require 'will_paginate/array' class LinksController < ApplicationController before_filter :authenticate_user! before_filter :assign_link, only: [:update, :destroy, :edit] @@ -9,9 +10,9 @@ class LinksController < ApplicationController def index selected_tag = params[:tag] if selected_tag - @links = current_user_links.tagged_with(selected_tag).order(:created_at => :desc).paginate(page: page) + @links = current_user_links.tagged_with(selected_tag).order(created_at: :desc).paginate(page: page) else - @links = current_user_links.order(:created_at => :desc).paginate(page: page) + @links = current_user_links.order(created_at: :desc).paginate(page: page) end respond_to do |format| format.html @@ -20,13 +21,31 @@ def index end end + def sort + case params[:sort_by] + when 'Added On' + @sorted_links = search_string_is_empty? ? current_user_links.order_by_created_at : + search_list.reorder('created_at DESC') + when 'Updated On' + @sorted_links = search_string_is_empty? ? current_user_links.order_by_updated_at : + search_list.reorder('updated_at DESC') + when 'Recently Learned' + @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_times.count }.reverse : + search_list.reorder('learn_times_count DESC') + end + @sorted_links = @sorted_links.paginate(page: page) + end + def new @link = Link.new end def create @link = Link.new(link_params.merge({ user_id: current_user.id }).except!(:tag_list)) - current_user.tag(@link, :with => link_params[:tag_list], :on => :tags) + current_user.tag(@link, with: link_params[:tag_list], on: :tags) if @link.save redirect_to root_path @@ -43,7 +62,7 @@ def edit def update if @link.present? @link.update(link_params.merge({user_id: current_user.id}).except!(:tag_list)) - current_user.tag(@link, :with => link_params[:tag_list], :on => :tags) + current_user.tag(@link, with: link_params[:tag_list], on: :tags) flash[:success] = 'Successfully Updated!!' redirect_to root_path else @@ -60,7 +79,7 @@ def destroy end def favourites - @links = current_user.links.where(favourite: true).order(:created_at => :desc).paginate(page: page) + @links = current_user.links.where(favourite: true).order(created_at: :desc).paginate(page: page) render 'links/index' end @@ -75,12 +94,17 @@ def import end def search - @search_list = params[:search_string].empty? ? current_user_links.order(:created_at => :desc).paginate(page: page) : Link.search(params[:search_string]).paginate(page: page) + @search_list = search_string_is_empty? ? current_user_links.order(created_at: :desc) : search_list + @search_list = @search_list.paginate(page: page) + end + + def search_list + Link.search(params[:search_string]) end private def link_params - params.require(:link).permit(:title, :url, :learning_status_id, :description, :category_id, :user_id, :link_type_id, :tag_list => []) + params.require(:link).permit(:title, :url, :learning_status_id, :description, :category_id, :user_id, :link_type_id, tag_list: []) end def assign_link @@ -108,6 +132,10 @@ def current_user_links current_user.links end + def search_string_is_empty? + params[:search_string].empty? + end + def to_csv(user, options= {}) column_names = Link.column_names column_names << 'tag_list' unless column_names.include? 'tag_list' diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index be32e32..843ccd9 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -43,7 +43,7 @@ def update @user = User.find(current_user.id) if @user.update(account_update_params) set_flash_message :notice, :updated - sign_in @user, :bypass => true + sign_in @user, bypass: true redirect_to after_update_path_for(@user) else render "edit" 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/helpers/links_helper.rb b/app/helpers/links_helper.rb index 0850696..fd2b639 100644 --- a/app/helpers/links_helper.rb +++ b/app/helpers/links_helper.rb @@ -1,6 +1,12 @@ module LinksHelper include ActsAsTaggableOn::TagsHelper + SORT_OPTIONS = ['Added On', 'Updated On', 'Recently Learned', 'Learn Count'] + + def options_for_sorting + options_for_select(SORT_OPTIONS) + end + def is_favourite_link?(link) link.favourite end @@ -38,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/learn_time.rb b/app/models/learn_time.rb index 9f09315..01acd94 100644 --- a/app/models/learn_time.rb +++ b/app/models/learn_time.rb @@ -1,9 +1,6 @@ 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 + belongs_to :link, counter_cache: true + end diff --git a/app/models/link.rb b/app/models/link.rb index ecae50f..7f79c4c 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -3,8 +3,8 @@ class Link < ActiveRecord::Base include PgSearch multisearchable against: [:title, :description] - pg_search_scope :search, against: [:title, :description], - associated_against: { link_type: :name, category: :name }, + pg_search_scope :search, against: [:title, :description, :created_at, :updated_at], + associated_against: { link_type: :name, category: :name, learn_times: :created_at }, using: { tsearch: { prefix: true } } self.per_page = 20 @@ -13,14 +13,14 @@ 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 - def self.learn_time(user) - LearnTime.create!(user_id: user.id, link_id: self.id) - end + scope :order_by_created_at, -> { order(created_at: :desc) } + + scope :order_by_updated_at, -> { order(updated_at: :desc) } def create_favourite(user_id, link_id) favourites.create!(user_id: user_id, link_id: link_id) @@ -41,7 +41,7 @@ def self.find_or_create(hash, current_user) if @link.empty? without_taglist = hash.except('tag_list') @link = self.create! without_taglist - current_user.tag(@link, :with => hash['tag_list'], :on => :tags) + current_user.tag(@link, with: hash['tag_list'], on: :tags) end end end diff --git a/app/models/user.rb b/app/models/user.rb index 4d3b859..a69b846 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,17 +9,21 @@ class User < ActiveRecord::Base attr_accessor :current_password has_many :favourites has_many :links - has_many :learn_time + has_many :learn_times - 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_times.order(created_at: :desc).map { |learn_time| learn_time.link }.uniq + end + + def user_learn_count_till(date) + learn_times.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 38031c3..adf2520 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -4,17 +4,17 @@ <%= link_to 'Learning Path', root_path, class: 'logo' %>
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 %><%= link_to "#{link.title}", link.url, target: '_blank'%>
diff --git a/app/views/links/index.html.erb b/app/views/links/index.html.erb index 7e5fd77..f999f74 100644 --- a/app/views/links/index.html.erb +++ b/app/views/links/index.html.erb @@ -3,8 +3,14 @@ <% if @links.blank? %>