Skip to content

Commit

Permalink
[89] Refact views and controllers to use concerns and helpers (#90)
Browse files Browse the repository at this point in the history
* feature: use scope in comments

* refactor: job proposal using helpers

* refact proposal comments controller

* create: concern authior

* refact: user author id and type as params in form

* rubocop: remove empty lien
  • Loading branch information
0jonjo authored Dec 16, 2023
1 parent 32e9b44 commit bbe2e6e
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 62 deletions.
8 changes: 0 additions & 8 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ class CommentsController < ApplicationController
before_action :set_comment, only: %i[show edit update destroy]
before_action :profile, only: %i[show edit update destroy]

def index
@comments = @profile.comments.all
end

def show
@comments = Comment.all
end

def new
@comment = Comment.new
end
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/concerns/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Author
def author
if headhunter_signed_in?
@author_id = current_headhunter.id
@author_type = current_headhunter.class.to_s
else
@author_id = current_user.id
@author_type = current_user.class.to_s
end
end
end
3 changes: 1 addition & 2 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def update
end

def show
current_headhunter.id if headhunter_signed_in?
@comments = @profile.comments.all
@comments = Comment.comments_by_headhunter(current_headhunter, @profile) if headhunter_signed_in?
@comment = @profile.comments.build
end

Expand Down
17 changes: 3 additions & 14 deletions app/controllers/proposal_comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

class ProposalCommentsController < ApplicationController
include Author
before_action :proposal_comment, only: %i[show edit update destroy]
before_action :apply, only: %i[show new create edit]
before_action :proposal, only: %i[index show new create edit update destroy]
before_action :author, only: %i[index create]
before_action :author, only: %i[index new create]

def index
@proposal_comments = ProposalComment.by_proposal_id(@proposal.id).page(params[:page])
@comments_from_author = @proposal_comments.by_author(@author_type, @author_id).ids
end

def show; end
Expand All @@ -20,9 +22,6 @@ def edit; end

def create
@proposal_comment = ProposalComment.new(proposal_comment_params)
@proposal_comment.proposal_id = @proposal.id
@proposal_comment.author_id = @author_id
@proposal_comment.author_type = @author_type
return redirect_to apply_proposal_proposal_comments_path, notice: 'Comment created.' if @proposal_comment.save

redirect_to request.referrer, notice: "Comment can't be created."
Expand All @@ -48,16 +47,6 @@ def apply
@apply = Apply.find(params[:apply_id])
end

def author
if headhunter_signed_in?
@author_id = current_headhunter.id
@author_type = current_headhunter.class.to_s
else
@author_id = current_user.id
@author_type = current_user.class.to_s
end
end

def proposal
@proposal = Proposal.find(params[:proposal_id])
end
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# frozen_string_literal: true

module ApplicationHelper
def format_datetime(datetime)
datetime.to_formatted_s(:long)
end

def format_date(date)
date.strftime('%d/%m/%Y')
end
end
8 changes: 8 additions & 0 deletions app/helpers/jobs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ def levels
def statuses
Job.job_statuses.keys.map { |status| [I18n.t("activerecord.attributes.job.job_statuses.keys.#{status}"), status] }
end

def translate_level(level)
I18n.t("activerecord.attributes.job.levels.keys.#{level}")
end

def translate_job_status(status)
I18n.t("activerecord.attributes.job.job_statuses.keys.#{status}")
end
end
4 changes: 4 additions & 0 deletions app/helpers/proposal_comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module ProposalCommentsHelper
end
7 changes: 7 additions & 0 deletions app/helpers/proposals_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module ProposalsHelper
def translate_user_acceptance(user_acceptance)
I18n.t(user_acceptance)
end
end
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Comment < ApplicationRecord
belongs_to :headhunter

validates :body, :profile_id, :headhunter_id, presence: true

scope :comments_by_headhunter, ->(headhunter, profile) { where(headhunter: headhunter, profile: profile) }
end
2 changes: 2 additions & 0 deletions app/models/proposal_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class ProposalComment < ApplicationRecord
validates :body, :proposal_id, presence: true

scope :by_proposal_id, ->(proposal_id) { where(proposal_id: proposal_id) }

scope :by_author, ->(author_type, author_id) { where(author_type: author_type, author_id: author_id) }
end
2 changes: 1 addition & 1 deletion app/views/applies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% if @apply.errors.any? %>
<% @apply.errors.full_messages.each do |message|%>
<p><%= message %></p>
<% end %>
<% end %>
<% end %>

<%= form_with model: apply do |form| %>
Expand Down
18 changes: 8 additions & 10 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<b><%= I18n.t('your_comments') if headhunter_signed_in? %></b>
<% @comments.each do |comment| %>
<% if comment.headhunter_id == current_headhunter.id %>
<div>
<p><%= I18n.localize(comment.updated_at, format: :short) %></p>
<p><%= comment.body %></p>
<p><%= link_to I18n.t('edit'), edit_profile_comment_path(@profile.id, comment.id) %></p>
<div>
<p><%= I18n.localize(comment.updated_at, format: :short) %></p>
<p><%= comment.body %></p>
<p><%= link_to I18n.t('edit'), edit_profile_comment_path(@profile.id, comment.id) %></p>
<div>
<%= button_to I18n.t('delete'), profile_comment_path(@profile.id, comment.id),
method: :delete,
data: { confirm: I18n.t('sure') } %>
</div>
<%= button_to I18n.t('delete'), profile_comment_path(@profile.id, comment.id),
method: :delete,
data: { confirm: I18n.t('sure') } %>
</div>
<% end %>
</div>
<% end %>
25 changes: 24 additions & 1 deletion app/views/jobs/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
<h4><%= @job.title %></h4>

<%= render partial: "layouts/show_atributtes", locals: {what_to_show: @job} %>
<div>
<dt><%= Job.human_attribute_name(:code) %></dt>
<dd><%= @job.code %></dd>
<dt><%= Job.human_attribute_name(:description) %></dt>
<dd><%= @job.description %></dd>
<dt><%= Job.human_attribute_name(:skills) %></dt>
<dd><%= @job.skills %></dd>
<dt><%= Job.human_attribute_name(:salary) %></dt>
<dd><%= @job.salary %></dd>
<dt><%= Job.human_attribute_name(:level) %></dt>
<dd><%= translate_level(@job.level) %></dd>
<dt><%= Job.human_attribute_name(:date) %></dt>
<dd><%= I18n.l(@job.date, format: :default) %></dd>
<dt><%= Job.human_attribute_name(:company_id) %></dt>
<dd><%= @job.company.name %></dd>
<dt><%= Job.human_attribute_name(:city) %></dt>
<dd><%= @job.city %></dd>
<dt><%= Job.human_attribute_name(:country_id) %></dt>
<dd><%= @job.country.acronym %></dd>
<dt><%= Job.human_attribute_name(:job_status) %></dt>
<dd><%= translate_job_status(@job.job_status) %></dd>
<dt><%= Job.human_attribute_name(:updated_at) %></dt>
<dd><%= format_datetime(@job.updated_at) %></dd>
</div>

<% if user_signed_in? %>
<% if @user_applied.present? %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_navbar_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<%= f.label :query, I18n.t('search_job') %>
<%= f.text_field :query %>
<%= f.submit I18n.t('search') %>
<% end %>
<% end %>
<% end %>

20 changes: 0 additions & 20 deletions app/views/layouts/_show_atributtes.html.erb

This file was deleted.

3 changes: 3 additions & 0 deletions app/views/proposal_comments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<% end %>

<%= form_with(model: [@apply, @proposal, @proposal_comment]) do |form| %>
<%= form.hidden_field :proposal_id, value: @proposal.id %>
<%= form.hidden_field :author_id, value: @author_id if @author_id %>
<%= form.hidden_field :author_type, value: @author_type if @author_type %>
<div>
<%= form.text_area :body %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/proposal_comments/_proposal_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p><%=proposal_comment.author_type%>: <%=I18n.localize(proposal_comment.updated_at, format: :short) %></p>
<p><%=proposal_comment.body%></p>

<% if proposal_comment.author_type == @author_type && proposal_comment.author_id == @author_id %>
<% if @comments_from_author.include?(proposal_comment.id) %>
<%= link_to I18n.t('edit'), edit_apply_proposal_proposal_comment_path(@proposal.apply_id, @proposal.id, proposal_comment.id) %>
<%= button_to I18n.t('delete'), apply_proposal_proposal_comment_path(@proposal.apply_id, @proposal.id, proposal_comment.id),
method: :delete,
Expand Down
22 changes: 20 additions & 2 deletions app/views/proposals/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
<h4><%= Proposal.model_name.human %> <%= @proposal.id %></h4>

<div>
<p><%= link_to "#{Apply.human_attribute_name(:id)}: #{@proposal.apply_id}", apply_path(@proposal.apply_id) %></p>
<p>
<%= Proposal.human_attribute_name(:user_accepted) %>:
<%= translate_user_acceptance(@proposal.user_accepted) %>
</p>
</div>

<div>
<p><%= link_to "#{Apply.human_attribute_name(:id)}: #{@proposal.apply_id}", apply_path(@proposal.apply_id) %></p>
</div>

<%= render partial: "layouts/show_atributtes", locals: {what_to_show: @proposal} %>
<div>
<dt><%= Proposal.human_attribute_name(:salary) %></dt>
<dd><%= @proposal.salary %></dd>
<dt><%= Proposal.human_attribute_name(:benefits) %>
<dd><%= @proposal.benefits %></dd>
<dt><%= Proposal.human_attribute_name(:expectations) %></dt>
<dd><%= @proposal.expectations %></dd>
<dt><%= Proposal.human_attribute_name(:expected_start) %></dt>
<dd><%= I18n.l(@proposal.expected_start, format: :default) %></dd>
<dt><%= Proposal.human_attribute_name(:updated_at) %></dt>
<dd><%= format_datetime(@proposal.updated_at) %></dd>
</div>

<div>
<% if headhunter_signed_in? %>
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
# config.navigational_formats = ['*/*', :html]

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete
config.sign_out_via = :get

# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
Expand Down

0 comments on commit bbe2e6e

Please sign in to comment.