Skip to content

feat: tags and postal code attributes #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
26 changes: 26 additions & 0 deletions app/assets/javascripts/application/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ $(function() {
loadTags('inquiry');
});

$(".switch_member-js").on("click", function() {
loadTags('user');
});

function loadTags(type){
$.get({
url: `/tags/alpha_grouped_index.js?post_type=${type}`,
Expand Down Expand Up @@ -41,4 +45,26 @@ $(function() {
}
}
});

$('#tags-js-members').select2({
tags: true,
tokenSeparators: [','],
dataType: 'json',
delay: 250,
ajax: {
url: '/tags.json',
data: function(params) {
console.log(params);
return { term: params.term, member: "true" };
},
processResults: function(data, params) {
return {
results: $.map(data, item => ({
id: item,
text: item
}))
};
}
}
});
});
6 changes: 6 additions & 0 deletions app/assets/stylesheets/application/member-card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
}
}

&__tags {
a {
color: white;
}
}

&__activity {
font-size: 14px;
color: #78adb9;
Expand Down
21 changes: 13 additions & 8 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class TagsController < ApplicationController
before_action :authenticate_user!, :member_should_be_active

def index
posts = Post.by_organization(current_organization)
@all_tags = posts.find_like_tag(params[:term])
model = params.key?(:member) ? Member : Post
model_organization = model.by_organization(current_organization)
@all_tags = model_organization.find_like_tag(params[:term])

render json: @all_tags
end
Expand All @@ -13,17 +14,21 @@ def alpha_grouped_index

post_type = params[:post_type] || "offer"
@alpha_tags = case post_type
when "offer" then Offer
when "inquiry" then Inquiry
end.by_organization(current_organization).
active.of_active_members.
alphabetical_grouped_tags

when "offer" then post_tags Offer
when "inquiry" then post_tags Inquiry
when "user" then Member.by_organization(current_organization).active
end.alphabetical_grouped_tags
respond_to do |format|
format.html
format.js do
render partial: "grouped_index", locals: { alpha_tags: @alpha_tags, post_type: post_type }
end
end
end

private

def post_tags(type)
type.by_organization(current_organization).active.of_active_members
end
end
20 changes: 16 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class UsersController < ApplicationController
before_action :authenticate_user!, :member_should_be_active

has_scope :tagged_with, as: :tag

def index
search_and_load_members current_organization.members.active, { s: 'user_last_sign_in_at DESC' }
context = current_organization.members.active
members = apply_scopes(context)

search_and_load_members members, { s: 'user_last_sign_in_at DESC' }
end

def manage
Expand All @@ -18,11 +23,13 @@ def show

def new
authorize User
@member = Member.new
@user = scoped_users.build
end

def edit
@user = find_user
@member = @user.as_member_of(current_organization)
end

def create
Expand All @@ -36,7 +43,7 @@ def create
@user.setup_and_save_user

if @user.persisted?
@user.tune_after_persisted(current_organization)
@user.tune_after_persisted(current_organization, get_tags)
redirect_to_after_create
else
@user.email = "" if empty_email
Expand All @@ -46,9 +53,10 @@ def create

def update
@user = scoped_users.find(params[:id])
@member = @user.as_member_of(current_organization)
authorize @user

if @user.update(user_params)
if @user.update(user_params) && @member.update(tags: get_tags)
redirect_to @user
else
render action: :edit, status: :unprocessable_entity
Expand Down Expand Up @@ -76,7 +84,7 @@ def scoped_users

def user_params
fields_to_permit = %w"gender username email date_of_birth phone
alt_phone active description notifications push_notifications"
alt_phone active description notifications push_notifications postcode"
fields_to_permit += %w"admin registration_number
registration_date" if admin?
fields_to_permit += %w"organization_id superadmin" if superadmin?
Expand Down Expand Up @@ -106,4 +114,8 @@ def redirect_to_after_create
name: @user.username)
end
end

def get_tags
params.key?(:tag_list) ? params.require(:tag_list) : []
end
end
2 changes: 1 addition & 1 deletion app/decorators/member_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MemberDecorator < ViewModel
delegate :user, :member_uid, :active?, to: :object
delegate :user, :member_uid, :tags, :active?, to: :object
delegate :phone, :alt_phone, :username, :description, :last_sign_in_at, to: :user

def manager?
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Taggable
extend ActiveSupport::Concern

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

def tag_list
Expand Down
6 changes: 6 additions & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class Member < ApplicationRecord
include Taggable
# Cast the member_uid integer to a string to allow pg ILIKE search (from Ransack *_contains)
ransacker :member_uid_search do
Arel.sql("member_uid::text")
end

ransacker :member_tags do
Arel.sql("array_to_string(tags, ',')")
end

belongs_to :user
belongs_to :organization
has_one :account, as: :accountable
Expand All @@ -15,6 +20,7 @@ class Member < ApplicationRecord

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

validates :organization_id, presence: true
validates :member_uid,
Expand Down
8 changes: 4 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def user
self
end

def add_to_organization(organization)
def add_to_organization(organization, tag_list = [])
return unless organization

member = members.where(organization: organization).first_or_initialize

return member if member.persisted?

member.entry_date = DateTime.now.utc

member.tags = tag_list
persister = ::Persister::MemberPersister.new(member)
persister.save

Expand Down Expand Up @@ -102,8 +102,8 @@ def setup_and_save_user
save
end

def tune_after_persisted(organization)
add_to_organization organization
def tune_after_persisted(organization, tag_list = [])
add_to_organization organization, tag_list

# If email was empty, udpate again with user.id just generated
set_dummy_email if empty_email
Expand Down
4 changes: 4 additions & 0 deletions app/views/tags/alpha_grouped_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<%= radio_button_tag 'switch_inquiry', 'inquiry' %>
<%= Inquiry.model_name.human count: :many %>
</label>
<label class="btn btn-primary switch_member-js">
<%= radio_button_tag 'switch_member', 'member' %>
<%= t "users.index.members" %>
</label>
</div>
<% end %>
</h1>
Expand Down
10 changes: 10 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
end_year: Date.today.year - 12,
include_blank: :true %>
<%= f.input :description, as: "text" %>
<%= f.input :postcode %>
<%= label_tag :tag_list, t('activerecord.attributes.post.tag_list') %>
<div class='form-group'>
<%= select_tag :tag_list,
options_for_select(@member.tags, @member.tags),
multiple: true,
data: { placeholder: t('application.tips.entertag')},
id: "tags-js-members",
class: "form-control" %>
</div>

<div class='form-group'>
<label><%= t('.notifications') %></label>
Expand Down
11 changes: 11 additions & 0 deletions app/views/users/_member_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
<% else %>
<%= t('.no_activity') %>
<% end %>
<% member.tags[0..2].each do |tag| %>
<span class="to-member-card__header__text__tags">
<%= link_to users_path tag: tag do %>
<%= glyph :tag %>
<%= tag&.truncate(29) %>
<% end %>
</span>
<% end %>
<% if member.tags.size > 3 %>
...
<% end %>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="col-md-12">
<%= search_form_for(@search, class: "navbar-form navbar-left", url: users_path) do |f| %>
<div class="form-group">
<%= f.search_field :user_username_or_user_email_or_member_uid_search_contains, class: "form-control" %>
<%= f.search_field :user_username_or_user_email_or_member_tags_or_member_uid_search_contains, class: "form-control" %>
</div>
<button class="btn btn-default" type="submit">
<%= t 'global.search' %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/manage.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="col-md-12">
<%= search_form_for(@search, class: "navbar-form navbar-left", url: manage_users_path) do |f| %>
<div class="form-group">
<%= f.search_field :user_username_or_user_email_or_user_phone_or_user_alt_phone_or_member_uid_search_contains, class: "form-control" %>
<%= f.search_field :user_username_or_user_email_or_user_phone_or_user_alt_phone_or_member_tags_or_member_uid_search_contains, class: "form-control" %>
</div>
<button class="btn btn-default" type="submit">
<%= t 'global.search' %>
Expand Down
23 changes: 23 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@
<% end %>
</dd>
<% end %>
<% if @user.postcode.present? %>
<dt>
<%= t "activerecord.attributes.user.postcode" %>
</dt>
<dd>
<%= @user.postcode %>
</dd>
<% end %>
<% if @member.tags.present? %>
<dt>
<%= t "application.navbar.tags" %>
</dt>
<dd>
<% @member.tags.each do |tag| %>
<span class="badge alert-success">
<%= link_to users_path tag: tag do %>
<%= glyph(:tag) %>
<%= tag %>
<% end %>
</span>
<% end %>
</dd>
<% end %>
</dl>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ en:
unconfirmed_email: Unconfirmed Email
updated_at: Updated
username: Name
postcode: Post code
errors:
models:
organization:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20210423193937_add_post_code_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPostCodeToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :postcode, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20210424174640_add_tags_to_members.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTagsToMembers < ActiveRecord::Migration[6.1]
def change
add_column :members, :tags, :text, array: true, default: []
end
end
Loading