Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/assets/javascripts/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$(document).ready(function(){
$('#search').submit(function(){
$('.links').empty();
});
});
6 changes: 6 additions & 0 deletions app/assets/javascripts/sort_links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$(document).ready(function(){
$('.select_box').change(function(){
$('#sorting_form').submit();
$('.links').empty();
});
});
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
8 changes: 4 additions & 4 deletions app/controllers/learn_time_controller.rb
Original file line number Diff line number Diff line change
@@ -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
42 changes: 35 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,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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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'
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
8 changes: 7 additions & 1 deletion 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 Expand Up @@ -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)
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
14 changes: 7 additions & 7 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_times: :created_at },
using: { tsearch: { prefix: true } }

self.per_page = 20
Expand All @@ -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)
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
14 changes: 9 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
</div>
<%= link_to 'Learning Path', root_path, class: 'logo' %>
<div class="search_bar">
<%= form_tag links_search_path, 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>
<%= form_tag search_links_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>
<% end %>
</div>

</div>
<ul class="nav pull-right top-menu">
<% if current_user %>
<li><%= link_to 'Profile', edit_user_registration_path(@current_user), class: 'logout' %></li>
<li><%= link_to 'Profile', edit_user_registration_path(current_user), class: 'logout' %></li>
<li><%= link_to 'Logout', destroy_user_session_path, method: :delete, class: 'logout' %></li>
<% end %>
</ul>
Expand Down
Loading