Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/assets/stylesheets/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
65 changes: 36 additions & 29 deletions app/controllers/charts_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions app/controllers/learn_time_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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])
@link.update_column(:last_learned_at, @learn_time.created_at)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LearnTime creation and last_learned_at should be wrapped in a transaction.

Check http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

respond_to do |format|
format.html
format.js
Expand Down
35 changes: 28 additions & 7 deletions app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'will_paginate/array'
class LinksController < ApplicationController
before_filter :authenticate_user!
before_filter :assign_link, only: [:update, :destroy, :edit]
Expand All @@ -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
Expand All @@ -20,13 +21,28 @@ def index
end
end

def sort_links
case params[:sort_by]
when 'Added On'
@sorted_links = params[:search_string].empty? ? current_user_links.order_by_created_at.paginate(page: page) : search_list.reorder('created_at DESC').paginate(page: page)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params[:search_string] appears multiple time. So it is candidate for local variable.

when 'Updated On'
@sorted_links = params[:search_string].empty? ? current_user_links.order_by_updated_at.paginate(page: page) : search_list.reorder('updated_at DESC').paginate(page: page)
when 'Recently Learned'
@sorted_links = params[:search_string].empty? ? current_user.user_learned_links.paginate(page: page) : search_list.reorder('last_learned_at DESC').reject{|link| link.last_learned_at.nil? }.paginate(page: page)
when 'Learn Count'
@sorted_links = params[:search_string].empty? ? @current_user_links.sort_by{ |link| link.learn_time.count }.reverse.paginate(page: page) : search_list.reorder('learn_times_count DESC').paginate(page: page)
end
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
Expand All @@ -43,7 +59,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
Expand All @@ -60,7 +76,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

Expand All @@ -75,12 +91,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 = params[:search_string].empty? ? current_user_links.order(created_at: :desc).paginate(page: page) :
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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ApplicationHelper
def links_count
current_user.current_user_links.count
current_user.links.count
end

def favourite_links_count
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/links_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 2 additions & 5 deletions app/models/learn_time.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_time: :created_at },
using: { tsearch: { prefix: true } }

self.per_page = 20
Expand All @@ -18,9 +18,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: :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)
Expand All @@ -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
12 changes: 8 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ class User < ActiveRecord::Base
has_many :links
has_many :learn_time
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't learn_time be pluralize 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_time.order(created_at: :desc).map { |link| link.link }.uniq
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to use join instead of map to get links and compare peformance?

end

def user_learn_count_till(date)
learn_time.where("created_at >= ?", date).group('date(created_at)').count
end
end
11 changes: 9 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</div>
<%= link_to 'Learning Path', root_path, class: 'logo' %>
<div class="search_bar">
<%= form_tag links_search_path, remote: true, id: 'search' do %>
<%= form_tag links_search_path, method: :get, remote: true, id: 'search' do %>
<%= text_field_tag 'search_string', nil, class: 'search_box' %>
<button class='fa fa-search search_button' style='width:40px;'>
</button>
Expand All @@ -19,4 +19,11 @@
<% end %>
</ul>
</div>
</header>
</header>
<script>
$(document).ready(function(){
$('#search').submit(function(){
$('.links').empty();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested elsewhere move it to a common function clearLinksPage

});
});
</script>
4 changes: 2 additions & 2 deletions app/views/links/_links_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<div class="showback" >
<div class="left"><i title= "<%= link.link_type.name %>" class='<%= "#{set_category_icon(link)}" %>'></i><br/>
<span id="learn_count_<%=link.id %>">
<%= render :partial => 'learn_count', :locals => {link: link} %>
<%= render partial: 'learn_count', locals: {link: link} %>
</span><br>
<i id="fav_<%=link.id %>">
<%= render :partial => 'favourite_btn', :locals => {link: link} %>
<%= render partial: 'favourite_btn', locals: {link: link} %>
</i>
</div><p>
<%= link_to "#{link.title}", link.url, target: '_blank'%></p>
Expand Down
22 changes: 18 additions & 4 deletions app/views/links/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
<% if @links.blank? %>
<div class="no_links_msg">You do not have anything to display at this time. <%= link_to 'Start saving one now!', new_link_path%></div>
<% else %>
<div class="col-lg-8 col-md-8 col-sm-8 links" id="links-list">
<%= render partial: 'links/links_list', locals: { links: @links}%>
<div class="col-lg-8 col-md-8 col-sm-8" id="links-list">
<%= form_tag sort_links_path, remote: true, method: :get, id: 'sorting_form' do %>
<%= hidden_field_tag :search_string, '', class: 'search_field_val' %>
<%= select_tag :sort_by, options_for_sorting, class: "select_box" %>
<% end %>
<div class="links">
<%= render partial: 'links/links_list', locals: { links: @links }%>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 showback tag-cloud-width">
<% tag_cloud Link.tag_counts, %w{s m l} do |tag, css_class| %>
<%= link_to tag.name, links_path(tag: tag.name), class: css_class %>
<% end %>
</div>
<div class="link_pagination">
<%= will_paginate @links || @search_list %>
<%= will_paginate @links %>
</div>
<% end %>
</div>
</section>
</section>
<script type="text/javascript">
$(document).ready(function(){
$('.select_box').change(function(){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binding change change should be wrapped in document.ready event handler.

$('#sorting_form').submit();
$('.links').empty();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to common function clearLinksPage

});
});
</script>
3 changes: 2 additions & 1 deletion app/views/links/search.js.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$('.links').html("<%= escape_javascript(render partial: 'links/links_list', locals: { links: @links = @search_list})%>");
$('.links').append("<%= escape_javascript(render partial: 'links/links_list', locals: { links: @links = @search_list})%>");
$('.search_field_val').val("<%= params[:search_string]%>");
$('.pagination').replaceWith('<%= escape_javascript(will_paginate(@search_list) )%>');
$('.flash_msg').remove();
3 changes: 3 additions & 0 deletions app/views/links/sort_links.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('.links').append("<%= escape_javascript(render partial: 'links/links_list', locals: { links: @links = @sorted_links })%>");
$('.pagination').replaceWith('<%= escape_javascript(will_paginate(@sorted_links) )%>');
$('.flash_msg').remove();
Loading