Skip to content

Commit 4d6cd66

Browse files
author
Hiren.Bhalani-BTC
committed
Code Refactor.
1 parent 2e26380 commit 4d6cd66

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

app/controllers/links_controller.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class LinksController < ApplicationController
99
def index
1010
selected_tag = params[:tag]
1111
if selected_tag
12-
@links = current_user_links.tagged_with(selected_tag).order(:created_at => :desc).paginate(page: page)
12+
@links = current_user_links.tagged_with(selected_tag).order(created_at: :desc).paginate(page: page)
1313
else
14-
@links = current_user_links.order(:created_at => :desc).paginate(page: page)
14+
@links = current_user_links.order(created_at: :desc).paginate(page: page)
1515
end
1616
respond_to do |format|
1717
format.html
@@ -42,7 +42,7 @@ def new
4242

4343
def create
4444
@link = Link.new(link_params.merge({ user_id: current_user.id }).except!(:tag_list))
45-
current_user.tag(@link, :with => link_params[:tag_list], :on => :tags)
45+
current_user.tag(@link, with: link_params[:tag_list], on: :tags)
4646

4747
if @link.save
4848
redirect_to root_path
@@ -59,7 +59,7 @@ def edit
5959
def update
6060
if @link.present?
6161
@link.update(link_params.merge({user_id: current_user.id}).except!(:tag_list))
62-
current_user.tag(@link, :with => link_params[:tag_list], :on => :tags)
62+
current_user.tag(@link, with: link_params[:tag_list], on: :tags)
6363
flash[:success] = 'Successfully Updated!!'
6464
redirect_to root_path
6565
else
@@ -76,7 +76,7 @@ def destroy
7676
end
7777

7878
def favourites
79-
@links = current_user.links.where(favourite: true).order(:created_at => :desc).paginate(page: page)
79+
@links = current_user.links.where(favourite: true).order(created_at: :desc).paginate(page: page)
8080
render 'links/index'
8181
end
8282

@@ -93,7 +93,7 @@ def import
9393

9494
private
9595
def link_params
96-
params.require(:link).permit(:title, :url, :learning_status_id, :description, :category_id, :user_id, :link_type_id, :tag_list => [])
96+
params.require(:link).permit(:title, :url, :learning_status_id, :description, :category_id, :user_id, :link_type_id, tag_list: [])
9797
end
9898

9999
def assign_link

app/helpers/links_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
module LinksHelper
22
include ActsAsTaggableOn::TagsHelper
33

4+
SORT_OPTIONS = ['Added On', 'Updated On', 'Recently Learned', 'Learn Count' ]
5+
6+
def options_for_sorting
7+
options_for_select(SORT_OPTIONS)
8+
end
9+
410
def is_favourite_link?(link)
511
link.favourite
612
end

app/models/link.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class Link < ActiveRecord::Base
1111
belongs_to :learning_status
1212
belongs_to :link_type
1313

14-
scope :order_by_created_at, -> { order(:created_at => :desc) }
14+
scope :order_by_created_at, -> { order(created_at: :desc) }
1515

16-
scope :order_by_updated_at, -> { order(:updated_at => :desc) }
16+
scope :order_by_updated_at, -> { order(updated_at: :desc) }
1717

1818
def create_favourite(user_id, link_id)
1919
favourites.create!(user_id: user_id, link_id: link_id)
@@ -34,7 +34,7 @@ def self.find_or_create(hash, current_user)
3434
if @link.empty?
3535
without_taglist = hash.except('tag_list')
3636
@link = self.create! without_taglist
37-
current_user.tag(@link, :with => hash['tag_list'], :on => :tags)
37+
current_user.tag(@link, with: hash['tag_list'], on: :tags)
3838
end
3939
end
4040
end

app/views/links/index.html.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<% else %>
66
<div class="col-lg-8 col-md-8 col-sm-8" id="links-list">
77
<%= form_tag sort_links_path, remote: true, id: 'sorting_form' do %>
8-
<%= select_tag :sort_by, options_for_select(['Added On', 'Updated On', 'Recently Learned', 'Learn Count' ]), class: "select_box" %>
8+
<%= select_tag :sort_by, options_for_sorting, class: "select_box" %>
99
<% end %>
1010
<div class="links">
11-
<%= render partial: 'links/links_list', locals: { links: @links}%>
11+
<%= render partial: 'links/links_list', locals: { links: @links }%>
1212
</div>
1313
</div>
1414
<div class="col-lg-3 col-md-3 col-sm-3 showback tag-cloud-width">
@@ -23,7 +23,9 @@
2323
</div>
2424
</section>
2525
<script type="text/javascript">
26+
$(document).ready(function(){
2627
$('.select_box').change(function(){
2728
$('#sorting_form').submit();
2829
});
30+
});
2931
</script>

0 commit comments

Comments
 (0)