Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
43 changes: 43 additions & 0 deletions app/assets/stylesheets/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,47 @@ a:focus, a:active {

.tag-cloud-width{
width: 32% !important;
}

.sort_links {
font-size: 14px;
padding: 5px 15px;
margin-right: 15px;
margin-top: 15px;
}

.showback{
width: 100% !important;
}

.sort-btn {
position: relative;
clear:left;
margin-top: 0px !important;
margin-bottom: 5px !important;
}

.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
16 changes: 16 additions & 0 deletions app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def index
end
end

def sort_links
case params[:sort_by]
when 'Added On'
@links = current_user_links.order_by_created_at
when 'Updated On'
@links = current_user_links.order_by_updated_at
when 'Recently Learned'
@links = current_user.user_learned_links
when '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
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
5 changes: 1 addition & 4 deletions app/models/learn_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => :desc) }
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.

User ruby 2 style hash notation


scope :order_by_updated_at, -> { order(:updated_at => :desc) }
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.

User ruby 2 style hash notation


def create_favourite(user_id, link_id)
favourites.create!(user_id: user_id, link_id: link_id)
Expand Down
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.

User ruby 2 style hash notation

end

def user_learn_count_till(date)
learn_time.where("created_at >= ?", date).group('date(created_at)').count
end
end
16 changes: 13 additions & 3 deletions app/views/links/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
<% 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, id: 'sorting_form' do %>
<%= select_tag :sort_by, options_for_select(['Added On', 'Updated On', 'Recently Learned', 'Learn Count' ]), class: "select_box" %>
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 sort option to a constant in LinksHelper

<% end %>
<div class="links">
<%= render partial: 'links/links_list', locals: { links: @links}%>
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.

required space before }

</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| %>
Expand All @@ -16,4 +21,9 @@
</div>
<% end %>
</div>
</section>
</section>
<script type="text/javascript">
$('.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();
});
</script>
1 change: 1 addition & 0 deletions app/views/links/sort_links.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('.links').html("<%= escape_javascript(render partial: 'links/links_list', locals: { links: @links})%>");
8 changes: 5 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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/favourites' => 'links#favourites', as: :favourite_links
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 all links/* routes mapping under resources :links as collection routes

get 'links/import_form' => 'links#import_form', as: :links_import_form
post 'links/sort_links' => 'links#sort_links', as: :sort_links

resources :links do
collection { post :import }
end
Expand All @@ -17,7 +19,7 @@
# devise_scope :users do
# get '/users' => 'users#index'
# end
post 'links/toggle' => 'favourites#toggle', :as => :toggle_favourite
post 'links/toggle' => 'favourites#toggle', as: :toggle_favourite
devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations', passwords: 'users/passwords'}

# You can have the root of your site routed with "root"
Expand Down