Class: LesliAudit::UserService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_audit/user_service.rb

Instance Method Summary collapse

Instance Method Details

#registrationsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/lesli_audit/user_service.rb', line 36

def registrations

    #Get filters from the request     
    group = query[:group]
    group = "month"

    #Get period only if the request have filters
    period = group unless group.blank?

    group_by = "DATE_TRUNC('month', created_at)" if group == 'month'
    group_by = "DATE_TRUNC('week', created_at)" if group == 'week'
    group_by = "DATE_TRUNC('day', created_at)" if group == 'day'

    # compatibility for SQLite
    if ActiveRecord::Base.connection.adapter_name == "SQLite"
        group_by = "strftime('%Y-%m', created_at)"
    end

    registrations = []

    if ["day", "week", "month", "year"].include?(period)

        registrations = current_user..users
        .group(group_by)
        .count.map do |request|
            {
                :date => request[0],
                :count => request[1]
            }
        end
    end

    registrations
end