-
Notifications
You must be signed in to change notification settings - Fork 69
/
user.rb
57 lines (48 loc) · 1.6 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
class User < ApplicationRecord
has_many :posts
has_many :other_posts, class_name: "Post"
has_many :comments
has_and_belongs_to_many :roles
def datetime_format
"%d/%m/%y %H:%M"
end
def created
created_at.strftime(datetime_format)
end
def updated
updated_at.strftime(datetime_format)
end
def self.postgres_version
connection.execute("select version()").to_a.first["version"].first(16)
end
private
# Allowlist the User model attributes for sorting, except +password_digest+.
#
# The +full_name+ ransacker is also not included because error-prone in SQL
# ORDER clauses and provided no additional functionality over +first_name+.
#
def self.ransortable_attributes(auth_object = nil)
column_names - ["password_digest"]
end
# Allowlist the User model attributes for search, except +password_digest+,
# as above. The +full_name+ ransacker below is included via +_ransackers.keys+
#
def self.ransackable_attributes(auth_object = nil)
ransortable_attributes + _ransackers.keys
end
# Allowlist the User model associations for sorting, only posts.
#
def self.ransackable_associations(auth_object = nil)
["posts", "other_posts"]
end
# Demonstration of using a "ransacker" (a virtual, searchable "column") to
# allow searching via the full name from concatenated first and last names.
#
ransacker :full_name do |parent|
Arel::Nodes::InfixOperation.new("||",
Arel::Nodes::InfixOperation.new("||",
parent.table[:first_name], Arel::Nodes.build_quoted(" ")),
parent.table[:last_name])
end
end