From 1daaa3b5289bc29f56b8168f4180903fb5d9212a Mon Sep 17 00:00:00 2001 From: Paulo Henrique de Meneses Oliveira Date: Thu, 15 Feb 2024 18:23:24 -0300 Subject: [PATCH 1/7] =?UTF-8?q?feat/cria=20p=C3=A1gina=20de=20cadastro=20d?= =?UTF-8?q?e=20an=C3=BAncios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Caique Andrade Co-authored-by: Danilo Martins --- app/controllers/advertisements_controller.rb | 32 +++++++++++++ app/models/advertisement.rb | 4 ++ app/models/user.rb | 1 + app/views/advertisements/index.html.erb | 1 + app/views/advertisements/new.html.erb | 23 ++++++++++ app/views/advertisements/show.html.erb | 6 +++ app/views/shared/_navbar.html.erb | 5 +++ config/locales/buttons.pt-BR.yml | 4 +- config/locales/models/advertisement.pt-BR.yml | 16 +++++++ config/routes.rb | 2 + .../20240215195415_create_advertisements.rb | 13 ++++++ db/schema.rb | 18 ++++++-- spec/factories/advertisements.rb | 10 +++++ spec/models/advertisement_spec.rb | 5 +++ .../user_visits_advertisements_page_spec.rb | 18 ++++++++ .../advertisements/admin_create_ad_spec.rb | 45 +++++++++++++++++++ 16 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 app/controllers/advertisements_controller.rb create mode 100644 app/models/advertisement.rb create mode 100644 app/views/advertisements/index.html.erb create mode 100644 app/views/advertisements/new.html.erb create mode 100644 app/views/advertisements/show.html.erb create mode 100644 config/locales/models/advertisement.pt-BR.yml create mode 100644 db/migrate/20240215195415_create_advertisements.rb create mode 100644 spec/factories/advertisements.rb create mode 100644 spec/models/advertisement_spec.rb create mode 100644 spec/requests/advertisements/user_visits_advertisements_page_spec.rb create mode 100644 spec/system/advertisements/admin_create_ad_spec.rb diff --git a/app/controllers/advertisements_controller.rb b/app/controllers/advertisements_controller.rb new file mode 100644 index 0000000..34c3094 --- /dev/null +++ b/app/controllers/advertisements_controller.rb @@ -0,0 +1,32 @@ +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; end + + def new + @advertisement = Advertisement.new + end + + def create + @advertisement = current_user.advertisements.build(ads_params) + + if @advertisement.save + redirect_to advertisement_path(@advertisement), notice: t('.success') + end + end + + def show + @advertisement = Advertisement.find(params[:id]) + 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 diff --git a/app/models/advertisement.rb b/app/models/advertisement.rb new file mode 100644 index 0000000..c59ef03 --- /dev/null +++ b/app/models/advertisement.rb @@ -0,0 +1,4 @@ +class Advertisement < ApplicationRecord + belongs_to :user + has_one_attached :image +end diff --git a/app/models/user.rb b/app/models/user.rb index 96a106d..c3d4c2e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 enum role: { user: 0, admin: 10 } diff --git a/app/views/advertisements/index.html.erb b/app/views/advertisements/index.html.erb new file mode 100644 index 0000000..3225a52 --- /dev/null +++ b/app/views/advertisements/index.html.erb @@ -0,0 +1 @@ +<%= link_to t('new_ad_btn'), new_advertisement_path, class: 'nav-link text-primary' %> \ No newline at end of file diff --git a/app/views/advertisements/new.html.erb b/app/views/advertisements/new.html.erb new file mode 100644 index 0000000..3557654 --- /dev/null +++ b/app/views/advertisements/new.html.erb @@ -0,0 +1,23 @@ +<%= form_with model: @advertisement, class: 'mt-3' do |f| %> +
+ <%= f.label :title, class: 'form-label' %> + <%= f.text_field :title, class: 'form-control' %> +
+ +
+ <%= f.label :link, class: 'form-label' %> + <%= f.text_field :link, class: 'form-control' %> +
+ +
+ <%= f.label :display_time, class: 'form-label' %> + <%= f.number_field :display_time, class: 'form-control', min: 1 %> +
+ +
+ <%= f.label :image, class: 'form-label' %> + <%= f.file_field :image, class: 'form-control' %> +
+ + <%= f.submit t('save_btn'), class: 'btn btn-primary mt-3' %> +<% end %> \ No newline at end of file diff --git a/app/views/advertisements/show.html.erb b/app/views/advertisements/show.html.erb new file mode 100644 index 0000000..56e5364 --- /dev/null +++ b/app/views/advertisements/show.html.erb @@ -0,0 +1,6 @@ +

<%= @advertisement.title %>

+ +<%= link_to t('return_btn'), advertisements_path, class: 'btn btn-secondary' %> + +<%= image_tag @advertisement.image %> + diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 707b4b8..47b4fe8 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -71,6 +71,11 @@ + <% if current_user.admin? %> + + <% end %> diff --git a/config/locales/buttons.pt-BR.yml b/config/locales/buttons.pt-BR.yml index b8949ae..9b88ca5 100644 --- a/config/locales/buttons.pt-BR.yml +++ b/config/locales/buttons.pt-BR.yml @@ -10,4 +10,6 @@ pt-BR: unpin_btn: Desafixar return_btn: Voltar send_btn: Enviar - publish_btn: Publicar \ No newline at end of file + publish_btn: Publicar + ads_btn: Anúncios + new_ad_btn: Criar Anúncio \ No newline at end of file diff --git a/config/locales/models/advertisement.pt-BR.yml b/config/locales/models/advertisement.pt-BR.yml new file mode 100644 index 0000000..8f902e2 --- /dev/null +++ b/config/locales/models/advertisement.pt-BR.yml @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8beab28..8c4707e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,8 @@ root to: 'home#index' + resources :advertisements, only: %i[index new create show] + resources :searches, only: %i[index] resources :invitations, only: %i[index show] do patch 'decline', on: :member diff --git a/db/migrate/20240215195415_create_advertisements.rb b/db/migrate/20240215195415_create_advertisements.rb new file mode 100644 index 0000000..b40a45e --- /dev/null +++ b/db/migrate/20240215195415_create_advertisements.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1e32535..b03cc4e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_14_151256) do +ActiveRecord::Schema[7.1].define(version: 2024_02_15_195415) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -49,14 +49,25 @@ t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end + create_table "advertisements", force: :cascade 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.integer "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_advertisements_on_user_id" + end + create_table "comments", force: :cascade do |t| t.text "message" t.integer "post_id", null: false t.integer "user_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "status", default: 0 t.text "old_message" + t.integer "status", default: 0 t.index ["post_id"], name: "index_comments_on_post_id" t.index ["user_id"], name: "index_comments_on_user_id" end @@ -162,7 +173,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "pin", default: 0 - t.datetime "edited_at" + t.datetime "edited_at", default: "2024-02-13 01:41:27" t.integer "status", default: 0 t.datetime "published_at" t.string "old_status" @@ -385,6 +396,7 @@ add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "advertisements", "users" add_foreign_key "comments", "posts" add_foreign_key "comments", "users" add_foreign_key "connections", "profiles", column: "followed_profile_id" diff --git a/spec/factories/advertisements.rb b/spec/factories/advertisements.rb new file mode 100644 index 0000000..d561f07 --- /dev/null +++ b/spec/factories/advertisements.rb @@ -0,0 +1,10 @@ +FactoryBot.define do + factory :advertisement do + image { nil } + link { "MyString" } + display_time { 1 } + view_count { 1 } + title { "MyString" } + user { nil } + end +end diff --git a/spec/models/advertisement_spec.rb b/spec/models/advertisement_spec.rb new file mode 100644 index 0000000..6511e17 --- /dev/null +++ b/spec/models/advertisement_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Advertisement, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/advertisements/user_visits_advertisements_page_spec.rb b/spec/requests/advertisements/user_visits_advertisements_page_spec.rb new file mode 100644 index 0000000..afdb424 --- /dev/null +++ b/spec/requests/advertisements/user_visits_advertisements_page_spec.rb @@ -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 diff --git a/spec/system/advertisements/admin_create_ad_spec.rb b/spec/system/advertisements/admin_create_ad_spec.rb new file mode 100644 index 0000000..283b96e --- /dev/null +++ b/spec/system/advertisements/admin_create_ad_spec.rb @@ -0,0 +1,45 @@ +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: '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_current_path advertisement_path(ad) + expect(page).to have_content 'Anúncio criado com sucesso' + 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 + + pending 'Visualizar o anúncio pela listagem' + + pending 'e visitante visualzia anúncio no feed' + + pending 'e view_count atualiza quando o anúncio é clicado' +end From 38ca8b29b90273a3a24fb091951d895ccd6997e9 Mon Sep 17 00:00:00 2001 From: Paulo Henrique de Meneses Oliveira Date: Thu, 15 Feb 2024 20:16:58 -0300 Subject: [PATCH 2/7] =?UTF-8?q?wip:=20usu=C3=A1rio=20free=20v=C3=AA=20an?= =?UTF-8?q?=C3=BAncios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Paulo Henrique Co-authored-by: Caique Arruda Co-authored-by: Danilo Martins --- app/models/advertisement.rb | 4 +++ .../advertisements/_advertisement.html.erb | 7 +++++ app/views/posts/_listing.html.erb | 5 +++- spec/factories/advertisements.rb | 9 +++---- .../advertisements/admin_create_ad_spec.rb | 2 -- spec/system/home/user_views_home_page_spec.rb | 26 +++++++++++++++++++ 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 app/views/advertisements/_advertisement.html.erb diff --git a/app/models/advertisement.rb b/app/models/advertisement.rb index c59ef03..7db6215 100644 --- a/app/models/advertisement.rb +++ b/app/models/advertisement.rb @@ -1,4 +1,8 @@ class Advertisement < ApplicationRecord belongs_to :user has_one_attached :image + + def self.displayed + where("created_at + display_time < ?", Time.zone.now).sample(1) + end end diff --git a/app/views/advertisements/_advertisement.html.erb b/app/views/advertisements/_advertisement.html.erb new file mode 100644 index 0000000..e096dad --- /dev/null +++ b/app/views/advertisements/_advertisement.html.erb @@ -0,0 +1,7 @@ + diff --git a/app/views/posts/_listing.html.erb b/app/views/posts/_listing.html.erb index 69c2728..8c6bdb1 100644 --- a/app/views/posts/_listing.html.erb +++ b/app/views/posts/_listing.html.erb @@ -1,4 +1,4 @@ -<% posts.each do |post| %> +<% posts.each_with_index do |post, index| %>
<%= link_to post, class: "text-decoration-none link-dark col-md-10" do %>
@@ -37,4 +37,7 @@
<% end %>
+ <% if (index+1) % 5 == 0 %> + <%= render partial: 'advertisements/advertisement', locals: { advertisement: Advertisement.displayed.first } %> + <% end %> <% end %> diff --git a/spec/factories/advertisements.rb b/spec/factories/advertisements.rb index d561f07..bc17faf 100644 --- a/spec/factories/advertisements.rb +++ b/spec/factories/advertisements.rb @@ -1,10 +1,9 @@ FactoryBot.define do factory :advertisement do image { nil } - link { "MyString" } - display_time { 1 } - view_count { 1 } - title { "MyString" } - user { nil } + link { "www.campuscode.com" } + display_time { 7 } + title { Faker::Lorem.paragraph } + user end end diff --git a/spec/system/advertisements/admin_create_ad_spec.rb b/spec/system/advertisements/admin_create_ad_spec.rb index 283b96e..6157a52 100644 --- a/spec/system/advertisements/admin_create_ad_spec.rb +++ b/spec/system/advertisements/admin_create_ad_spec.rb @@ -37,8 +37,6 @@ end end - pending 'Visualizar o anúncio pela listagem' - pending 'e visitante visualzia anúncio no feed' pending 'e view_count atualiza quando o anúncio é clicado' diff --git a/spec/system/home/user_views_home_page_spec.rb b/spec/system/home/user_views_home_page_spec.rb index b73d9bd..9d00f8e 100644 --- a/spec/system/home/user_views_home_page_spec.rb +++ b/spec/system/home/user_views_home_page_spec.rb @@ -121,6 +121,32 @@ expect(page.body.index(first_user.full_name)).to be < page.body.index(second_user.full_name) expect(page.body.index(second_user.full_name)).to be < page.body.index(third_user.full_name) end + + it 'como usuário free e vê anúncios' do + user = create(:user) + admin = create(:user, role: 'admin') + ad1 = create(:advertisement, user: admin, title: 'Cursos de Software', link: 'wwww.campuscode.com.br') + ad2 = create(:advertisement, user: admin, title: 'Venha ser Dev', link: 'wwww.dev.com.br') + + 10.times { create(:post) } + allow(Post).to receive(:get_sample).and_return(Post.all) + allow(Advertisement).to receive(:displayed).and_return([ad1], [ad2]) + + login_as user + visit root_path + + within "#advertisement_#{ad1.id}" do + expect(page).to have_link 'Cursos de Software', href: 'wwww.campuscode.com.br' + end + within "#advertisement_#{ad2.id}" do + expect(page).to have_link 'Venha ser Dev', href: 'wwww.dev.com.br' + end + + expect(page).to have_selector '.advertisement', count: 2 + expect(page.body.index(Post.find(5).title)).to be < page.body.index('Cursos de Software') + expect(page.body.index(Post.find(6).title)).to be > page.body.index('Cursos de Software') + expect(page.body.index(Post.find(10).title)).to be < page.body.index('Venha ser Dev') + end end context 'quando não está logado' do From 5b2d8d701ec5be5ac0d0ad197df333376768018f Mon Sep 17 00:00:00 2001 From: Paulo Henrique de Meneses Oliveira Date: Thu, 15 Feb 2024 22:44:58 -0300 Subject: [PATCH 3/7] =?UTF-8?q?wip:=20incrementa=20contador=20de=20cliques?= =?UTF-8?q?=20ao=20clicar=20no=20an=C3=BAncio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Danilo Martins Co-authored-by: Caique Arruda Co-authored-by: Paulo Henrique --- app/controllers/advertisements_controller.rb | 10 +++ app/controllers/home_controller.rb | 2 +- .../advertisements/_advertisement.html.erb | 10 +-- app/views/advertisements/show.html.erb | 2 +- app/views/posts/_listing.html.erb | 2 +- config/routes.rb | 2 +- spec/models/advertisement_spec.rb | 5 +- .../advertisements/admin_create_ad_spec.rb | 4 - .../advertisements/user_sees_ads_spec.rb | 74 +++++++++++++++++++ spec/system/home/user_views_home_page_spec.rb | 26 ------- 10 files changed, 97 insertions(+), 40 deletions(-) create mode 100644 spec/system/advertisements/user_sees_ads_spec.rb diff --git a/app/controllers/advertisements_controller.rb b/app/controllers/advertisements_controller.rb index 34c3094..3b1c5ce 100644 --- a/app/controllers/advertisements_controller.rb +++ b/app/controllers/advertisements_controller.rb @@ -20,6 +20,16 @@ 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 diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index b5a0ba6..1583a1d 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -8,6 +8,6 @@ def index return if @followed_posts.any? - @posts = Post.get_sample(3) + @posts = Post.get_sample(10) end end diff --git a/app/views/advertisements/_advertisement.html.erb b/app/views/advertisements/_advertisement.html.erb index e096dad..bcd3258 100644 --- a/app/views/advertisements/_advertisement.html.erb +++ b/app/views/advertisements/_advertisement.html.erb @@ -1,7 +1,7 @@ diff --git a/app/views/advertisements/show.html.erb b/app/views/advertisements/show.html.erb index 56e5364..e5757af 100644 --- a/app/views/advertisements/show.html.erb +++ b/app/views/advertisements/show.html.erb @@ -2,5 +2,5 @@ <%= link_to t('return_btn'), advertisements_path, class: 'btn btn-secondary' %> -<%= image_tag @advertisement.image %> +<%= image_tag @advertisement.image if @advertisement.image.present? %> diff --git a/app/views/posts/_listing.html.erb b/app/views/posts/_listing.html.erb index 8c6bdb1..790f0fe 100644 --- a/app/views/posts/_listing.html.erb +++ b/app/views/posts/_listing.html.erb @@ -37,7 +37,7 @@ <% end %> - <% if (index+1) % 5 == 0 %> + <% if current_user.subscription.inactive? && (index+1) % 5 == 0 && Advertisement.any? %> <%= render partial: 'advertisements/advertisement', locals: { advertisement: Advertisement.displayed.first } %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 8c4707e..85f94f7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ root to: 'home#index' - resources :advertisements, only: %i[index new create show] + resources :advertisements, only: %i[index show new create update] resources :searches, only: %i[index] resources :invitations, only: %i[index show] do diff --git a/spec/models/advertisement_spec.rb b/spec/models/advertisement_spec.rb index 6511e17..9e932e4 100644 --- a/spec/models/advertisement_spec.rb +++ b/spec/models/advertisement_spec.rb @@ -1,5 +1,8 @@ require 'rails_helper' RSpec.describe Advertisement, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + pending 'deve ter título' + pending 'deve ter link' + pending '#displayed' + pending 'formato do link' end diff --git a/spec/system/advertisements/admin_create_ad_spec.rb b/spec/system/advertisements/admin_create_ad_spec.rb index 6157a52..164bd76 100644 --- a/spec/system/advertisements/admin_create_ad_spec.rb +++ b/spec/system/advertisements/admin_create_ad_spec.rb @@ -36,8 +36,4 @@ expect(page).not_to have_link 'Anúncios' end end - - pending 'e visitante visualzia anúncio no feed' - - pending 'e view_count atualiza quando o anúncio é clicado' end diff --git a/spec/system/advertisements/user_sees_ads_spec.rb b/spec/system/advertisements/user_sees_ads_spec.rb new file mode 100644 index 0000000..f2f14b0 --- /dev/null +++ b/spec/system/advertisements/user_sees_ads_spec.rb @@ -0,0 +1,74 @@ +require 'rails_helper' + +describe 'Usuário visualiza anúncios na home page' do + context 'com assinatura free' do + it 'a cada 5 posts' do + user = create(:user, :free) + admin = create(:user, role: 'admin') + ad1 = create(:advertisement, user: admin, title: 'Cursos de Software', link: 'wwww.campuscode.com.br') + ad2 = create(:advertisement, user: admin, title: 'Venha ser Dev', link: 'wwww.dev.com.br') + + 10.times { create(:post) } + allow(Post).to receive(:get_sample).and_return(Post.all) + allow(Advertisement).to receive(:displayed).and_return([ad1], [ad2]) + + login_as user + visit root_path + + within "#advertisement_#{ad1.id}" do + expect(page).to have_button 'Cursos de Software' + end + within "#advertisement_#{ad2.id}" do + expect(page).to have_button 'Venha ser Dev' + end + + expect(page).to have_selector '.advertisement', count: 2 + expect(page.body.index(Post.find(5).title)).to be < page.body.index('Cursos de Software') + expect(page.body.index(Post.find(6).title)).to be > page.body.index('Cursos de Software') + expect(page.body.index(Post.find(10).title)).to be < page.body.index('Venha ser Dev') + end + + it 'e clica em um anúncio' do + user = create(:user, :free) + admin = create(:user, role: 'admin') + ad = create(:advertisement, user: admin, title: 'Cursos de Software', + link: 'https://www.campuscode.com.br', view_count: 0) + ad.image.attach(io: File.open('spec/support/assets/images/test_image.png'), + filename: 'test_image.png', content_type: 'image/png') + ad.save + + 5.times { create(:post) } + allow(Post).to receive(:get_sample).and_return(Post.all) + login_as user + visit root_path + + within "#advertisement_#{ad.id}" do + click_button id: 'to-ad' + end + + expect(ad.reload.view_count).to eq 1 + expect(page).to have_current_path ad.link + end + end + + context 'com assinatura premium' do + it 'não visualiza anúncios' do + user = create(:user, :paid) + admin = create(:user, role: 'admin') + ad = create(:advertisement, user: admin, title: 'Cursos de Software', + link: 'https://www.campuscode.com.br', view_count: 0) + ad.image.attach(io: File.open('spec/support/assets/images/test_image.png'), + filename: 'test_image.png', content_type: 'image/png') + ad.save + + 5.times { create(:post) } + allow(Post).to receive(:get_sample).and_return(Post.all) + login_as user + visit root_path + + expect(page).not_to have_css "#advertisement_#{ad.id}" + expect(page).not_to have_content 'Cursos de Software' + expect(page).not_to have_link 'https://www.campuscode.com.br' + end + end +end diff --git a/spec/system/home/user_views_home_page_spec.rb b/spec/system/home/user_views_home_page_spec.rb index 9d00f8e..b73d9bd 100644 --- a/spec/system/home/user_views_home_page_spec.rb +++ b/spec/system/home/user_views_home_page_spec.rb @@ -121,32 +121,6 @@ expect(page.body.index(first_user.full_name)).to be < page.body.index(second_user.full_name) expect(page.body.index(second_user.full_name)).to be < page.body.index(third_user.full_name) end - - it 'como usuário free e vê anúncios' do - user = create(:user) - admin = create(:user, role: 'admin') - ad1 = create(:advertisement, user: admin, title: 'Cursos de Software', link: 'wwww.campuscode.com.br') - ad2 = create(:advertisement, user: admin, title: 'Venha ser Dev', link: 'wwww.dev.com.br') - - 10.times { create(:post) } - allow(Post).to receive(:get_sample).and_return(Post.all) - allow(Advertisement).to receive(:displayed).and_return([ad1], [ad2]) - - login_as user - visit root_path - - within "#advertisement_#{ad1.id}" do - expect(page).to have_link 'Cursos de Software', href: 'wwww.campuscode.com.br' - end - within "#advertisement_#{ad2.id}" do - expect(page).to have_link 'Venha ser Dev', href: 'wwww.dev.com.br' - end - - expect(page).to have_selector '.advertisement', count: 2 - expect(page.body.index(Post.find(5).title)).to be < page.body.index('Cursos de Software') - expect(page.body.index(Post.find(6).title)).to be > page.body.index('Cursos de Software') - expect(page.body.index(Post.find(10).title)).to be < page.body.index('Venha ser Dev') - end end context 'quando não está logado' do From 0483781ebd7ee812e431384b0f5dd861540b72dd Mon Sep 17 00:00:00 2001 From: Caique Date: Fri, 16 Feb 2024 10:45:09 -0400 Subject: [PATCH 4/7] =?UTF-8?q?Feat/Adiciona=20valida=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?links=20e=20teste=20de=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/advertisement.rb | 8 ++- .../advertisements/_advertisement.html.erb | 4 +- db/schema.rb | 4 +- spec/factories/advertisements.rb | 2 +- spec/models/advertisement_spec.rb | 49 +++++++++++++++++-- .../advertisements/admin_create_ad_spec.rb | 4 +- .../advertisements/user_sees_ads_spec.rb | 4 +- 7 files changed, 61 insertions(+), 14 deletions(-) diff --git a/app/models/advertisement.rb b/app/models/advertisement.rb index 7db6215..ad58182 100644 --- a/app/models/advertisement.rb +++ b/app/models/advertisement.rb @@ -1,8 +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("created_at + display_time < ?", Time.zone.now).sample(1) + where.not(id: Advertisement.select(&:expired?)).sample(1) + end + + def expired? + (created_at + display_time.days) < Time.zone.now end end diff --git a/app/views/advertisements/_advertisement.html.erb b/app/views/advertisements/_advertisement.html.erb index bcd3258..1ba8e70 100644 --- a/app/views/advertisements/_advertisement.html.erb +++ b/app/views/advertisements/_advertisement.html.erb @@ -1,7 +1,7 @@ -