Skip to content

Commit

Permalink
Merge pull request #249 from TreinaDev/feat/anuncios-assinatura
Browse files Browse the repository at this point in the history
[ FEAT ] Anúncios no feed de usuários free
  • Loading branch information
caiquedv authored Feb 16, 2024
2 parents c981bd0 + 27aed0d commit 05a9539
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 4 deletions.
42 changes: 42 additions & 0 deletions app/controllers/advertisements_controller.rb
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
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def index

return if @followed_posts.any?

@posts = Post.get_sample(3)
@posts = Post.get_sample(10)
end
end
14 changes: 14 additions & 0 deletions app/models/advertisement.rb
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
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User < ApplicationRecord
has_many :professional_infos, through: :profile
has_many :education_infos, through: :profile
has_many :invitation_requests, through: :profile
has_many :advertisements, dependent: :destroy
has_one :subscription, dependent: :destroy

enum role: { user: 0, admin: 10 }
Expand Down
7 changes: 7 additions & 0 deletions app/views/advertisements/_advertisement.html.erb
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>
11 changes: 11 additions & 0 deletions app/views/advertisements/index.html.erb
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 %>
23 changes: 23 additions & 0 deletions app/views/advertisements/new.html.erb
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 %>
14 changes: 14 additions & 0 deletions app/views/advertisements/show.html.erb
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>

5 changes: 4 additions & 1 deletion app/views/posts/_listing.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% posts.each do |post| %>
<% posts.each_with_index do |post, index| %>
<div class="feed-post mb-3 p-3 rounded row bg-white border" id="post-list">
<%= link_to post, class: "text-decoration-none link-dark col-md-10" do %>
<div class="card-body">
Expand Down Expand Up @@ -37,4 +37,7 @@
</div>
<% end %>
</div>
<% if current_user.subscription.inactive? && (index+1) % 5 == 0 && Advertisement.any? %>
<%= render partial: 'advertisements/advertisement', locals: { advertisement: Advertisement.displayed.first } %>
<% end %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<li class="dropdown-item" href="#">
<%= link_to t('settings.index.settings'), profile_settings_path(current_user.profile), class: 'nav-link' %>
</li>
<% if current_user.admin? %>
<li class="dropdown-item">
<%= link_to t('ads_btn'), advertisements_path, class: 'nav-link' %>
</li>
<% end %>
<div class="dropdown-divider"></div>
<li class="dropdown-item" href="#">
<%= link_to Subscription.model_name.human, subscriptions_path, class: 'nav-link text-primary' %>
Expand Down
4 changes: 3 additions & 1 deletion config/locales/buttons.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pt-BR:
unpin_btn: Desafixar
return_btn: Voltar
send_btn: Enviar
publish_btn: Publicar
publish_btn: Publicar
ads_btn: Anúncios
new_ad_btn: Criar Anúncio
16 changes: 16 additions & 0 deletions config/locales/models/advertisement.pt-BR.yml
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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

root to: 'home#index'

resources :advertisements, only: %i[index show new create update]

resources :searches, only: %i[index]
resources :invitations, only: %i[index show] do
patch 'decline', on: :member
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240215195415_create_advertisements.rb
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
14 changes: 13 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions spec/factories/advertisements.rb
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
49 changes: 49 additions & 0 deletions spec/models/advertisement_spec.rb
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
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
39 changes: 39 additions & 0 deletions spec/system/advertisements/admin_create_ad_spec.rb
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
Loading

0 comments on commit 05a9539

Please sign in to comment.