Skip to content

Commit d91a32b

Browse files
authored
[Feat] Signup users (#661)
1 parent 60fbd67 commit d91a32b

File tree

18 files changed

+289
-22
lines changed

18 files changed

+289
-22
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class PetitionsController < ApplicationController
2+
def create
3+
petition = Petition.new petition_params
4+
5+
if petition.save
6+
flash[:notice] = 'Application sent'
7+
else
8+
flash[:error] = 'Something went wrong'
9+
end
10+
11+
redirect_to organizations_path
12+
end
13+
14+
def update
15+
petition = Petition.find params[:id]
16+
status = params[:status]
17+
18+
if petition.update(status: status)
19+
User.find(params[:user_id]).add_to_organization(current_organization) if status == 'accepted'
20+
flash[:notice] = "Application #{status}"
21+
else
22+
flash[:error] = 'Something went wrong'
23+
end
24+
25+
redirect_to manage_petitions_path
26+
end
27+
28+
def manage
29+
@status = params[:status] || 'pending'
30+
@users = User.joins(:petitions).where(petitions: { organization_id: current_organization.id, status: @status }).page(params[:page]).per(20)
31+
end
32+
33+
private
34+
35+
def petition_params
36+
params.permit(%i[organization_id user_id status])
37+
end
38+
end

app/controllers/terms_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ def show
88

99
def accept
1010
current_user.touch :terms_accepted_at
11-
redirect_to root_path
11+
redirect_to(current_user.organizations.empty? ? organizations_path : root_path)
1212
end
1313
end

app/controllers/users_controller.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class UsersController < ApplicationController
2-
before_action :authenticate_user!, :member_should_be_active
2+
before_action :authenticate_user!, :member_should_be_active, except: [:signup, :create]
33

44
has_scope :tagged_with, as: :tag
55

@@ -41,8 +41,10 @@ def create
4141
@user.setup_and_save_user
4242

4343
if @user.persisted?
44-
@user.tune_after_persisted(current_organization)
45-
@user.add_tags(current_organization, params[:tag_list] || [])
44+
unless request.referer.include?(signup_users_path)
45+
@user.tune_after_persisted(current_organization)
46+
@user.add_tags(current_organization, params[:tag_list] || [])
47+
end
4648

4749
redirect_to_after_create
4850
else
@@ -65,6 +67,10 @@ def update
6567
end
6668
end
6769

70+
def signup
71+
@user = User.new
72+
end
73+
6874
def update_avatar
6975
operation = AvatarGenerator.new(current_user, params)
7076

@@ -102,6 +108,7 @@ def user_params
102108
fields_to_permit += %w"admin registration_number
103109
registration_date" if admin?
104110
fields_to_permit += %w"organization_id superadmin" if superadmin?
111+
fields_to_permit += %w"password" if request.referer.include?(signup_users_path)
105112

106113
params.require(:user).permit *fields_to_permit
107114
end
@@ -115,17 +122,22 @@ def find_user
115122
end
116123

117124
def redirect_to_after_create
118-
id = @user.member(current_organization).member_uid
119-
if params[:more]
120-
redirect_to new_user_path,
121-
notice: I18n.t("users.new.user_created_add",
122-
uid: id,
123-
name: @user.username)
125+
if request.referer.include?(signup_users_path)
126+
sign_in(@user)
127+
redirect_to terms_path
124128
else
125-
redirect_to users_path,
126-
notice: I18n.t("users.index.user_created",
127-
uid: id,
128-
name: @user.username)
129+
id = @user.member(current_organization).member_uid
130+
if params[:more]
131+
redirect_to new_user_path,
132+
notice: I18n.t("users.new.user_created_add",
133+
uid: id,
134+
name: @user.username)
135+
else
136+
redirect_to users_path,
137+
notice: I18n.t("users.index.user_created",
138+
uid: id,
139+
name: @user.username)
140+
end
129141
end
130142
end
131143
end

app/models/organization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Organization < ApplicationRecord
2121
has_many :offers
2222
has_many :inquiries
2323
has_many :documents, as: :documentable, dependent: :destroy
24+
has_many :petitions, dependent: :delete_all
2425

2526
validates :name, presence: true, uniqueness: true
2627

app/models/petition.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Petition < ApplicationRecord
2+
enum status: %i[pending accepted declined]
3+
4+
belongs_to :user
5+
belongs_to :organization
6+
end

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class User < ApplicationRecord
2727
has_many :offers
2828
has_many :inquiries
2929
has_many :device_tokens
30+
has_many :petitions, dependent: :delete_all
3031

3132
accepts_nested_attributes_for :members
3233

app/policies/user_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class UserPolicy < ApplicationPolicy
22
def create?
3-
user.admins?(organization)
3+
!user || user.admins?(organization)
44
end
55

66
def update?

app/views/application/menus/_organization_listings_menu.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
<%= t "application.navbar.users" %>
1212
<% end %>
1313
</li>
14+
<li>
15+
<%= link_to manage_petitions_path do %>
16+
<%= glyph 'list-alt' %>
17+
<%= 'Manage Applications' %>
18+
<% end %>
19+
</li>
1420
<li>
1521
<%= link_to offers_path(org: current_organization) do %>
1622
<%= glyph :link %>

app/views/devise/sessions/new.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
</div>
4444
</div>
4545
</div>
46+
OR
47+
<div class="form-group">
48+
<div class="row">
49+
<div class="col-xs-12">
50+
<%= link_to 'Sign up', signup_users_path, class: "btn btn-primary btn-lg col-xs-12" %>
51+
</div>
52+
</div>
53+
</div>
4654
<% end %>
4755
</div>
4856
<div class="panel-footer">

app/views/organizations/index.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<th><%= t '.neighborhood' %></th>
2828
<th><%= t '.web' %></th>
2929
<th><%= t '.member_count' %></th>
30+
<th></th>
3031
</tr>
3132
</thead>
3233
<tbody>
@@ -37,6 +38,15 @@
3738
<td><%= org.neighborhood %></td>
3839
<td><%= link_to(org.web, org.web) if org.web.present? %></td>
3940
<td><%= org.members.count %></td>
41+
<td>
42+
<% if current_user %>
43+
<% if petition = current_user.petitions.where(organization_id: org.id).last %>
44+
<%= petition.status %>
45+
<% else %>
46+
<%= link_to 'Apply to join', petitions_path(user_id: current_user.id, organization_id: org.id, status: 'pending'), method: :post %>
47+
<% end %>
48+
<% end %>
49+
</td>
4050
</tr>
4151
<% end %>
4252
</tbody>

0 commit comments

Comments
 (0)