-
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.
Merge pull request #249 from TreinaDev/feat/anuncios-assinatura
[ FEAT ] Anúncios no feed de usuários free
- Loading branch information
Showing
20 changed files
with
358 additions
and
4 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,42 @@ | ||
class AdvertisementsController < ApplicationController | ||
before_action :authenticate_user!, only: %i[index new create show] | ||
before_action :redirect_unauthorized_user, only: %i[index new create show] | ||
|
||
def index | ||
@advertisements = Advertisement.all | ||
end | ||
|
||
def new | ||
@advertisement = Advertisement.new | ||
end | ||
|
||
def create | ||
@advertisement = current_user.advertisements.build(ads_params) | ||
|
||
redirect_to advertisement_path(@advertisement), notice: t('.success') if @advertisement.save | ||
end | ||
|
||
def show | ||
@advertisement = Advertisement.find(params[:id]) | ||
end | ||
|
||
def update | ||
@advertisement = Advertisement.find(params[:id]) | ||
@advertisement.update(view_count: @advertisement.view_count + 1) | ||
|
||
url = @advertisement.link | ||
url = "http://#{url}" unless url.start_with?('http://', 'https://') | ||
|
||
redirect_to url, allow_other_host: true | ||
end | ||
|
||
private | ||
|
||
def ads_params | ||
params.require(:advertisement).permit(:title, :link, :display_time, :image) | ||
end | ||
|
||
def redirect_unauthorized_user | ||
redirect_to root_path unless current_user.admin? | ||
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 |
---|---|---|
|
@@ -8,6 +8,6 @@ def index | |
|
||
return if @followed_posts.any? | ||
|
||
@posts = Post.get_sample(3) | ||
@posts = Post.get_sample(10) | ||
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,14 @@ | ||
class Advertisement < ApplicationRecord | ||
belongs_to :user | ||
has_one_attached :image | ||
validates :title, :link, presence: true | ||
validates :link, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]) | ||
|
||
def self.displayed | ||
where.not(id: Advertisement.select(&:expired?)).sample(1) | ||
end | ||
|
||
def expired? | ||
(created_at + display_time.days) < Time.zone.now | ||
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
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 @@ | ||
<div id="advertisement_<%= advertisement.id %>" class="text-center mb-3"> | ||
<%= button_to advertisement_path(advertisement), method: :patch, | ||
class: 'advertisement rounded btn btn-light w-100', data: { turbo: false }, id: 'to-ad' do %> | ||
<p><%= advertisement.title %></p> | ||
<p><%= image_tag advertisement.image, width: '400rem', alt: 'advertisement' if advertisement.image.present? %></p> | ||
<% end %> | ||
</div> |
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,11 @@ | ||
<%= link_to t('new_ad_btn'), new_advertisement_path, class: 'btn btn-primary mt-3' %> | ||
|
||
<% @advertisements.each do |ad| %> | ||
|
||
<ul class="list-group mt-3"> | ||
<li class="list-group-item my-2"> | ||
<%= link_to ad.title, advertisement_path(ad) %> | ||
</li> | ||
</ul> | ||
|
||
<% 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,23 @@ | ||
<%= form_with model: @advertisement, class: 'mt-3' do |f| %> | ||
<div class="form-group"> | ||
<%= f.label :title, class: 'form-label' %> | ||
<%= f.text_field :title, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= f.label :link, class: 'form-label' %> | ||
<%= f.text_field :link, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= f.label :display_time, class: 'form-label' %> | ||
<%= f.number_field :display_time, class: 'form-control', min: 1 %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= f.label :image, class: 'form-label' %> | ||
<%= f.file_field :image, class: 'form-control' %> | ||
</div> | ||
|
||
<%= f.submit t('save_btn'), class: 'btn btn-primary mt-3' %> | ||
<% 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,14 @@ | ||
<%= link_to t('return_btn'), advertisements_path, class: 'btn btn-secondary mb-3' %> <br> | ||
|
||
<div class="container"> | ||
<h4><%= @advertisement.title %></h4> | ||
|
||
<%= image_tag @advertisement.image, width: 400, class: 'mb-3' if @advertisement.image.present? %> | ||
|
||
<p><strong>Tempo de exibição (em dias):</strong> <%= @advertisement.display_time %></p> | ||
|
||
<p><strong>Link:</strong> <%= @advertisement.link %></p> | ||
|
||
<p><strong>Cliques:</strong> <%= @advertisement.view_count %></p> | ||
</div> | ||
|
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
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,16 @@ | ||
pt-BR: | ||
activerecord: | ||
models: | ||
advertisement: | ||
one: Anúncio | ||
other: Anúncios | ||
attributes: | ||
advertisement: | ||
title: Título | ||
link: Link | ||
image: Imagem | ||
view_count: Visualizações | ||
display_time: Prazo (em dias) | ||
advertisements: | ||
create: | ||
success: Anúncio criado com sucesso |
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,13 @@ | ||
class CreateAdvertisements < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :advertisements do |t| | ||
t.string :link, null: false | ||
t.integer :display_time, default: 0 | ||
t.integer :view_count, default: 0 | ||
t.string :title, null: false | ||
t.references :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
FactoryBot.define do | ||
factory :advertisement do | ||
image { nil } | ||
link { 'https://www.campuscode.com' } | ||
display_time { 7 } | ||
title { Faker::Lorem.paragraph } | ||
user | ||
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 'rails_helper' | ||
|
||
RSpec.describe Advertisement, type: :model do | ||
describe '#valid?' do | ||
it 'Deve ter título' do | ||
advertisement = Advertisement.new | ||
advertisement.valid? | ||
expect(advertisement.errors[:title]).to include('não pode ficar em branco') | ||
end | ||
|
||
context 'Formato do link' do | ||
it 'Deve ter link' do | ||
advertisement = Advertisement.new | ||
advertisement.valid? | ||
expect(advertisement.errors[:link]).to include('não pode ficar em branco') | ||
end | ||
|
||
it 'deve ser válido com formato correto' do | ||
user = create(:user) | ||
ad = Advertisement.new(title: 'Venha ser Dev', user:, link: 'https://www.campuscode.com') | ||
expect(ad).to be_valid | ||
end | ||
|
||
it 'deve ser inválido com formato incorreto' do | ||
user = create(:user) | ||
ad = Advertisement.new(title: 'Venha ser Dev', user:, link: 'campus##code.com') | ||
expect(ad).not_to be_valid | ||
end | ||
end | ||
end | ||
|
||
describe '#displayed' do | ||
it 'deve retornar apenas anúncios não expirados' do | ||
create(:advertisement, created_at: 6.days.ago, display_time: 7) | ||
|
||
ads = Advertisement.displayed | ||
|
||
expect(ads.size).to eq 1 | ||
end | ||
|
||
it 'não deve retornar anúncios expirados' do | ||
create(:advertisement, created_at: 2.days.ago, display_time: 1) | ||
|
||
ads = Advertisement.displayed | ||
|
||
expect(ads.size).to eq 0 | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
spec/requests/advertisements/user_visits_advertisements_page_spec.rb
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,18 @@ | ||
require 'rails_helper' | ||
|
||
describe 'Usuário visita listagem de anúncios' do | ||
it 'e não é admin' do | ||
user = create(:user) | ||
|
||
login_as user | ||
get advertisements_path | ||
|
||
expect(response).to redirect_to root_path | ||
end | ||
|
||
it 'e não está autenticado' do | ||
get advertisements_path | ||
|
||
expect(response).to redirect_to new_user_session_path | ||
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,39 @@ | ||
require 'rails_helper' | ||
|
||
describe 'Administrador cadastra um anúncio' do | ||
it 'com sucesso' do | ||
admin = create(:user, role: 'admin') | ||
|
||
login_as admin | ||
visit root_path | ||
click_button class: 'dropdown-toggle' | ||
within 'nav' do | ||
click_on 'Anúncios' | ||
end | ||
click_on 'Criar Anúncio' | ||
fill_in 'Título', with: 'Buscador' | ||
fill_in 'Link', with: 'https://www.google.com' | ||
fill_in 'Prazo (em dias)', with: 7 | ||
attach_file('Imagem', Rails.root.join('spec/support/assets/images/test_image.png')) | ||
click_on 'Salvar' | ||
|
||
ad = Advertisement.last | ||
expect(page).to have_content 'Anúncio criado com sucesso' | ||
expect(page).to have_current_path advertisement_path(ad) | ||
expect(page).to have_content 'Buscador' | ||
expect(page).to have_css('img[src*="test_image.png"]') | ||
expect(page).to have_link 'Voltar', href: advertisements_path | ||
end | ||
|
||
it 'e não é admin' do | ||
user = create(:user) | ||
|
||
login_as user | ||
visit root_path | ||
click_button class: 'dropdown-toggle' | ||
|
||
within 'nav' do | ||
expect(page).not_to have_link 'Anúncios' | ||
end | ||
end | ||
end |
Oops, something went wrong.