mardi 28 juin 2016

Add Methods to Iterated Objects

I have a block that looks similar to this one:

    <% @github_tmp_files.files.each do |file| %>
      <li><%= link_to @github_tmp_files.filename(file.key), @github_tmp_files.download_url(file.key) %></li>
    <% end %>

As you can see in the loop I call two methods with file as the argument:

@github_tmp_files.filename(file.key)
@github_tmp_files.download_url(file.key)

I would prefer to call this two methods like that:

file.filename (should return) @github_tmp_files.filename(file.key)
file.download_url (should return) @github_tmp_files.download_url(file.key)

So that at the end I can write the loop like this:

     <% @github_tmp_files.files.each do |file| %>
      <li><%= link_to file.filename, file.download_url %></li>
    <% end %>

How do I have to change the files method in @github_tmp_files, so that it allows this behaviour? Thanks

#in @github_tmp_files -> Class

def files
   github_bucket.objects(prefix: @folder)
end

Aucun commentaire:

Enregistrer un commentaire