Skip to content

Commit

Permalink
Merge branch 'feat/protege-rotas-premium' into feat/priorizando-usuar…
Browse files Browse the repository at this point in the history
…ios-premium
  • Loading branch information
hreis1 committed Feb 14, 2024
2 parents 5eeda24 + a3d6de3 commit eb9ac42
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 44 deletions.
8 changes: 8 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class ProjectsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_subscriber, only: :create_invitation_request

def index
@invitation_request = current_user.invitation_requests.build
@invitation_requests = current_user.invitation_requests.pluck(:project_id).to_json
@invitation_requests_projects_ids = current_user.invitation_requests.pluck(:project_id)
@free_user = current_user.subscription.inactive?
end

def create_invitation_request
Expand All @@ -19,6 +21,12 @@ def create_invitation_request

private

def authenticate_subscriber
return if current_user.subscription.active?

redirect_to root_path, alert: t('alerts.unauthorized')
end

def invitation_request_params
invitation_request_params = params.require(:invitation_request).permit(:message)
invitation_request_params['project_id'] = params['project_id']
Expand Down
1 change: 1 addition & 0 deletions app/javascript/components/projects_vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
showingForm: false,
currentProjectId: null,
invitationRequestsProjectsIds: window.invitationRequestsProjectsIds,
freeUser: window.freeUser,
errorMsg: false,
}
},
Expand Down
14 changes: 14 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Subscription < ApplicationRecord
belongs_to :user
enum status: { inactive: 0, active: 10 }

def active!
self.start_date = Time.zone.now.to_date
super
end

def inactive!
self.start_date = nil
super
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile, dependent: :destroy
has_one :subscription, dependent: :destroy
has_many :posts, dependent: :nullify
has_many :likes, dependent: :destroy
has_many :comments, dependent: :nullify
Expand All @@ -24,6 +25,7 @@ class User < ApplicationRecord
after_create :'create_profile!'
after_create :subscribe_likes_mailer_job
after_create :update_search_name
after_create :create_subscription

def description
if admin?
Expand Down
84 changes: 45 additions & 39 deletions app/views/invitation_requests/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
<h2 class="text-center mb-5"><%= InvitationRequest.model_name.human(count: @invitation_request_infos.length) %></h2>

<div class="container">
<div class="row justify-content-center gap-5">
<%= form_with url: invitation_requests_path, method: :get, class: 'col-10 me-5 px-0' do |f| %>
<div class="d-flex gap-2 col-3">
<%= f.label :filter, class: 'd-none' %>
<%= f.select :filter,
invitation_request_filter_options,
{ include_blank: 'Todas', selected: params[:filter] },
class: 'form-select' %>
<%= f.submit t(:filter_btn ), class: 'btn btn-sm btn-primary' %>
</div>
<% end %>
<% if current_user.subscription.inactive? %>
<div class="text-center become-premium-div mt-5">
<h2 class="become-premium-text"><%= t('subscriptions.become') %> <strong><%= t('subscriptions.subscriber') %></strong> <%= t('subscriptions.see_and_request') %></h2 class="become-premium-text">
</div>
<% else %>
<div class="container">
<div class="row justify-content-center gap-5">
<%= form_with url: invitation_requests_path, method: :get, class: 'col-10 me-5 px-0' do |f| %>
<div class="d-flex gap-2 col-3">
<%= f.label :filter, class: 'd-none' %>
<%= f.select :filter,
invitation_request_filter_options,
{ include_blank: 'Todas', selected: params[:filter] },
class: 'form-select' %>
<%= f.submit t(:filter_btn ), class: 'btn btn-sm btn-primary' %>
</div>
<% end %>
<% if @invitation_request_infos.any? %>
<% @invitation_request_infos.each do |invitation_request| %>
<div class="col-5 rounded border p-3" id="request_<%= invitation_request.id %>">
<div class="card-body">
<div class="d-flex flex-column gap-4">
<div class="d-flex flex-column gap-2">
<h4 class="text-break text-dark m-0"><%= invitation_request.project_title %></h4>
<div class="d-flex flex-column gap-2 align-items-start">
<p class="m-0"><%= invitation_request.project_description %></p>
<p class="m-0 fs-sm badge text-bg-secondary"><%= invitation_request.project_category %></p>
<% if @invitation_request_infos.any? %>
<% @invitation_request_infos.each do |invitation_request| %>
<div class="col-5 rounded border p-3" id="request_<%= invitation_request.id %>">
<div class="card-body">
<div class="d-flex flex-column gap-4">
<div class="d-flex flex-column gap-2">
<h4 class="text-break text-dark m-0"><%= invitation_request.project_title %></h4>
<div class="d-flex flex-column gap-2 align-items-start">
<p class="m-0"><%= invitation_request.project_description %></p>
<p class="m-0 fs-sm badge text-bg-secondary"><%= invitation_request.project_category %></p>
</div>
</div>
<div class="d-flex justify-content-between align-items-center">
<p class="m-0 text-muted">
<%= t(:time_ago, time: time_ago_in_words(invitation_request.created_at)) %>
</p>
<p class="m-0 text-center <%= css_color_class(invitation_request.status.to_sym) %> fw-bold fs-5">
<%= InvitationRequest.human_attribute_name("status.#{invitation_request.status}") %>
</p>
</div>
</div>
<div class="d-flex justify-content-between align-items-center">
<p class="m-0 text-muted">
<%= t(:time_ago, time: time_ago_in_words(invitation_request.created_at)) %>
</p>
<p class="m-0 text-center <%= css_color_class(invitation_request.status.to_sym) %> fw-bold fs-5">
<%= InvitationRequest.human_attribute_name("status.#{invitation_request.status}") %>
</p>
</div>
</div>
</div>
</div>
<% end %>
<% elsif @error %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:project_api_error) %></h3>
<% elsif params[:filter].nil? || params[:filter].blank? %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_invitation_request_msg) %></h3>
<% else %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_filter_results_msg) %></h3>
<% end %>
<% elsif @error %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:project_api_error) %></h3>
<% elsif params[:filter].nil? || params[:filter].blank? %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_invitation_request_msg) %></h3>
<% else %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_filter_results_msg) %></h3>
<% end %>
</div>
</div>
</div>
<% end %>
9 changes: 7 additions & 2 deletions app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="container" id="vue-projects-app">
<h2 class="text-center"><%= t('.index_title') %> </h2>

<div v-if="emptyData" class="text-center">
<div v-if="freeUser" class="text-center become-premium-div mt-5">
<h2 class="become-premium-text"><%= t('subscriptions.become') %> <strong><%= t('subscriptions.subscriber') %></strong> <%= t('subscriptions.see_listed_projects') %></h2 class="become-premium-text">
</div>

<div v-else-if="emptyData" class="text-center">
<p>{{ emptyData }}</p>
</div>

Expand Down Expand Up @@ -40,7 +44,7 @@
<h3 v-else-if="projects.length == 0 && !errorMsg" class="mt-5 alert alert-warning text-center">
<%= t('.errors.no_result') %>
</h3>

<div v-else>
<div class="mt-5 border-top w-50 mx-auto" v-for="project in filteredProjects" :key="project.id">
<h3 class="mt-3">{{ project.title }}</h3>
Expand Down Expand Up @@ -71,4 +75,5 @@

<script>
var invitationRequestsProjectsIds = <%= @invitation_requests_projects_ids %>
var freeUser = <%= @free_user %>
</script>
4 changes: 2 additions & 2 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
<%= link_to t('projects.model.other'), projects_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to Invitation.model_name.human(count: 2), invitations_path, class: 'nav-link' %>
<%= link_to InvitationRequest.model_name.human(count: 2), invitation_requests_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to 'Solicitações de Convites', invitation_requests_path, class: 'nav-link' %>
<%= link_to Invitation.model_name.human(count: 2), invitations_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to notifications_path, class: 'nav-link' do %>
Expand Down
10 changes: 10 additions & 0 deletions config/locales/subscription.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pt-BR:
activerecord:
models:
subscription: Assinatura

subscriptions:
become: Torne-se
subscriber: assinante
see_listed_projects: para ver projetos listados
see_and_request: para ver e fazer solicitações
11 changes: 11 additions & 0 deletions db/migrate/20240214151256_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateSubscriptions < ActiveRecord::Migration[7.1]
def change
create_table :subscriptions do |t|
t.references :user, null: false, foreign_key: true
t.date :start_date
t.integer :status, default: 0

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

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

7 changes: 7 additions & 0 deletions spec/factories/subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :subscription do
user { nil }
start_date { "2024-02-14" }
status { 1 }
end
end
10 changes: 10 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
email { Faker::Internet.email }
password { '123456' }

after(:create) do |user|
user.subscription.active!
end

trait :seed do
after(:build) do |user|
user.class.skip_callback(:create, :after, :create_profile!, raise: false)
end
end

trait :free do
after(:create) do |user|
user.subscription.inactive!
end
end
end
end
5 changes: 5 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Subscription, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
27 changes: 27 additions & 0 deletions spec/system/subscription/free_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

describe 'Usuário não assinante' do
it 'acessa página de solicitações e recebe mensagem para se tornar assinante' do
user = create(:user, :free)

login_as user
visit root_path
click_button class: 'dropdown-toggle'
click_on 'Solicitações de Convite'

expect(page).to have_current_path(invitation_requests_path)
expect(page).to have_content 'Torne-se assinante para ver e fazer solicitações'
end

it 'acessa página de projetos e recebe mensagem para se tornar assinante' do
user = create(:user, :free)

login_as user
visit root_path
click_button class: 'dropdown-toggle'
click_on 'Projetos'

expect(page).to have_current_path(projects_path)
expect(page).to have_content 'Torne-se assinante para ver projetos listados'
end
end
Empty file.

0 comments on commit eb9ac42

Please sign in to comment.