Skip to content

Commit 3badc1a

Browse files
authored
Merge pull request #626 from coopdevs/feat/tags-postcode-members
feat: tags and postal code attributes
2 parents ed6871a + c8d2eeb commit 3badc1a

29 files changed

+309
-180
lines changed

app/admin/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
filter :email
3131
filter :username
3232
filter :phone
33+
filter :postcode
3334

3435
form do |f|
3536
f.semantic_errors *f.object.errors.keys
3637
f.inputs do
3738
f.input :username
3839
f.input :email
3940
f.input :phone
41+
f.input :postcode
4042
f.input :gender, as: :select, collection: User::GENDERS
4143
f.input :identity_document
4244
end

app/assets/javascripts/application/tags.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ $(function() {
77
loadTags('inquiry');
88
});
99

10+
$(".switch_member-js").on("click", function() {
11+
loadTags('user');
12+
});
13+
1014
function loadTags(type){
1115
$.get({
1216
url: `/tags/alpha_grouped_index.js?post_type=${type}`,
@@ -28,7 +32,7 @@ $(function() {
2832
ajax: {
2933
url: '/tags.json',
3034
data: function(params) {
31-
return { term: params.term };
35+
return { term: params.term, model: $(this).data("model") };
3236
},
3337
processResults: function(data, params) {
3438
// parse the data into the format expected by Select2

app/assets/stylesheets/application/member-card.scss

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
}
3838
}
3939

40+
&__tags {
41+
a {
42+
margin-left: 4px;
43+
color: white;
44+
}
45+
}
46+
4047
&__activity {
4148
font-size: 14px;
4249
color: #78adb9;

app/controllers/tags_controller.rb

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@ class TagsController < ApplicationController
22
before_action :authenticate_user!, :member_should_be_active
33

44
def index
5-
posts = Post.by_organization(current_organization)
6-
@all_tags = posts.find_like_tag(params[:term])
5+
model = params[:model].classify.constantize
6+
posts = model.by_organization(current_organization)
7+
@tags = posts.find_like_tag(params[:term])
78

8-
render json: @all_tags
9+
render json: @tags
910
end
1011

1112
def alpha_grouped_index
1213
redirect_to users_path && return unless current_organization
1314

1415
post_type = params[:post_type] || "offer"
15-
@alpha_tags = case post_type
16-
when "offer" then Offer
17-
when "inquiry" then Inquiry
18-
end.by_organization(current_organization).
19-
active.of_active_members.
20-
alphabetical_grouped_tags
16+
@tags = case post_type
17+
when "offer"
18+
Offer.by_organization(current_organization).active.of_active_members
19+
when "inquiry"
20+
Inquiry.by_organization(current_organization).active.of_active_members
21+
when "user"
22+
Member.by_organization(current_organization).active
23+
end.alphabetical_grouped_tags
2124

2225
respond_to do |format|
2326
format.html
2427
format.js do
25-
render partial: "grouped_index", locals: { alpha_tags: @alpha_tags, post_type: post_type }
28+
render partial: "grouped_index", locals: { alpha_tags: @tags, post_type: post_type }
2629
end
2730
end
2831
end

app/controllers/users_controller.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
class UsersController < ApplicationController
22
before_action :authenticate_user!, :member_should_be_active
33

4+
has_scope :tagged_with, as: :tag
5+
46
def index
5-
search_and_load_members current_organization.members.active, { s: 'user_last_sign_in_at DESC' }
7+
members = current_organization.members.active
8+
members = apply_scopes(members)
9+
10+
search_and_load_members members, { s: 'user_last_sign_in_at DESC' }
611
end
712

813
def manage
@@ -37,9 +42,12 @@ def create
3742

3843
if @user.persisted?
3944
@user.tune_after_persisted(current_organization)
45+
@user.add_tags(current_organization, params[:tag_list] || [])
46+
4047
redirect_to_after_create
4148
else
4249
@user.email = "" if empty_email
50+
4351
render action: "new"
4452
end
4553
end
@@ -49,6 +57,8 @@ def update
4957
authorize @user
5058

5159
if @user.update(user_params)
60+
@user.add_tags(current_organization, params[:tag_list] || [])
61+
5262
redirect_to @user
5363
else
5464
render action: :edit, status: :unprocessable_entity
@@ -76,7 +86,7 @@ def scoped_users
7686

7787
def user_params
7888
fields_to_permit = %w"gender username email date_of_birth phone
79-
alt_phone active description notifications push_notifications"
89+
alt_phone active description notifications push_notifications postcode"
8090
fields_to_permit += %w"admin registration_number
8191
registration_date" if admin?
8292
fields_to_permit += %w"organization_id superadmin" if superadmin?

app/decorators/member_decorator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class MemberDecorator < ViewModel
2-
delegate :user, :member_uid, :active?, to: :object
2+
delegate :user, :member_uid, :tags, :active?, to: :object
33
delegate :phone, :alt_phone, :username, :description, :last_sign_in_at, to: :user
44

55
def manager?

app/models/concerns/taggable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Taggable
66
extend ActiveSupport::Concern
77

88
included do
9-
scope :tagged_with, ->(tag) { where("? = ANY (tags)", tag) }
9+
scope :tagged_with, ->(tag) { where("? = ANY (#{table_name}.tags)", tag) }
1010
end
1111

1212
def tag_list

app/models/member.rb

+15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
class Member < ApplicationRecord
2+
include Taggable
3+
24
# Cast the member_uid integer to a string to allow pg ILIKE search (from Ransack *_contains)
35
ransacker :member_uid_search do
46
Arel.sql("member_uid::text")
57
end
8+
# Convert array of tags to string
9+
ransacker :member_tags do
10+
Arel.sql("array_to_string(tags, ',')")
11+
end
12+
ransack_alias :member_search, %w(
13+
user_username
14+
user_email
15+
user_phone
16+
user_alt_phone
17+
member_uid_search
18+
member_tags
19+
).join("_or_")
620

721
belongs_to :user
822
belongs_to :organization
@@ -15,6 +29,7 @@ class Member < ApplicationRecord
1529

1630
scope :by_month, -> (month) { where(created_at: month.beginning_of_month..month.end_of_month) }
1731
scope :active, -> { where active: true }
32+
scope :by_organization, ->(org) { where(organization_id: org) if org }
1833

1934
validates :organization_id, presence: true
2035
validates :member_uid,

app/models/user.rb

+5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ def tune_after_persisted(organization)
110110
save
111111
end
112112

113+
def add_tags(organization, tag_list)
114+
member = as_member_of(organization)
115+
member.update(tag_list: tag_list)
116+
end
117+
113118
def has_valid_email?
114119
!email.include? "example.com"
115120
end

app/views/shared/_post_form.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
f.object.tags || [],
3535
{ selected: f.object.tags },
3636
multiple: true,
37-
data: { placeholder: t('application.tips.entertag') },
37+
data: { placeholder: t("application.tips.entertag"), model: "post" },
3838
id: "tags-js",
3939
class: "form-control" %>
4040
</div>
+20-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
<h1>
2-
<%= t '.maintitle' %>
3-
<%= form_tag alpha_grouped_index_tags_path, method: :get do %>
4-
<div class="btn-group" data-toggle="buttons">
5-
<label class="btn btn-primary active switch_offer-js">
6-
<%= radio_button_tag 'switch_offer', 'offer', true %>
7-
<%= Offer.model_name.human count: :many %>
8-
</label>
9-
<label class="btn btn-primary switch_inquiry-js">
10-
<%= radio_button_tag 'switch_inquiry', 'inquiry' %>
11-
<%= Inquiry.model_name.human count: :many %>
12-
</label>
13-
</div>
14-
<% end %>
15-
</h1>
1+
<h1><%= t '.maintitle' %></h1>
2+
3+
<%= form_tag alpha_grouped_index_tags_path, method: :get do %>
4+
<div class="btn-group" data-toggle="buttons">
5+
<label class="btn btn-primary active switch_offer-js">
6+
<%= radio_button_tag 'switch_offer', 'offer', true %>
7+
<%= Offer.model_name.human count: :many %>
8+
</label>
9+
<label class="btn btn-primary switch_inquiry-js">
10+
<%= radio_button_tag 'switch_inquiry', 'inquiry' %>
11+
<%= Inquiry.model_name.human count: :many %>
12+
</label>
13+
<label class="btn btn-primary switch_member-js">
14+
<%= radio_button_tag 'switch_member', 'user' %>
15+
<%= t "users.index.members" %>
16+
</label>
17+
</div>
18+
<% end %>
19+
1620
<div class="alpha_tag_list col-xs-12 col-md-12">
17-
<%= render 'grouped_index',
18-
alpha_tags: @alpha_tags,
19-
post_type: params[:post_type] || 'offer' %>
21+
<%= render 'grouped_index', alpha_tags: @tags, post_type: params[:post_type] || 'offer' %>
2022
</div>

app/views/users/_form.html.erb

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
end_year: Date.today.year - 12,
2121
include_blank: :true %>
2222
<%= f.input :description, as: "text" %>
23+
<%= f.input :postcode %>
24+
<%= label_tag :tag_list, t('activerecord.attributes.post.tag_list') %>
25+
<div class='form-group'>
26+
<%= select_tag :tag_list,
27+
options_for_select(member.tags, member.tags),
28+
multiple: true,
29+
data: { placeholder: t("application.tips.entertag"), model: "member" },
30+
id: "tags-js",
31+
class: "form-control" %>
32+
</div>
2333

2434
<div class='form-group'>
2535
<label><%= t('.notifications') %></label>

app/views/users/_member_card.html.erb

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
<% else %>
1212
<%= t('.no_activity') %>
1313
<% end %>
14+
<% member.tags[0..2].each do |tag| %>
15+
<span class="to-member-card__header__text__tags">
16+
<%= link_to users_path tag: tag do %>
17+
<%= glyph :tag %>
18+
<%= tag&.truncate(29) %>
19+
<% end %>
20+
</span>
21+
<% end %>
22+
<% if member.tags.size > 3 %>
23+
...
24+
<% end %>
1425
</div>
1526
</div>
1627
</div>

app/views/users/edit.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<small><%= t ".edit_user" %></small>
44
</h1>
55

6-
<%= render "form" %>
6+
<%= render "form", member: @user.as_member_of(current_organization) %>

app/views/users/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="col-md-12">
1515
<%= search_form_for(@search, class: "navbar-form navbar-left", url: users_path) do |f| %>
1616
<div class="form-group">
17-
<%= f.search_field :user_username_or_user_email_or_member_uid_search_contains, class: "form-control" %>
17+
<%= f.search_field :member_search_cont, class: "form-control" %>
1818
</div>
1919
<button class="btn btn-default" type="submit">
2020
<%= t 'global.search' %>

app/views/users/manage.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="col-md-12">
1515
<%= search_form_for(@search, class: "navbar-form navbar-left", url: manage_users_path) do |f| %>
1616
<div class="form-group">
17-
<%= f.search_field :user_username_or_user_email_or_user_phone_or_user_alt_phone_or_member_uid_search_contains, class: "form-control" %>
17+
<%= f.search_field :member_search_cont, class: "form-control" %>
1818
</div>
1919
<button class="btn btn-default" type="submit">
2020
<%= t 'global.search' %>

app/views/users/new.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<h1>
22
<%= t ".new_user" %>
33
</h1>
4-
<%= render "form" %>
4+
<%= render "form", member: Member.new %>

app/views/users/show.html.erb

+23
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@
6969
<% end %>
7070
</dd>
7171
<% end %>
72+
<% if @user.postcode.present? %>
73+
<dt>
74+
<%= t "activerecord.attributes.user.postcode" %>
75+
</dt>
76+
<dd>
77+
<%= @user.postcode %>
78+
</dd>
79+
<% end %>
80+
<% if @member.tags.present? %>
81+
<dt>
82+
<%= t "application.navbar.tags" %>
83+
</dt>
84+
<dd>
85+
<% @member.tags.each do |tag| %>
86+
<span class="badge alert-success">
87+
<%= link_to users_path tag: tag do %>
88+
<%= glyph(:tag) %>
89+
<%= tag %>
90+
<% end %>
91+
</span>
92+
<% end %>
93+
</dd>
94+
<% end %>
7295
</dl>
7396
</div>
7497
</div>

config/locales/ca.yml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ ca:
6666
notifications: Rebre notificacions
6767
organization: Organització
6868
phone: Telèfon
69+
postcode: Codi postal
6970
push_notifications: Rebre notificacions al mòbil
7071
registration_date: Data d'alta
7172
registration_number: Codi d'usuari

config/locales/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ en:
6666
notifications: Receive email notifications
6767
organization: Organization
6868
phone: Phone
69+
postcode: Postal code
6970
push_notifications: Receive mobile notifications
7071
registration_date: Registration date
7172
registration_number: User code

config/locales/es.yml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ es:
6666
notifications: Recibir notificaciones
6767
organization: Organización
6868
phone: Teléfono
69+
postcode: Código postal
6970
push_notifications: Recibir notificaciones móviles
7071
registration_date: Fecha de alta
7172
registration_number: Código de usuario

config/locales/eu.yml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ eu:
6666
notifications: Jaso eposta jakinarazpenak
6767
organization: Erakundea
6868
phone: Telefonoa
69+
postcode:
6970
push_notifications: Jaso jakinarazpenak sakelakoan
7071
registration_date: Erregistratze-da
7172
registration_number: Erabiltzaile kodea

config/locales/gl.yml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ gl:
6666
notifications: Recibir notificacións por correo electrónico
6767
organization: Organización
6868
phone: Teléfono
69+
postcode:
6970
push_notifications: Recibir notificacións móbiles
7071
registration_date: Data de rexistro
7172
registration_number: Código de persoa usuaria

config/locales/pt-BR.yml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pt-BR:
6666
notifications: Receber notificações
6767
organization: Organização
6868
phone: Telefone
69+
postcode:
6970
push_notifications: Receber notificações pelo celular
7071
registration_date: Data de ingresso
7172
registration_number: Código do usuário
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPostCodeToUsers < ActiveRecord::Migration[6.1]
2+
def change
3+
add_column :users, :postcode, :string
4+
end
5+
end

0 commit comments

Comments
 (0)