This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"token": "8b7794a62dd440db597b3566f95dc6fb", | |
"id": 1, | |
"name": "Giancarlos Isasi", | |
"email": "giancarlos@devacademy.la" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
# protect_from_forgery with: :null_session | |
protected | |
def authenticate # este método lo usaremos en nuestro controlador Articles para saber que usuario desea crear un articulo. | |
# Tambien nos servira para comprobar si el usuario es el dueño del articulo que desea actualizar o eliminar | |
token_of_params = params[:token] # obtenemos el token que nos pasaran en la petición | |
token = Token.find_by_token(token_of_params) # verificamos que el token exista en la base de datos | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
namespace :api, defaults: { format: "json" } do | |
namespace :v1 do | |
resources :users, only: [:create] # solo usaremos la accion 'create' para agregar nuevos usuarios | |
resources :articles , except: [:new, :edit] # no necesitaremos estas acciones al tratarse de un API | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::ArticlessController < ApplicationController | |
before_action :authenticate, only: [:create, :update, :destroy] # usando el metodo de nuestro applicationController vericamos que el token sea valido y autentificamos al usuario (este sera asigando a nuestra variable @current_user) | |
before_action :set_article, only: [:show ,:update, :destroy] # el tipo metodo para encontrar al articulo, esta demas explicarlo | |
def index | |
@articles = Article.all | |
end | |
def show | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bad | |
resources :comunity do | |
resources :users do | |
resources :articles do | |
resources :comments | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# good | |
resources :comunity do | |
resources :users | |
end | |
resources :users do | |
resources :articles | |
end | |
resources :articles do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :company do | |
resources :departments do | |
resources :employees | |
end | |
end | |
resources :employees |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collection :@users | |
attributes :id, :name, :last_name, :article_id | |
child(:article) { attributes :title, :body } | |
node(:read) { |message| message.read_by?(@user) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# INDEX action | |
it 'show the correct view' do | |
expect(response).to render_template ‘index’ | |
end | |
# SHOW action | |
it 'show the correct view' do | |
expect(response).to render_template ‘show’ | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
feature 'Show the post selected' do | |
scenario 'user want to see the first post' do | |
visit posts_path | |
click_link 'Its my first posts' | |
expect(page).to have_content 'Its my first post!' # Titulo del post mostrado en la web app | |
expect(page).to have_content 'Welcome to my first post guys' # body del post | |
end | |
end |