-
Notifications
You must be signed in to change notification settings - Fork 0
Dashboard
#Dashboard
This page describes the functionality of the User and Admin dashboard.
The document contains a description of the code from:
- dashboard_controller
- dashboard_helper
- user.html.erb
- admin.html.erb
##Dashboard Controller
class DashboardController < ApplicationController
###Before filters
before_filter :authenticate_user!
This line calls a method to authenticate if a user is logged in correctly before the controller loads the page.
###Methods
####User def user @articleslimit = current_user.articles.order("articles.created_at desc").limit(4) @articlesall = current_user.articles.order("articles.created_at desc")
respond_to do |format|
format.html
format.json { render json: @articlesall }
end
end
The code for 'def user' takes the articles from the database to put them on the page.
@articleslimit = current_user.articles.order("articles.created_at desc").limit(4)
Within the 'def user'; this line creates a variable to use on the dashboard view page user.html.erb. The dashboard has a limit of four articles, hence the .limit(4). The list is ordered chronologically by showing the latest articles first.
@articlesall = current_user.articles.order("articles.created_at desc")
The next line lists all the articles that have been uploaded by the user. This list consists of the same content as the line above, but without the limit. The list will be used to show more articles when the user requests for it.
respond_to do |format|
format.html
format.json { render json: @articlesall }
end
The code above needs to be executed in order to be functional. 'format.html' renders a view page for the user's articles. 'format.json' renders a json file that contains the content for the articles.
####Admin
def admin
if current_user.admin
@falsearticles = Article.find(:all, :conditions => { :accepted => false })
else
redirect_to '/dashboard/user', notice: 'U bent geen admin'
end
end
This code authenticates if the current user is also an admin. An admin is able to monitor new articles.
if current_user.admin
@falsearticles = Article.find(:all, :conditions => { :accepted => false })
The dashboard page shows the articles that haven't been accepted when the user is an admin. The first line authenticates if the user is an Admin. The second line gets the new articles that aren't accepted yet.
redirect_to '/dashboard/user', notice: 'U bent geen admin'
end
When the current user is not an Admin, the page redirects to the regular user dashboard. The user gets a notice saying the user is not an admin.
##Dashboard_helper
module DashboardHelper
####Render_userpic
def render_userpic
if current_user.avatar_url(:thumb) === nil
default_url
else
current_user.avatar_url(:thumb)
end
end
The method above puts a placeholder avatar image on the dashboard's view page when the user didn't upload an avatar.
if current_user.avatar_url(:thumb) === nil
default_url
The if statement checks if the user avatar has no value in the database. If there is no avatar, the default_url gets displayed. A detailed explanation of the default_url can be found in the uploader page.
else
current_user.avatar_url(:thumb)
end
The else statement gets triggered when there is an uploaded avatar. The method gets the avatar from the database to display in the view.
##User.html.erb
<h1>
Welkom <%= current_user.name %>
</h1>
The header of the dashboard's view page displays 'Welcome' and the name of the user that is logged in.
<div class="avatar">
<%= image_tag render_userpic %>
</div>
The avatar div uses the render_userpic method from the dashboard_helper. It shows the uploaded avatar or a placeholder image.
<% userarticlenumber = 0 %>
The article block starts by defining the number of the article that will be shown.
<% @articleslimit.each do |article| %>
The @articleslimit.each do |article| uses the content of the articles from the rendered json file. The code starts a loop that each loaded article uses.
<img class="article_tile" src="<%= article.picture_url(:thumb) %>" alt="images">
Article_tile is an image class that shows the image connected to the article.
<h3><%= article.title %></h3>
The title of an article gets displayed in the header of each individual article_tile.
<div class="text_wrapper">
<%= truncate(article.content, :length => 270, :separator => ' ') %>
</div>
The actual content or text of the article is displayed in the text wrapper. Truncate monitors the length of the text. When there are more than 270 characters in an article, it gets cut off to fit the article_tile in a decent manner.
<button class="article-index-button" id="<%= userarticlenumber %>">Bekijken <div></div> </button>
The article opens in a popup screen when the article-index-button is clicked.
<%= article.created_at %>
<%= link_to 'Show', article %>
<%= link_to 'Edit', edit_article_path(article) %>
<%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %>
The code above puts the date of creation in the content, as well as links to show, edit and destroy the article. The link to destroy an article is secured by a popup that asks the user to confirm the choice to destroy the article.
##Admin.html.erb
<% @falsearticles.each do |article| %>
@falsearticles gets all articles from the database that aren't accepted by the Admin. The each loop affects each individual loaded article.
<img class="article_tile" src="<%= article.picture_url(:thumb) %>" alt="images">
The article_tile contains the image that is connected to the article.
<div class="text_wrapper">
<h3><%= article.title %></h3>
<div class="article_text">
<%= truncate(article.content, :length => 150, :separator => ' ') %>
</div>
The text_wrapper contains the text of the article. 'Truncate' keeps the amount of characters below 150. The end of the text gets replaced with '...'.
<%= article.created_at %><br />
The date of creation is shown in the article content.
<%= link_to 'Bekijken', article %>
<%= link_to 'Bewerken', edit_article_path(article) %>
<%= link_to 'Verwijderen', article, method: :delete, data: { confirm: 'Dit artikel verwijderen?' } %>
<%= link_to "Goedkeuren", accept_article_path(article), method: :put %>
There are links for the Admin to open the article, edit, remove and to accept the articles.