Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: RubySchools/social_network
base: 37600834a5
...
head fork: RubySchools/social_network
compare: ed3d39a8ee
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
22 app/controllers/uploads_controller.rb
@@ -5,7 +5,6 @@ class UploadsController < ApplicationController
#Page where all Uploads are available
def index
@files = Upload.all
-@user = User.find(1)
@title = "Downloads"
respond_to do |format|
format.html # index.html.erb
@@ -81,7 +80,26 @@ def destroy
end
def user_uploads
- @files = current_user.uploads.all
+ @files = current_user.saved_uploads + current_user.uploads
+ end
+
+ def add_to_favorite
+ favourite_upload = UsersUpload.new
+ favourite_upload.user = current_user
+ favourite_upload.upload = Upload.find(params[:id])
+ if favourite_upload.save
+ respond_to do |format|
+ format.html { redirect_to uploads_url }
+ end
+ end
+ end
+
+ def delete_from_favorites
+ @favourite_upload = UsersUpload.where(:upload_id => params[:id],:user_id => current_user.id)
+ @favourite_upload.first.destroy
+ respond_to do |format|
+ format.html { redirect_to my_downloads_url }
+ end
end
View
6 app/models/upload.rb
@@ -4,8 +4,8 @@ class Upload < ActiveRecord::Base
#Relations
belongs_to :user
-# has_many :users
-# has_many :users , :through => :users_uploads
-
+ has_many :users_upload
+ has_many :savers, :through => :users_upload, :source => :user
+
#Validation
end
View
6 app/models/user.rb
@@ -4,10 +4,8 @@ class User < ActiveRecord::Base
#Relations
has_many :uploads
-# has_many :uploads, :through => :users_uploads
-
-
-
+ has_many :users_upload
+ has_many :saved_uploads, :through => :users_upload, :source => :upload
#Validation
View
8 app/models/users_upload.rb
@@ -0,0 +1,8 @@
+class UsersUpload < ActiveRecord::Base
+#Relations
+ belongs_to :user
+ belongs_to :upload
+#Validations
+validates :user_id, :presence => true
+validates :upload_id, :presence => true
+end
View
16 app/views/uploads/index.html.erb
@@ -13,12 +13,20 @@
<tr>
<td><%= file.name %></td>
<td><%= file.description %></td>
- <td><%= link_to "#{file.user.username}", user_path(file.user) %></td>
+ <td><%= link_to "#{file.user.username}", file.user %></td>
<td><%= link_to 'Show', file %></td>
- <td><%= link_to 'Edit', edit_upload_path(file) %></td>
- <!-- <td><%= link_to 'Destroy', file, confirm: 'Are you sure?', method: :delete %></td> -->
- <td><%= link_to 'Destroy', file, method: :delete %></td>
+ <td><%= link_to 'Edit', edit_upload_path(file) %></td>
+ <% if current_user.uploads.include?(file) %>
+ <td><%= link_to 'Destroy', file , method: :delete %></td>
+ <% else if current_user.saved_uploads.include?(file)%>
+ <td>Already_in_favorites</td>
+ <% else %>
+ <td><%= link_to 'add_to_favorite', add_upload_to_favorite_path(file) %></td>
+ <% end %>
+ <% end %>
+
+
</tr>
<% end %>
</table>
View
16 app/views/uploads/user_uploads.html.erb
@@ -8,17 +8,27 @@
<th></th>
<th></th>
</tr>
-
+<% if @files %>
<% @files.each do |file| %>
<tr>
- <td><%= file.name %></td>
+ <td><%= file.name %></td>
<td><%= file.description %></td>
<td><%= link_to 'Show', file %></td>
<td><%= link_to 'Edit', edit_upload_path(file) %></td>
<!-- <td><%= link_to 'Destroy', file, confirm: 'Are you sure?', method: :delete %></td> -->
- <td><%= link_to 'Destroy', file, method: :delete %></td>
+ <% if file.user == current_user %>
+ <td><%= link_to 'Destroy file', file, method: :delete %></td>
+ <% else %>
+ <td><%= link_to 'Remove from favorite', delete_upload_from_favorite_path(file) %></td>
+ <% end %>
</tr>
<% end %>
+<% else %>
+ <tr>
+ <td>No files found for <%= current_user.username %></td>
+ <tr>
+<% end %>
+
</table>
<br />
View
3  config/routes.rb
@@ -9,6 +9,9 @@
match 'logout' => 'user_sessions#destroy', :as => :logout
match 'downloads' => 'uploads#index',:as => :downloads
match 'my_downloads' => 'uploads#user_uploads', :as => :my_downloads
+ match 'add_upload_to_favorite/:id' => 'uploads#add_to_favorite', :as => :add_upload_to_favorite
+ match 'delete_upload_from_favorite/:id' => 'uploads#delete_from_favorites', :as => :delete_upload_from_favorite
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
9 db/migrate/20120227164015_fix_users_uploads.rb
@@ -0,0 +1,9 @@
+class FixUsersUploads < ActiveRecord::Migration
+def change
+ rename_column :users_uploads, :upload_ip, :upload_id
+
+end
+
+ def down
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.