Roy Zinn

Joined

1,180 Experience
3 Lessons Completed
1 Question Solved

Activity

Posted in Uploading a file from remote url using shrine?

OK,
The way to go is using shrine with the remote_url plugin which does exactly what I was looking for
If anyone cares to see the solution in code, i'll be happy to post it here.

Cheers

Posted in Uploading a file from remote url using shrine?

Hey Jacob,
Thanks for your reply and sorry for the delayed response, I somehow missed your answer.
I don't have the code yet, was just wondering what would be the "right" way to do it.
From all the readings that I did, nothing seems to give a simple solution to "copy" from random URL (e.g., I am getting a link to a downloadable PDF) to S3.
The best thing I found is to use the probably use the Down gem which is included to stream the remote file into a temp one (as you suggested, just replace the File.open as it's not opening remote file) and then attach it.

I was pretty sure that there's something much simpler to copy a file from some URL to S3.
I guess there is another solution, I'll post it here if I find one :-)

Posted in Uploading a file from remote url using shrine?

Hi,
I need to upload a remote file to my S3 bucket (e.g., I get a link to a pdf e-ticket and I need to upload it from its location). In addition (as a bonus) what's the best way to allow users to download these pdf's from my s3 bucket? is there a way to bundle few pdfs into one download (e.g., allow a user to download multiple stored e-tickets with one button click)

I looked into shrine and it's plugin ecosystem but I wasn't able to tell which one to use...

Any idea how to accomplish this? shrine is not mandatory, but would be nice. I saw that paperclip has something with remote url but I prefer Shrine as I want to use it for other tasks as well.

Thanks,
Roy

Thanks a lot for taking the time looking at this, highly appreciated!

Oh, and BTW, by looking at the suggested code (I know you don't have the full picture so it's very hard) I see that in the Answer class you're creating in the container a new AnswerList (@answerList = new DWiz.AnswerList @answer.find("[data-behavior='answers-list']") which will end up having answerlist item for each answer that doesn't make sense.
The AnswerList should be the container of all Answer

Cheers

Hi Chris.
Thanks for the prompt reply. This is what I actually started with which is bringing back the issue of new answers loaded through AJAX.
If I just "push" those loaded new answers into the DOM. there won't be any class for them and no "parent" to listen to the click events on them (this is why I moved things up to a parent container). Another solution for that would probably be to create those classes when AJAX completes the loading of the new answers (i.e., load new answer and then create a new Answer class for each)
In the meantime, I did something a bit different: In my AnswerList, I manage classes that were already created (keeping track on them with an array). this way, if nothing is clicked, no classes are instantiated and if it is clicked, I push them to array and use them in subsequent clicks. this way it also takes care of newly loaded items. The problem with this approach is that it still feels that the AnswerList knows too much about classes it shouldn't.
BTW - parentsUntil was the wrong one, I changed to closest ;)

Hi Chris.
Below is what I came up with. basically, I have an answers container which has list of answers. each answer is a container for the comments on it, so hierarchy is: answer-list => answer => comments-list => comments

class DWiz.AnswerList
  constructor: (@answerList) ->
    @setEvents()

  setEvents: =>
    @answerList.on 'click', "[data-behavior='show-comments']", (e) => @toggleCommentsAndSetFocus(e, false)
    @answerList.on 'click', "[data-behavior='new-comment-link']", (e) => @toggleCommentsAndSetFocus(e, true)

  toggleCommentsAndSetFocus: (e, setFocus) =>
    answer = $(e.target).parentsUntil('[data-behavior="answer"]')
    commentList = new DWiz.CommentList $(answer).find("[data-behavior='comments']")
    comment = new DWiz.Comment $(answer).find("[data-behavior='comment']")
    commentList.toggleComments()
    comment.toggleComment()
    if setFocus
      newCommentLinkTopPos = $(answer).find("[data-behavior='new-comment-link']").offset().top
      comment.setFocus(newCommentLinkTopPos)

$(document).on "page:change", (e) ->
  answerButtons = $.map $("[data-behavior='new-answer-btn']"), (item, i) -> new DWiz.NewAnswerBtn(item)
 answersList = new DWiz.AnswerList $("[data-behavior='answers-list']")

to clarify some more, in the answers I have two links (show comments & comment), they both do quite the same (open the loaded comments and optionally sets focus on new comment box)
below is the code for comments:

class DWiz.CommentList
  constructor: (el) ->
    @container = $(el)

  toggleComments: =>
    @container.collapse('toggle')


class DWiz.Comment
  constructor: (el) ->
    @container = $(el)
    @textInput = @container.find("[data-behavior='comment-input']")
    @submitBtn = @container.find("[data-behavior='comment-submit']")
    @setEvents()

  setEvents: =>
    @submitBtn.on 'click', @handleSubmit

  toggleComment: =>
    @container.toggleClass('list-group-item').collapse('toggle')

  setFocus: (elPos) =>
    if @container.hasClass 'list-group-item'
      dist = @textInput.offset().top - elPos
      $('body').animate { scrollTop: elPos + dist }, 500
      @textInput.focus()

  handleSubmit: (e) =>
    e.preventDefault()
    comment = @textInput.val()
    return unless comment
    data = comment: {}
    data['comment']['answer_id'] = @textInput.data('answer')
    data['comment']['comment'] = comment
    $.post '/comments', data

As you can see, the handler for showing the comments is on the parent (AnswerList) which creates an instance of comment. The problem with that is that if I click show comments multiple times, I end up having multiple instances of the comment class which when I try to submit a comment, it will be submitted as the number of instances...
pretty long but I hope it clarifies.
Cheers

Thanks Chris.
It implies quite a lot of changes now. it makes me feel that making classes now while most event listeners are on the parent is not the "cleanest" way.
I was wondering what would be your approach to the following scenario:

  1. I have a Q&A site for which I have answers container
  2. for each answer I have comment list (quite similar to FB threads with comments
  3. I load new answers with comments when scrolling down

The latter is loaded with AJAX and are inserted into the DOM with create.js.erb (not returning JSON and handling it on the client right now)
Those loaded answers should have functionality of expending comment list etc so as you wrote above, the listeners seems to be on the container which is loaded at the beginning.

Question is, how would you model it CS classes in this case (I used to have class for almost any object: answer, comment, answerList, commentList etc...)

Thanks,
Roy

Hi,
After watching the screencast about extracting JS code to CS classes, I tried to follow the same pattern.
The problem I am facing is that the page loads new items through AJAX and in order for them to respond to clicks like other object, i'm using the page:update event

$(document).on "page:update", ->
  DWiz.newAnswerBtns = $.map $("[data-behavior='new-answer-btn']"), (item, i) ->
    new DWiz.NewAnswerBtn(item)

the classes above have their events handling (listening to on 'click' etc).
The problem is that on every load of new items, the events are registered again and hence fire multiple times.
I am also using the jquery-turbolinks gem but it's not helping.
Any idea on how to approach this?
Thanks in advance,
Roy

Ah, awesome, thanks

This cast is awesome, thanks. On Github, when you start typing the emoji name or mention (i.e., when you type ":hea..." or "@..." it pops up suggestion engine that allows you to choose what you want - a kind of auto-completion. any idea on how to implement something like that? I earched the documentation but couldn't find anything. of-course, the idea is to get some "real time" feedback and not just after you "pot" the mesage.
Thanks

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.