diff --git a/Gemfile b/Gemfile index a13a041..237d1cc 100644 --- a/Gemfile +++ b/Gemfile @@ -68,6 +68,7 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # ================== group :development do gem 'annotate' + gem 'htmlbeautifier' gem 'awesome_print' gem 'better_errors' gem 'binding_of_caller' diff --git a/Gemfile.lock b/Gemfile.lock index 340545c..2e91732 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,6 +136,7 @@ GEM rchardet (~> 1.8) globalid (1.0.0) activesupport (>= 5.0) + htmlbeautifier (1.4.2) i18n (1.10.0) concurrent-ruby (~> 1.0) indefinite_article (0.2.5) @@ -200,7 +201,7 @@ GEM rack (>= 1.2.0) rack-protection (2.2.0) rack - rack-proxy (0.7.2) + rack-proxy (0.7.6) rack rack-test (1.1.0) rack (>= 1.0, < 3) @@ -376,6 +377,7 @@ DEPENDENCIES devise draft_generators! faker + htmlbeautifier jbuilder (~> 2.7) listen (~> 3.3) pg (~> 1.1) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5b62df9..eac5e39 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -21,6 +21,7 @@ def new def edit respond_to do |format| format.html + format.js end end @@ -33,6 +34,7 @@ def create if @comment.save format.html { redirect_back fallback_location: root_path, notice: "Comment was successfully created." } format.json { render :show, status: :created, location: @comment } + format.js else format.html { render :new, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -46,6 +48,7 @@ def update if @comment.update(comment_params) format.html { redirect_to root_url, notice: "Comment was successfully updated." } format.json { render :show, status: :ok, location: @comment } + format.js else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @comment.errors, status: :unprocessable_entity } @@ -59,6 +62,7 @@ def destroy respond_to do |format| format.html { redirect_back fallback_location: root_url, notice: "Comment was successfully destroyed." } format.json { head :no_content } + format.js end end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 49affd3..d7ef279 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -27,6 +27,7 @@ def create if @like.save format.html { redirect_back fallback_location: root_url, notice: "Like was successfully created." } format.json { render :show, status: :created, location: @like } + format.js else format.html { render :new, status: :unprocessable_entity } format.json { render json: @like.errors, status: :unprocessable_entity } @@ -53,6 +54,7 @@ def destroy respond_to do |format| format.html { redirect_back fallback_location: root_url, notice: "Like was successfully destroyed." } format.json { head :no_content } + format.js end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 25bdd33..d439e0b 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,4 @@ -
  • +
  • <%= image_tag comment.author.avatar_image, class: "rounded-circle mr-2", width: 36 %>
    @@ -9,11 +9,11 @@
    <% if current_user == comment.author %> - <%= link_to edit_comment_path(comment), class: "btn btn-link btn-sm text-muted" do %> + <%= link_to edit_comment_path(comment), class: "btn btn-link btn-sm text-muted", remote: :true do %> <% end %> - <%= link_to comment, method: :delete, class: "btn btn-link btn-sm text-muted" do %> + <%= link_to comment, method: :delete, class: "btn btn-link btn-sm text-muted", remote: true do %> <% end %> <% end %> @@ -21,5 +21,5 @@

    <%= comment.body %>

    -
    +
  • diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index f289224..3917721 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,5 +1,5 @@ -
  • - <%= form_with(model: comment) do |form| %> +
  • + <%= form_with(model: comment, local: false ) do |form| %> <% if comment.errors.any? %>
    diff --git a/app/views/comments/edit.js.erb b/app/views/comments/edit.js.erb new file mode 100644 index 0000000..aa0562a --- /dev/null +++ b/app/views/comments/edit.js.erb @@ -0,0 +1,3 @@ +var edit_comment = $("<%= j(render 'comments/form', comment: @comment, context_id: dom_id(@comment)) %>"); +$("#<%= dom_id(@comment) %>").after(edit_comment); +$("#<%= dom_id(@comment) %>").remove(); diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb index f8b43b6..343e846 100644 --- a/app/views/comments/new.html.erb +++ b/app/views/comments/new.html.erb @@ -1,5 +1,5 @@

    New Comment

    -<%= render 'form', comment: @comment %> +<%= render 'form', comment: @comment, context_id: "new_comment" %> <%= link_to 'Back', comments_path %> diff --git a/app/views/comments/update.js.erb b/app/views/comments/update.js.erb new file mode 100644 index 0000000..8603b04 --- /dev/null +++ b/app/views/comments/update.js.erb @@ -0,0 +1,2 @@ +var updated_comment = $("<%= j(render 'comments/comment', comment: @comment) %>"); +$("#<%= dom_id(@comment) %>_form").after(updated_comment).remove(); diff --git a/app/views/likes/_form.html.erb b/app/views/likes/_form.html.erb index eff47d7..19b214a 100644 --- a/app/views/likes/_form.html.erb +++ b/app/views/likes/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: like, class: "d-inline-block") do |form| %> +<%= form_with(model: like, local: false) do |form| %> <%= form.hidden_field :fan_id %> <%= form.hidden_field :photo_id %> diff --git a/app/views/likes/create.js.erb b/app/views/likes/create.js.erb new file mode 100644 index 0000000..38556a6 --- /dev/null +++ b/app/views/likes/create.js.erb @@ -0,0 +1,5 @@ +var change_like = "<%= j(render 'photos/likes', photo: @like.photo) %>"; +$("#<%= dom_id(@like.photo) %>_likes").replaceWith(change_like); + +var change_likes_count = "<%= j(render 'users/likes_count', user: current_user) %>"; +$("#<%= dom_id(current_user) %>_likes_count").replaceWith(change_likes_count); diff --git a/app/views/likes/destroy.js.erb b/app/views/likes/destroy.js.erb new file mode 100644 index 0000000..38556a6 --- /dev/null +++ b/app/views/likes/destroy.js.erb @@ -0,0 +1,5 @@ +var change_like = "<%= j(render 'photos/likes', photo: @like.photo) %>"; +$("#<%= dom_id(@like.photo) %>_likes").replaceWith(change_like); + +var change_likes_count = "<%= j(render 'users/likes_count', user: current_user) %>"; +$("#<%= dom_id(current_user) %>_likes_count").replaceWith(change_likes_count); diff --git a/app/views/photos/_likes.html.erb b/app/views/photos/_likes.html.erb new file mode 100644 index 0000000..c1a27d8 --- /dev/null +++ b/app/views/photos/_likes.html.erb @@ -0,0 +1,15 @@ +
    + <%= pluralize(photo.likes_count, "like") %> + + <% like = current_user.likes.find_by(photo: photo) %> + + <% if like %> + <%= link_to like, class: "btn btn-link", method: :delete, remote: true do %> + + + Un-like + <% end %> + <% else %> + <%= render "likes/form", like: photo.likes.build(fan: current_user) %> + <% end %> +
    diff --git a/app/views/photos/_photo.html.erb b/app/views/photos/_photo.html.erb index dd220b0..27fafa6 100644 --- a/app/views/photos/_photo.html.erb +++ b/app/views/photos/_photo.html.erb @@ -12,34 +12,20 @@ <% end %> - <%= link_to photo, method: :delete, class: "btn btn-link btn-sm text-muted" do %> <% end %> <% end %> - + <%= image_tag photo.image, class: "img-fluid" %> - +
    -
    - <%= pluralize(photo.likes_count, "like") %> - <% like = current_user.likes.find_by(photo: photo) %> - - <% if like %> - <%= link_to like, class: "btn btn-link", method: :delete do %> - - - Un-like - <% end %> - <% else %> - <%= render "likes/form", like: photo.likes.build(fan: current_user) %> - <% end %> -
    + <%= render 'photos/likes', photo: photo %>
    <% unless policy(photo).show? %> @@ -47,7 +33,6 @@ <% end %>
    -

    <%= photo.caption %>

    @@ -57,8 +42,8 @@ <% photo.comments.default_order.each do |comment| %> <%= render "comments/comment", comment: comment %> <% end %> - - <%= render "comments/form", comment: photo.comments.build %> + + <%= render "comments/form", comment: photo.comments.build, context_id: "new_comment" %> <% end %> diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index fc60fef..b634811 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -11,7 +11,7 @@ <% if user_signed_in? %>