Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/controllers/cas/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
before_action :set_current_user
before_action :set_user_sites
before_action :set_person_sites
before_action :set_domain
before_action :set_site

Expand All @@ -13,8 +13,8 @@ def set_current_user
@current_user = current_user
end

def set_user_sites
@user_sites = @current_user.sites if @current_user.present?
def set_person_sites
@person_sites = @current_user.sites if @current_user.present?
end

def set_domain
Expand Down
75 changes: 75 additions & 0 deletions app/controllers/cas/sites/people_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require_dependency "cas/application_controller"

module Cas
class Sites::PeopleController < Sites::ApplicationController
def index
@people = @site.people.order('name ASC')
end

def new
@person = ::Cas::Person.new
get_selected_sites
end

def create
@person = ::Cas::Person.new(person_params)
@person.roles = person_params[:roles]
@person.site_ids = person_params[:site_ids]
if @person.save
redirect_to site_people_url(@site)
else
get_selected_sites
render :new
end
end

def edit
@person = ::Cas::Person.find(params[:id])
get_selected_sites
end

def update
@person = ::Cas::Person.find(params[:id])
success = nil
@person.site_ids = person_params[:site_ids]
if person_params[:password].blank? && person_params[:password_confirmation].blank?
without_password = person_params.except(:password, :password_confirmation)
success = @person.update_without_password(without_password)
else
success = @person.update_attributes(person_params)
end

if success
redirect_to site_people_url(@site)
else
get_selected_sites
render 'edit'
end
end

private

def person_params
site_ids = Array.wrap(params[:person][:site_ids]) << @site.id
params
.require(:person)
.permit(
:name,
:email,
:site_ids,
:password,
:password_confirmation,
:roles,
)
.merge!(
roles: [params[:person][:roles]],
site_ids: (site_ids).uniq.keep_if(&:present?)
)
end

def get_selected_sites
@selected_sites = @person.sites.map(&:id)
@selected_sites << @site.id if @person.new_record?
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/cas/sites/sections/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create
@content.section_id = @section.id
@content.tag_list = content_params[:tag_list] if content_params[:tag_list]
success = @content.save!
::Cas::Activity.create!(user: current_user, site: @site, subject: @content, event_name: 'create')
::Cas::Activity.create!(person: current_user, site: @site, subject: @content, event_name: 'create')
associate_files(@content, :images)
associate_files(@content, :attachments)
end
Expand Down Expand Up @@ -63,7 +63,7 @@ def update
success = @content.update!(content_params)
associate_files(@content, :images)
associate_files(@content, :attachments)
::Cas::Activity.create!(user: current_user, site: @site, subject: @content, event_name: 'update')
::Cas::Activity.create!(person: current_user, site: @site, subject: @content, event_name: 'update')
end
rescue ActiveRecord::RecordInvalid
success = nil
Expand Down
75 changes: 0 additions & 75 deletions app/controllers/cas/sites/users_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/cas/activity.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cas
class Activity < ApplicationRecord
belongs_to :site
belongs_to :user
belongs_to :person
belongs_to :subject, polymorphic: true
end
end
4 changes: 2 additions & 2 deletions app/models/cas/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Content < ApplicationRecord
belongs_to :section
has_one :site, through: :section
belongs_to :category
belongs_to :author, class_name: "::Cas::User"
has_many :images, ->{ where(media_type: :image).order("cas_media_files.order ASC") }, class_name: "::Cas::MediaFile", as: :attachable, dependent: :destroy
belongs_to :author, class_name: "::Cas::Person"
has_many :images, ->{ where(media_type: :image).order("cas_media_files.order ASC") }, class_name: Cas::MediaFile, as: :attachable, dependent: :destroy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisa colocar de volta a string em class_name pra que a classe só seja executada quando necessária (lazy evaluation) e os :: (pra evitar conflito de namespace)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faltou aqui

has_many :attachments, ->{ where(media_type: :attachment).order("cas_media_files.order ASC") }, class_name: "::Cas::MediaFile", as: :attachable, dependent: :destroy
has_many :activities, as: :subject
has_one :cover_image, ->{ where(media_type: :image, cover: true) }, class_name: "::Cas::MediaFile", as: :attachable
Expand Down
2 changes: 1 addition & 1 deletion app/models/cas/media_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class UnknownPath < StandardError; end
class UnknownFileService < StandardError; end

belongs_to :attachable, polymorphic: true
belongs_to :author, class_name: "::Cas::User"
belongs_to :author, class_name: "Cas::Person"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preciso do :: pra evitar conflito de namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆


before_validation :set_media_type
before_save :set_image_as_unique_cover
Expand Down
7 changes: 7 additions & 0 deletions app/models/cas/people_site.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Cas
class PeopleSite < ApplicationRecord
belongs_to :site
belongs_to :person
belongs_to :owner, class_name: '::Cas::Person'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end
end
10 changes: 5 additions & 5 deletions app/models/cas/user.rb → app/models/cas/person.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module Cas
class User < ApplicationRecord
ROLES = %w[admin editor writer].freeze
class Person < ApplicationRecord
ROLES = %w[admin editor writer visitor].freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pensando melhor, acho que o nome visitor aqui fica um pouco restrito... e se for um cliente? Se for um fornecedor? No caso da tua app, não seria visitor, seria mais um... customer?

Acho que o melhor nome seria generic. É tipo um coringa que serve pra tudo e aí no futuro tornamos mais específico. O que acha?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️


devise :database_authenticatable, #:recoverable,
:rememberable, :trackable, :validatable, request_keys: [:domain]

has_many :contents
has_many :files, class_name: '::Cas::MediaFile', as: :attachable
has_many :sites_users, class_name: '::Cas::SitesUser'
has_many :sites, through: :sites_users
has_many :files, class_name: 'Cas::MediaFile', as: :attachable
has_many :people_site, class_name: 'Cas::PeopleSite'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has_many :people_sites, creio. :sites_users era plural.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️

has_many :sites, through: :people_site
has_many :activities, as: :subject

validates :name, presence: true, length: { maximum: 50 }
Expand Down
4 changes: 2 additions & 2 deletions app/models/cas/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Site < ApplicationRecord
friendly_id :name, use: :slugged

has_many :sections, dependent: :destroy
has_many :sites_users, class_name: '::Cas::SitesUser'
has_many :users, through: :sites_users
has_many :people_site, class_name: '::Cas::PeopleSite'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:people_sites aqui também porque é múltiplos

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️

has_many :people, through: :people_site
end
end
7 changes: 0 additions & 7 deletions app/models/cas/sites_user.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/cas/activities/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<%= activity.site.name %>
</td>
<td>
<strong><%= activity.user.name %></strong>
<strong><%= activity.person.name %></strong>
<% if activity.event_name == 'create' %>
criou
<% elsif activity.event_name == 'update'%>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= simple_form_for [@site, @user] do |f| %>
<%= simple_form_for [@site, @person] do |f| %>
<%= f.input :name %><br />
<%= f.input :email %><br />

Expand All @@ -16,7 +16,7 @@
<% end %>

<% if current_user.admin? %>
<%= f.collection_select :roles, Cas::User::ROLES, :to_s, :humanize %></p>
<%= f.collection_select :roles, Cas::Person::ROLES, :to_s, :humanize %></p>
<% end %>
<%= f.input :password, required: false %><br />
<%= f.input :password_confirmation %><br />
Expand Down
34 changes: 34 additions & 0 deletions app/views/cas/sites/people/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div class="grid">
<div class="col-1-2">
<h1>Pessoas</h1>
</div>
<div class="col-1-2 section-options">
</div>
</div>

<%= link_to "Nova Pessoa", new_site_person_path(@site) if current_user.admin? %>

<br />
<br />

<table>
<thead>
<tr>
<th>Nome</th>
<th></th>
</tr>
</thead>

<tbody>
<% @people.each do |person| %>
<tr id="person-<%= person.id %>">
<td>
<%= link_to person.name,
edit_site_person_url(@site, person),
id: "edit-person-#{person.id}" %>
</td>
<td><%= person.roles.join(", ") %></td>
</tr>
<% end %>
</tbody>
</table>
2 changes: 1 addition & 1 deletion app/views/cas/sites/sections/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</thead>

<% @sections.each do |section| %>
<% next unless Cas::SectionConfig.new(section).accessible_by_user?(current_user) %>
<% next unless Cas::SectionConfig.new(section).accessible_by_person?(current_user) %>
<tr id="section-<%= section.id %>">
<td><%= section.name %></td>
<td>
Expand Down
34 changes: 0 additions & 34 deletions app/views/cas/sites/users/index.html.erb

This file was deleted.

Loading