-
Notifications
You must be signed in to change notification settings - Fork 0
Favorites
The favorites system allows users to save articles to their favorites. This system also provides the basic building blocks for trending articles. "Favorites" has it's own model, controllers and added routes.
##Routes Favorites has it's own Resource. It lives in the system under the default (scaffold) parameters.
match "/favorites/add/:article_id" => "favorites#create", :as => :favorite
match "/favorites/destroy/:article_id" => "favorites#destroy"
Unlike the reviews with it's embedding system in the articles. Favorites is separated but is linked to the articles trough match routes. This has no specific good reason other than a creating a nice challenge for the programmers.
##The Favorites Model The article model is wrapped in a class. The class defines this model in to Favorites range and the active record tells the system that it's dealing with base (in this case a base-model)
Let's break this model down to a smaller level.
###Accessible
attr_accessible :article_id, :user_id
The database linked to the favorites only has 3 columns. The default ID, the linked article_id and the linked user_id. the two non-default columns are made accessible in the model.
###Relations
belongs_to :article, :counter_cache => true
belongs_to :user
Each favorite belongs to one article and one user. The countercache option adds a +1 when a new favorite is created and a -1 when a favorite is destroyed to favorites_counter in the Articles model.
##The Favorites Controller
The controller is the small center of this function in the app. The controller holds the information to adding and removing favorites (safely and secure).
###Index
def index
@favorites = Favorite.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @favorites }
end
end
The index is a temporary file for our server. It allows the developers of the railsapp to view a graphical output of the database.
####Variables
@favorites = Favorite.all
@favorites
calls all favorites from the database and puts them in an array.
####Responses Each response is defined as a format.
format.html # index.html.erb
format.json { render json: @favorites }
The server renders a html page found in app/views/favorites
and a Json with the collection of all favorites found at /favorites.json
in the Routes.
###Create The create method adds a new row to the table with 2 predefined things: the current-user's ID and the current Article's ID. The user ID is grabbed from the active record and the session and the Article ID from the routing of the page `/articles/[id]/favorite/create'
####Tasks
current_user.favorites.create(:article_id => params[:article_id])
The current_user makes a new favorite where article_id is defined from the variables of the current route.
####Respons
render :layout => false
No layout is rendered. Hence this command is completely ignored by the server the right code should go into Respond_to
###Show This method has no options in the controller.
###Destroy
def destroy
current_user.favorites.where(:article_id => params[:article_id]).first.destroy
render :layout => false
end
Destroy removes a favorite from the user and in a chain reaction lowers the value of the articles Favcount by 1
####Tasks
current_user.favorites.where(:article_id => params[:article_id]).first.destroy
this line of code finds all the favorites from the logged-in user and filters them by the current article ID. from this Array (with only 1 variable) the first (and only) is selected and destroyed.
render :layout => false
No layout is rendered. Hence this command is completely ignored by the server the right code should go into Respond_to
##Views
Aside from the index page (for developers purposes) Favorites have no views. they are however used in the articles#show
page. Check the Show section in the Articles page
###Index
<% @favorites.each do |favorite| %>
Each favorite from the array is put in a variable called favorite and looped until all favorites have been put.
<%= favorite.user_id %>
<%= favorite.article_id %>
For each favorite the user_id and article_id are called.
<%= link_to 'Show', favorite %>
<%= link_to 'Edit', edit_favorite_path(favorite) %>
<%= link_to 'Destroy', favorite, method: :delete, data: { confirm: 'Are you sure?' } %>
For each favorite a link to the favorite (not working), the edit link and destroy link are viewed these can be used to manually remove a favorite.
<%= link_to 'New Favorite', new_favorite_path %>
At the bottom of the page a link to new favorite is shown with this link new favorites can be manualy added (this link does not work anymore since the 27-05-2013 updates and merges.