-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c3524d
commit 533fb1c
Showing
18 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
class NewsController < ApplicationController | ||
skip_before_action :verify_authenticity_token | ||
before_action :set_news, only: [:show, :edit, :update, :destroy] | ||
|
||
# GET /news | ||
def index | ||
news = News.all | ||
hash_news = news.map do |c| | ||
{'id' => c.id, | ||
'tag' => c.tag, | ||
'titulo' => c.title, | ||
'link' => c.link, | ||
'icone' => c.icon | ||
} | ||
end | ||
if !hash_news.empty? | ||
render status: 200, json: { | ||
noticias: hash_news, | ||
}.to_json | ||
else | ||
render status: 400, json: { | ||
mensagem: "Não existe notícias.", | ||
}.to_json | ||
end | ||
end | ||
|
||
# GET /news/1 | ||
def show | ||
respond_to do |format| | ||
if @news | ||
format.json { render :show, status: :ok, location: @news } | ||
else | ||
format.json { render json: {"mensagem": "Essa notícia não existe!"}, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# POST /news | ||
def create | ||
@news = News.new(news_params) | ||
if @news.save | ||
render status: 200, json: { | ||
mensagem: "A notícia foi criada com sucesso!", | ||
}.to_json | ||
else | ||
render status: 400, json: { | ||
mensagem: "Não foi possível criar a notícia.", | ||
}.to_json | ||
end | ||
end | ||
|
||
# PATCH/PUT /news/1 | ||
def update | ||
respond_to do |format| | ||
if @news | ||
if @news.update(news_params) | ||
format.json { render :show, status: :ok, location: @news } | ||
else | ||
format.json { render json: @news.errors, status: :unprocessable_entity } | ||
end | ||
else | ||
format.json { render json: {"mensagem": "Essa notícia não existe!"}, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /news/1 | ||
def destroy | ||
respond_to do |format| | ||
if @news | ||
if @news.destroy | ||
format.json { render json: {"mensagem": "Notícia foi excluída com sucesso!"}, status: :ok } | ||
else | ||
format.json { render json: @news.errors, status: :unprocessable_entity } | ||
end | ||
else | ||
format.json { render json: {"mensagem": "Essa notícia não existe!"}, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
private | ||
def set_news | ||
begin | ||
@news = News.find(params[:id]) | ||
rescue | ||
@new = nil | ||
end | ||
end | ||
|
||
def news_params | ||
params.require(:news).permit(:tag, :title, :link, :icon) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module NewsHelper | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class News < ApplicationRecord | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<%= form_with(model: news, local: true) do |form| %> | ||
<% if news.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(news.errors.count, "error") %> prohibited this news from being saved:</h2> | ||
|
||
<ul> | ||
<% news.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= form.label :tag %> | ||
<%= form.text_field :tag %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= form.label :title %> | ||
<%= form.text_field :title %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= form.label :link %> | ||
<%= form.text_field :link %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= form.label :icon %> | ||
<%= form.text_field :icon %> | ||
</div> | ||
|
||
<div class="actions"> | ||
<%= form.submit %> | ||
</div> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! news, :id, :tag, :title, :link, :icon, :created_at, :updated_at | ||
json.url news_url(news, format: :json) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Editing News</h1> | ||
|
||
<%= render 'form', news: @news %> | ||
|
||
<%= link_to 'Show', @news %> | | ||
<%= link_to 'Back', news_index_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>News</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Tag</th> | ||
<th>Title</th> | ||
<th>Link</th> | ||
<th>Icon</th> | ||
<th colspan="3"></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @news.each do |news| %> | ||
<tr> | ||
<td><%= news.tag %></td> | ||
<td><%= news.title %></td> | ||
<td><%= news.link %></td> | ||
<td><%= news.icon %></td> | ||
<td><%= link_to 'Show', news %></td> | ||
<td><%= link_to 'Edit', edit_news_path(news) %></td> | ||
<td><%= link_to 'Destroy', news, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<br> | ||
|
||
<%= link_to 'New News', new_news_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @news, partial: 'news/news', as: :news |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>New News</h1> | ||
|
||
<%= render 'form', news: @news %> | ||
|
||
<%= link_to 'Back', news_index_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<p> | ||
<strong>Tag:</strong> | ||
<%= @news.tag %> | ||
</p> | ||
|
||
<p> | ||
<strong>Title:</strong> | ||
<%= @news.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>Link:</strong> | ||
<%= @news.link %> | ||
</p> | ||
|
||
<p> | ||
<strong>Icon:</strong> | ||
<%= @news.icon %> | ||
</p> | ||
|
||
<%= link_to 'Edit', edit_news_path(@news) %> | | ||
<%= link_to 'Back', news_index_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "news/news", news: @news |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateNews < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :news do |t| | ||
t.string :tag | ||
t.string :title | ||
t.string :link | ||
t.string :icon | ||
|
||
t.timestamps | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'test_helper' | ||
|
||
class NewsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@news = news(:one) | ||
end | ||
|
||
test "should get index" do | ||
get news_index_url | ||
assert_response :success | ||
end | ||
|
||
test "should get new" do | ||
get new_news_url | ||
assert_response :success | ||
end | ||
|
||
test "should create news" do | ||
assert_difference('News.count') do | ||
post news_index_url, params: { news: { icon: @news.icon, link: @news.link, tag: @news.tag, title: @news.title } } | ||
end | ||
|
||
assert_redirected_to news_url(News.last) | ||
end | ||
|
||
test "should show news" do | ||
get news_url(@news) | ||
assert_response :success | ||
end | ||
|
||
test "should get edit" do | ||
get edit_news_url(@news) | ||
assert_response :success | ||
end | ||
|
||
test "should update news" do | ||
patch news_url(@news), params: { news: { icon: @news.icon, link: @news.link, tag: @news.tag, title: @news.title } } | ||
assert_redirected_to news_url(@news) | ||
end | ||
|
||
test "should destroy news" do | ||
assert_difference('News.count', -1) do | ||
delete news_url(@news) | ||
end | ||
|
||
assert_redirected_to news_index_url | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
tag: MyString | ||
title: MyString | ||
link: MyString | ||
icon: MyString | ||
|
||
two: | ||
tag: MyString | ||
title: MyString | ||
link: MyString | ||
icon: MyString |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'test_helper' | ||
|
||
class NewsTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require "application_system_test_case" | ||
|
||
class NewsTest < ApplicationSystemTestCase | ||
setup do | ||
@news = news(:one) | ||
end | ||
|
||
test "visiting the index" do | ||
visit news_url | ||
assert_selector "h1", text: "News" | ||
end | ||
|
||
test "creating a News" do | ||
visit news_url | ||
click_on "New News" | ||
|
||
fill_in "Icon", with: @news.icon | ||
fill_in "Link", with: @news.link | ||
fill_in "Tag", with: @news.tag | ||
fill_in "Title", with: @news.title | ||
click_on "Create News" | ||
|
||
assert_text "News was successfully created" | ||
click_on "Back" | ||
end | ||
|
||
test "updating a News" do | ||
visit news_url | ||
click_on "Edit", match: :first | ||
|
||
fill_in "Icon", with: @news.icon | ||
fill_in "Link", with: @news.link | ||
fill_in "Tag", with: @news.tag | ||
fill_in "Title", with: @news.title | ||
click_on "Update News" | ||
|
||
assert_text "News was successfully updated" | ||
click_on "Back" | ||
end | ||
|
||
test "destroying a News" do | ||
visit news_url | ||
page.accept_confirm do | ||
click_on "Destroy", match: :first | ||
end | ||
|
||
assert_text "News was successfully destroyed" | ||
end | ||
end |