diff --git a/app/controllers/advertisements_controller.rb b/app/controllers/advertisements_controller.rb new file mode 100644 index 0000000..850e230 --- /dev/null +++ b/app/controllers/advertisements_controller.rb @@ -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 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/models/advertisement.rb b/app/models/advertisement.rb new file mode 100644 index 0000000..ad58182 --- /dev/null +++ b/app/models/advertisement.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index b3e2455..05f5aa6 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 has_one :subscription, dependent: :destroy enum role: { user: 0, admin: 10 } diff --git a/app/views/advertisements/_advertisement.html.erb b/app/views/advertisements/_advertisement.html.erb new file mode 100644 index 0000000..0b57c24 --- /dev/null +++ b/app/views/advertisements/_advertisement.html.erb @@ -0,0 +1,7 @@ + diff --git a/app/views/advertisements/index.html.erb b/app/views/advertisements/index.html.erb new file mode 100644 index 0000000..9678849 --- /dev/null +++ b/app/views/advertisements/index.html.erb @@ -0,0 +1,11 @@ +<%= link_to t('new_ad_btn'), new_advertisement_path, class: 'btn btn-primary mt-3' %> + +<% @advertisements.each do |ad| %> + + + +<% end %> \ 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..05fea3e --- /dev/null +++ b/app/views/advertisements/show.html.erb @@ -0,0 +1,14 @@ +<%= link_to t('return_btn'), advertisements_path, class: 'btn btn-secondary mb-3' %>
+ +
+

<%= @advertisement.title %>

+ + <%= image_tag @advertisement.image, width: 400, class: 'mb-3' if @advertisement.image.present? %> + +

Tempo de exibição (em dias): <%= @advertisement.display_time %>

+ +

Link: <%= @advertisement.link %>

+ +

Cliques: <%= @advertisement.view_count %>

+
+ diff --git a/app/views/posts/_listing.html.erb b/app/views/posts/_listing.html.erb index 5a85c5a..480d261 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 current_user.subscription.inactive? && (index+1) % 5 == 0 && Advertisement.any? %> + <%= render partial: 'advertisements/advertisement', locals: { advertisement: Advertisement.displayed.first } %> + <% end %> <% end %> diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 156a684..46462fe 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -69,6 +69,11 @@ + <% if current_user.admin? %> + + <% end %>