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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 13 additions & 10 deletions
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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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>

0 commit comments

Comments
 (0)