Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3848886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:46:10+00:00 2026-05-19T16:46:10+00:00

Seems like there should be a good way via MP to DRY this up:

  • 0

Seems like there should be a good way via MP to DRY this up:

class Dashboard

  def self.num_registrations_past_day
    return User.recent_registrations(24.hours.ago).count
  end

  def self.num_registrations_past_three_days
    return User.recent_registrations(3.days.ago).count
  end

  def self.num_registrations_past_seven_days
    return User.recent_registrations(7.days.ago).count
  end

  def self.num_registrations_past_month
    return User.recent_registrations(30.days.ago).count
  end

  def self.avg_registrations_past_three_days
    return (self.num_registrations_past_three_days / 3.to_f)
  end

  def self.avg_registrations_past_seven_days
    return (self.num_registrations_past_seven_days / 7.to_f)
  end

  def self.avg_registrations_past_month
    return (self.num_registrations_past_month / 30.to_f)
  end

  def self.total_registered_users
    return User.count
  end

  def self.total_activated_users
    return User.total_activated
  end

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:46:11+00:00Added an answer on May 19, 2026 at 4:46 pm

    I would just have the length of time passed in as an argument:

    def self.num_registrations_since(time)
      User.recent_registrations(time).count
    end
    
    def self.avg_registrations_since(time)
      self.num_registrations_since(time) / ((Time.now - time) / 1.day)
    end
    

    See, it’s still very readable:

    Dashboard.num_registrations_since(3.days.ago)
    Dashboard.avg_registrations_since(7.days.ago)
    

    For fun, here is the metaprogramming way:

    { :day => 24.hours.ago,
      :three_days => 3.days.ago,
      :seven_days => 7.days.ago,
      :month => 30.days.ago }.each do |method_suffix, time|
      define_singleton_method "num_registrations_past_#{method_suffix}" do
        User.recent_registrations(time).count
      end
      define_singleton_method "avg_registrations_past_#{method_suffix}" do
        self.send("num_registrations_past_#{method_suffix}") / ((Time.now - time) / 1.day)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like there should be... Right now it just seems like magic that you
It seems like there should be a removalAllOccuring(Collection) (or similiar) method in Multiset. A
It seems like there used to be way more binary protocols because of the
I been wondering about this for a while. It seems like there are so
I have a module like this (but more complicated): module Aliasable def self.included(base) base.has_many
It seems like this should be something built into jQuery without the need for
I am used to the c-style getchar() , but it seems like there is
It seems to me like there's a lot of sheeping going on, with everyone
cgi.escape seems like one possible choice. Does it work well? Is there something that
Well is there? From everything I've read, it seems like the answer is no,but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.