From 475a55f42d05eb05f5e2fc81340c51445a1da25d Mon Sep 17 00:00:00 2001 From: Leonel Samayoa Date: Sat, 25 Jul 2015 07:54:15 -0600 Subject: [PATCH] ISSUE-203: ajaxified star/unstar --- app/controllers/repositories_controller.rb | 2 ++ app/views/repositories/show.html.slim | 6 +++--- app/views/repositories/star.js.erb | 15 +++++++++++++++ app/views/repositories/unstar.js.erb | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 app/views/repositories/star.js.erb create mode 100644 app/views/repositories/unstar.js.erb diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 679097e11..c5f9135bf 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -22,6 +22,7 @@ def star respond_to do |format| format.html { redirect_to(repository_path(@repository)) } format.json { render json: @repository } + format.js {} end end @@ -32,6 +33,7 @@ def unstar respond_to do |format| format.html { redirect_to(repository_path(@repository)) } format.json { render json: @repository } + format.js {} end end diff --git a/app/views/repositories/show.html.slim b/app/views/repositories/show.html.slim index 1ad651c64..7787a22d1 100644 --- a/app/views/repositories/show.html.slim +++ b/app/views/repositories/show.html.slim @@ -4,12 +4,12 @@ = link_to @repository.namespace.clean_name, @repository.namespace | / = @repository.name - .pull-right + #repo-stars.pull-right - if can_star_repository?(@repository) - = link_to 'Star', star_repository_path(@repository), method: :post, id: 'star_repo', class: 'btn btn-small btn-default' + = link_to 'Star', star_repository_path(@repository), method: :post, class: 'btn btn-small btn-default', remote: true i.fa.fa-star-o - else - = link_to 'Unstar', unstar_repository_path(@repository), method: :post, id: 'unstar_repo', class: 'btn btn-small btn-default' + = link_to 'Unstar', unstar_repository_path(@repository), method: :post, class: 'btn btn-small btn-default', remote: true i.fa.fa-star span#star-counter= @repository.stars.count .panel-body diff --git a/app/views/repositories/star.js.erb b/app/views/repositories/star.js.erb new file mode 100644 index 000000000..0aeb6069c --- /dev/null +++ b/app/views/repositories/star.js.erb @@ -0,0 +1,15 @@ +<% if @repository.errors.any? %> + $('#alert p').html("<%= escape_javascript(@repository.errors.full_messages.join('
')) %>"); + $('#alert').fadeIn(); +<% else %> +(function () { + var link = $('#repo-stars a'); + var icon = $('#repo-stars i') + var counter = $('#repo-stars #star-counter'); + link.html("<%= escape_javascript(t('Unstar')) %>"); + link.attr("href", "<%= escape_javascript(unstar_repository_path(@repository)) %>"); + icon.addClass("fa-star"); + icon.removeClass("fa-star-o"); + counter.html("<%= escape_javascript(@repository.stars.count.to_s) %>"); +})(); +<% end %> diff --git a/app/views/repositories/unstar.js.erb b/app/views/repositories/unstar.js.erb new file mode 100644 index 000000000..66bdcf8ee --- /dev/null +++ b/app/views/repositories/unstar.js.erb @@ -0,0 +1,15 @@ +<% if @repository.errors.any? %> + $('#alert p').html("<%= escape_javascript(@repository.errors.full_messages.join('
')) %>"); + $('#alert').fadeIn(); +<% else %> +(function () { + var link = $('#repo-stars a'); + var icon = $('#repo-stars i') + var counter = $('#repo-stars #star-counter'); + link.html("<%= escape_javascript(t('Star')) %>"); + link.attr("href", "<%= escape_javascript(star_repository_path(@repository)) %>"); + icon.addClass("fa-star-o"); + icon.removeClass("fa-star"); + counter.html("<%= escape_javascript(@repository.stars.count.to_s) %>"); +})(); +<% end %>