Module: TbCore::UserModel::ClassMethods

Defined in:
app/models/concerns/tb_core/user_model.rb

Instance Method Summary collapse

Instance Method Details

#as_csv(column_names) ⇒ Object



59
60
61
62
63
64
65
66
# File 'app/models/concerns/tb_core/user_model.rb', line 59

def as_csv(column_names)
  CSV.generate do |csv|
    csv << column_names
    all.find_each do |item|
      csv << item.attributes.values_at(*column_names)
    end
  end
end

#where_name_like(string) ⇒ Object

Returns an ActiveRecord::Relation performing a LIKE query against name columns



37
38
39
40
41
42
43
44
# File 'app/models/concerns/tb_core/user_model.rb', line 37

def where_name_like(string)
  full_name = Arel::Nodes::NamedFunction.new(
    'concat',
    [arel_table[:first_name], Arel::Nodes.build_quoted(' '), arel_table[:last_name]]
  )
  search = '%' + string + '%'
  where(full_name.matches(search))
end

#with_permission(tag, include_supers: true) ⇒ Object

Return an array of users who have the requested permission

  • tag - Desired permission tag string (required)

  • include_supers - Whether to include super user (default: true)



50
51
52
53
54
55
56
57
# File 'app/models/concerns/tb_core/user_model.rb', line 50

def with_permission(tag, include_supers: true)
  role_ids = SpudRolePermission.where(spud_permission_tag: tag).pluck(:spud_role_id).uniq()
  if include_supers
    where(super_admin: true).or(where(spud_role_id: role_ids))
  else
    where(spud_role_id: role_ids)
  end
end