diff --git a/Gemfile b/Gemfile index 5fbb65c..8dd72d9 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,7 @@ gem 'sass-rails', '~> 4.0.2' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.0.0' gem 'jquery-rails' +gem 'jquery-ui-rails' gem 'turbolinks' gem 'bootstrap-sass', '~> 3.3' @@ -52,7 +53,7 @@ gem 'bugsnag' gem 'redcarpet' # API stackoverflow -gem 'ruby-stackoverflow', git: 'https://github.com/PowerCodeGirls/ruby-stackoverflow.git' +gem 'ruby-stackoverflow', git: 'https://github.com/PowerCodeGirls/ruby-stackoverflow.git' gem 'sidekiq' gem "sidekiq-cron", "~> 0.3.0" diff --git a/Gemfile.lock b/Gemfile.lock index 1c03192..8bd4c75 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -125,6 +125,8 @@ GEM jquery-rails (3.1.3) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) + jquery-ui-rails (5.0.5) + railties (>= 3.2.16) json (1.8.3) jwt (1.5.1) launchy (2.4.3) @@ -327,6 +329,7 @@ DEPENDENCIES guard-rspec jbuilder (~> 1.2) jquery-rails + jquery-ui-rails launchy newrelic_rpm omniauth diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 703cb38..f727611 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,6 +12,7 @@ // //= require jquery //= require jquery_ujs +//= require jquery-ui //= require turbolinks //= require bootstrap-sprockets //= require_tree . @@ -20,7 +21,7 @@ $(function(){ $('#welcome_message').bind('closed.bs.alert', function() { var form = $('#welcome_message').next('form'); $.post(form.attr('action'), form.serialize()); - }) + }); $('#digest_signup_form') .bind("ajax:success", function(data, status, xhr) { @@ -29,4 +30,82 @@ $(function(){ .bind("ajax:error", function(xhr, status, error) { alert('Failed.'); }); -}) +}); + +$(function() { + $('.tags-input').autocomplete({ + minLength: 1, + source: "/tags.json", + autoFocus: true, + + focus: function() { + // prevent value inserted on focus + return false; + }, + + select: function(event, ui) { + var terms = this.value.split(/,\s*/); + // remove the current input + terms.pop(); + // add the selected item + terms.push(ui.item.value); + // add an empty item for a trailing comma + terms.push(""); + + this.value = terms.join(", "); + $(this).autocomplete('close'); + return false; + } + }); + + $('.tags-input').on('blur', function() { + // remove any trailing commas + this.value = this.value.replace(/,\s*$/, ''); + }); +}); + +$(function() { + $(".vote-count a").on('click', function() { + var link = $(this); + var url = $(this).attr('href'); + + $.ajax({ + url: url, + type: "PUT", + + success: function(response) { + link.replaceWith(response); + } + }); + + return false; + }); +}); + +$(function() { + $(".post-link").on('click', function() { + var link = $(this); + var url = link.attr('href'); + var container = link.parent('td'); + + if (container.find('.post-summary:visible').length > 0) { + $('.post-summary').slideUp(); + } else if (container.find('.post-summary:hidden').length > 0) { + container.find('.post-summary:hidden').slideDown(); + } else { + $('.post-summary').slideUp(); + + $.ajax({ + url: url, + success: function(response) { + var postSummary = $(response); + postSummary.hide(); + container.append(postSummary); + postSummary.slideDown(); + } + }) + } + + return false; + }); +}); \ No newline at end of file diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 6769138..5e4bba7 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -17,6 +17,10 @@ def index def show @comment = Comment.new @comment.parent = @post + + if request.xhr? + render partial: 'posts/post_summary' + end end # GET /posts/new @@ -72,11 +76,16 @@ def destroy end end - def vote + def vote @post = Post.find(params[:id]) @post.add_vote(current_user) - flash[:notice] = "You have successfully voted" - redirect_to(:back) + + if request.xhr? + render partial: 'votes/vote', locals: {post: @post} + else + flash[:notice] = "You have successfully voted" + redirect_to(:back) + end end private @@ -97,4 +106,4 @@ def post_params params.require(post_type.underscore.to_sym). permit(:title, :body_markdown, :user_id, :tags_string) end -end +end \ No newline at end of file diff --git a/app/views/posts/_list.html.erb b/app/views/posts/_list.html.erb index df55391..b0c8f11 100644 --- a/app/views/posts/_list.html.erb +++ b/app/views/posts/_list.html.erb @@ -5,8 +5,10 @@ <% if post.instance_of? Guide %> <%= image_tag 'Guide.png', alt: 'Guides', class: 'guide-xs pull-left show', width: 25 %> - <% end %> - <%= link_to post.title, post_path(post) %> + <% end %> + + <%= link_to post.title, post_path(post), class: 'post-link' %> +
Submitted by <%= post.username %> on <%= post.created_at.strftime("%m/%d/%Y") %>
<%= render 'posts/tags', post: post %> @@ -17,4 +19,4 @@ <% end %> - + \ No newline at end of file diff --git a/app/views/posts/_post_summary.html.erb b/app/views/posts/_post_summary.html.erb new file mode 100644 index 0000000..4f753c7 --- /dev/null +++ b/app/views/posts/_post_summary.html.erb @@ -0,0 +1,15 @@ +
+ <% if @post.type == "Guide" %> +

+

Category: <%= @post.category.name %>

+

+ <% end %> + +
+ <%= raw @post.body_html %> + <% if @post.external_post? %> +

<%= link_to 'Visit Website', @post.source_url %>

+ <% end %> + <%= link_to "View post details", post_path(@post) %> +
+
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 268dc7f..dd9de09 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -8,10 +8,10 @@

<%= @post.title %> - <%= render 'votes/vote', post: @post %> + <%= render 'posts/post_summary', post: @post %> <% if @post.instance_of? Guide %> <%= image_tag 'Guide.png', alt: 'Guides', class: 'guide-xs pull-left show', width: 25 %> - <% end %> + <% end %>

<% if @post.type == "Guide" %>