Skip to content
Merged
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
38 changes: 38 additions & 0 deletions app/controllers/petitions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class PetitionsController < ApplicationController
def create
petition = Petition.new petition_params

if petition.save
flash[:notice] = 'Application sent'
else
flash[:error] = 'Something went wrong'
end

redirect_to organizations_path
end

def update
petition = Petition.find params[:id]
status = params[:status]

if petition.update(status: status)
User.find(params[:user_id]).add_to_organization(current_organization) if status == 'accepted'
flash[:notice] = "Application #{status}"
else
flash[:error] = 'Something went wrong'
end

redirect_to manage_petitions_path
end

def manage
@status = params[:status] || 'pending'
@users = User.joins(:petitions).where(petitions: { organization_id: current_organization.id, status: @status }).page(params[:page]).per(20)
end

private

def petition_params
params.permit(%i[organization_id user_id status])
end
end
2 changes: 1 addition & 1 deletion app/controllers/terms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def show

def accept
current_user.touch :terms_accepted_at
redirect_to root_path
redirect_to(current_user.organizations.empty? ? organizations_path : root_path)
end
end
38 changes: 25 additions & 13 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UsersController < ApplicationController
before_action :authenticate_user!, :member_should_be_active
before_action :authenticate_user!, :member_should_be_active, except: [:signup, :create]

has_scope :tagged_with, as: :tag

Expand Down Expand Up @@ -41,8 +41,10 @@ def create
@user.setup_and_save_user

if @user.persisted?
@user.tune_after_persisted(current_organization)
@user.add_tags(current_organization, params[:tag_list] || [])
unless request.referer.include?(signup_users_path)
@user.tune_after_persisted(current_organization)
@user.add_tags(current_organization, params[:tag_list] || [])
end

redirect_to_after_create
else
Expand All @@ -65,6 +67,10 @@ def update
end
end

def signup
@user = User.new
end

def update_avatar
operation = AvatarGenerator.new(current_user, params)

Expand Down Expand Up @@ -102,6 +108,7 @@ def user_params
fields_to_permit += %w"admin registration_number
registration_date" if admin?
fields_to_permit += %w"organization_id superadmin" if superadmin?
fields_to_permit += %w"password" if request.referer.include?(signup_users_path)

params.require(:user).permit *fields_to_permit
end
Expand All @@ -115,17 +122,22 @@ def find_user
end

def redirect_to_after_create
id = @user.member(current_organization).member_uid
if params[:more]
redirect_to new_user_path,
notice: I18n.t("users.new.user_created_add",
uid: id,
name: @user.username)
if request.referer.include?(signup_users_path)
sign_in(@user)
redirect_to terms_path
else
redirect_to users_path,
notice: I18n.t("users.index.user_created",
uid: id,
name: @user.username)
id = @user.member(current_organization).member_uid
if params[:more]
redirect_to new_user_path,
notice: I18n.t("users.new.user_created_add",
uid: id,
name: @user.username)
else
redirect_to users_path,
notice: I18n.t("users.index.user_created",
uid: id,
name: @user.username)
end
end
end
end
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Organization < ApplicationRecord
has_many :offers
has_many :inquiries
has_many :documents, as: :documentable, dependent: :destroy
has_many :petitions, dependent: :delete_all

validates :name, presence: true, uniqueness: true

Expand Down
6 changes: 6 additions & 0 deletions app/models/petition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Petition < ApplicationRecord
enum status: %i[pending accepted declined]

belongs_to :user
belongs_to :organization
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class User < ApplicationRecord
has_many :offers
has_many :inquiries
has_many :device_tokens
has_many :petitions, dependent: :delete_all

accepts_nested_attributes_for :members

Expand Down
2 changes: 1 addition & 1 deletion app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UserPolicy < ApplicationPolicy
def create?
user.admins?(organization)
!user || user.admins?(organization)
end

def update?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<%= t "application.navbar.users" %>
<% end %>
</li>
<li>
<%= link_to manage_petitions_path do %>
<%= glyph 'list-alt' %>
<%= 'Manage Applications' %>
<% end %>
</li>
<li>
<%= link_to offers_path(org: current_organization) do %>
<%= glyph :link %>
Expand Down
8 changes: 8 additions & 0 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
</div>
</div>
</div>
OR
<div class="form-group">
<div class="row">
<div class="col-xs-12">
<%= link_to 'Sign up', signup_users_path, class: "btn btn-primary btn-lg col-xs-12" %>
</div>
</div>
</div>
<% end %>
</div>
<div class="panel-footer">
Expand Down
10 changes: 10 additions & 0 deletions app/views/organizations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<th><%= t '.neighborhood' %></th>
<th><%= t '.web' %></th>
<th><%= t '.member_count' %></th>
<th></th>
</tr>
</thead>
<tbody>
Expand All @@ -37,6 +38,15 @@
<td><%= org.neighborhood %></td>
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
<td><%= org.members.count %></td>
<td>
<% if current_user %>
<% if petition = current_user.petitions.where(organization_id: org.id).last %>
<%= petition.status %>
<% else %>
<%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
82 changes: 82 additions & 0 deletions app/views/petitions/manage.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h1>
<%= link_to current_organization.name, current_organization %>
<%= 'Applications' %>
</h1>
</div>
</div>
</div>

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<ul class="nav nav-pills actions-menu">
<li class="<%= "active" if @status == 'pending' %>">
<%= link_to manage_petitions_path(status: 'pending') do %>
<%= glyph :minus %>
<%= 'Pending applications' %>
<% end %>
</li>
<li class="<%= "active" if @status == 'accepted' %>">
<%= link_to manage_petitions_path(status: 'accepted') do %>
<%= glyph :ok %>
<%= 'Accepted applications' %>
<% end %>
</li>
<li class="<%= "active" if @status == 'declined' %>">
<%= link_to manage_petitions_path(status: 'declined') do %>
<%= glyph :remove %>
<%= 'Declined applications' %>
<% end %>
</li>
</ul>
</div>
</div>

<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-hover table-condensed panel">
<thead>
<tr>
<th></th>
<th><%= 'ID' %></th>
<th><%= 'username' %></th>
<th><%= User.human_attribute_name(:email) %></th>
<th><%= User.human_attribute_name(:phone) %></th>
<% if current_user.manages?(current_organization) && @status == 'pending' %>
<th>
<span class="glyphicon glyphicon-hand-down"></span>
<%= t(".actions") %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<%= content_tag(:tr) do %>
<% petition = user.petitions.where(organization_id: current_organization.id).last %>
<td><%= image_tag avatar_url(user.user) %></td>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= phone_to user.phone %></td>
<% if current_user.manages?(current_organization) && @status == 'pending' %>
<td>
<%= link_to 'Accept', petition_path(id: petition.id, user_id: user.id, status: 'accepted'), class: 'btn btn-primary', method: :put %>
<%= link_to 'Decline', petition_path(id: petition.id, user_id: user.id, status: 'declined'), class: 'btn btn-danger', method: :put %>
</td>
<% end %>
<% end %>
<% end %>
</tbody>
</table>
</div>
<div class="paginate-align">
<%= paginate @users %>
</div>
</div>
</div>
</div>
13 changes: 9 additions & 4 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="form-inputs">
<%= f.input :username %>

<% if admin? %>
<% if admin? || short %>
<%= f.input :email %>
<% if @user.unconfirmed_email.present? %>
<%= f.input :unconfirmed_email, readonly: true %>
Expand All @@ -12,6 +12,8 @@
<%= f.input :email, readonly: true %>
<% end %>

<%= f.input :password, label: t("application.login_form.password"), required: true if short %>

<%= f.input :phone %>
<%= f.input :alt_phone %>
<%= f.input :gender, collection: genders_collection %>
Expand Down Expand Up @@ -49,9 +51,12 @@
</div>
<div class="form-actions">
<%= f.button :submit %>
<% if @user.new_record? %>
<%= f.button :submit, t('users.new.create_more_users_button'), name: 'more' %>

<% if !short %>
<% if @user.new_record? %>
<%= f.button :submit, t('users.new.create_more_users_button'), name: 'more' %>
<% end %>
<%= link_to t('users.new.cancel'), users_path, class: "btn btn-default" %>
<% end %>
<%= link_to t('users.new.cancel'), users_path, class: "btn btn-default" %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<small><%= t ".edit_user" %></small>
</h1>

<%= render "form", member: @user.as_member_of(current_organization) %>
<%= render "form", member: @user.as_member_of(current_organization), short: false %>
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>
<%= t ".new_user" %>
</h1>
<%= render "form", member: Member.new %>
<%= render "form", member: Member.new, short: false %>
1 change: 1 addition & 0 deletions app/views/users/signup.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form', member: Member.new, short: true %>
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@

resources :users, concerns: :accountable, except: :destroy, :path => "members" do
collection do
get 'signup'
get 'manage'
end
end
put :update_avatar, to: 'users#update_avatar'

resources :petitions, only: [:create, :update] do
collection do
get 'manage'
end
end

resources :transfers, only: [:new, :create] do
member do
put :delete_reason
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20221016192111_create_petitions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreatePetitions < ActiveRecord::Migration[6.1]
def change
create_table :petitions do |t|
t.references :user, null: false, foreign_key: true
t.references :organization, null: false, foreign_key: true
t.integer :status

t.timestamps
end
end
end
Loading