Module: Growth::ApplicationHelper

Defined in:
app/helpers/growth/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#counts(grouped_models) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/helpers/growth/application_helper.rb', line 17

def counts(grouped_models)
  counts = {}
  grouped_models&.count&.each do |key, value|
    counts.has_key?(value) ? counts[value] += 1 : counts[value] = 1
  end

  counts
end

#get_grouped_options(resources) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/growth/application_helper.rb', line 5

def get_grouped_options(resources)
  resources.map do |model|
    [
        model,
        model.reflect_on_all_associations(:has_many).map do |reflection|
          name = reflection.name.to_s.singularize.camelize
          [name, "#{model}-#{name}"]
        end
    ]
  end
end

#group_resource_by_month(resource, year) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/helpers/growth/application_helper.rb', line 26

def group_resource_by_month(resource, year)
  from, to = Date.parse("#{year.to_i - 1}-12-01"), Date.parse("#{year}-12-31")
  grouped_resource_by_month = resource
                                  .unscoped
                                  .group_by_month(:created_at, range: from..to)
                                  .count

  map_grouped_resource(grouped_resource_by_month, 1.month)
end

#group_resource_by_year(resource, resources) ⇒ Object



36
37
38
39
40
41
42
# File 'app/helpers/growth/application_helper.rb', line 36

def group_resource_by_year(resource, resources)
  from = Date.parse("#{years_since_first_resource(resources).first}-01-01")
  to = Date.parse("#{years_since_first_resource(resources).last}-12-31")

  grouped_resource_by_year = resource.unscoped.group_by_year(:created_at, range: from..to).count
  map_grouped_resource(grouped_resource_by_year, 1.year)
end

#growth_month(resource) ⇒ Object



48
49
50
# File 'app/helpers/growth/application_helper.rb', line 48

def growth_month(resource)
  resource.constantize.where(created_at: Date.current.beginning_of_month..Date.current.end_of_month).count
end

#growth_today(resource) ⇒ Object



44
45
46
# File 'app/helpers/growth/application_helper.rb', line 44

def growth_today(resource)
  resource.constantize.where(created_at: Time.current.beginning_of_day..Time.current.end_of_day).count
end

#growth_year_to_date(resource) ⇒ Object



52
53
54
# File 'app/helpers/growth/application_helper.rb', line 52

def growth_year_to_date(resource)
  resource.constantize.unscoped.where(created_at: Time.current.beginning_of_year..Time.current.end_of_year).count
end

#pluralize_constant(count = nil, constant) ⇒ Object



66
67
68
69
# File 'app/helpers/growth/application_helper.rb', line 66

def pluralize_constant(count = nil, constant)
  return constant.to_s.pluralize if count == nil
  return pluralize(count, constant.to_s)
end

#years_since_first_resource(resources) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'app/helpers/growth/application_helper.rb', line 56

def years_since_first_resource(resources)
  return @years if defined? @years

  mapped_resources = resources.map do |resource|
    resource.unscoped.order(:created_at).first
  end.compact.map(&:created_at).sort

  @years = mapped_resources.empty? ? [Date.current.year] : (mapped_resources.first.to_date.year..Date.current.year).to_a
end