Permalink
Browse files

Added ability to use symbol instead of string for stat name (changed …

…README to reflect this as well)
  • Loading branch information...
alex
alex committed May 27, 2009
1 parent 40b5d66 commit 1a687fd3f893247328faf24c91373e6f3848543c
Showing with 33 additions and 25 deletions.
  1. +19 −17 README.markdown
  2. +2 −2 lib/statistics.rb
  3. +12 −6 test/statistics_test.rb
View
@@ -9,24 +9,24 @@ This ActiverRecord plugin allows you to easily define and pull statistics for AR
#### Defining statistics is similar to defining named scopes:
class Account < ActiveRecord::Base
- define_statistic "User Count", :count => :all
- define_statistic "Average Age", :average => :all, :column_name => 'age'
- define_statistic "Subcriber Count", :count => :all, :conditions => "subscription_opt_in = 1"
+ define_statistic :user_count, :count => :all
+ define_statistic :average_age, :average => :all, :column_name => 'age'
+ define_statistic :subscriber_count, :count => :all, :conditions => "subscription_opt_in = 1"
end
class Donations < ActiveRecord::Base
- define_statistic "Total Donations", :sum => :all, :column_name => "amount"
+ define_statistic :total_donations, :sum => :all, :column_name => "amount"
end
#### Actually pulling the numbers is simple:
#####for all stats
- Account.statistics # returns { 'User Count' => 120, 'Average Age' => 28, 'Subscriber Count' => 74 }
+ Account.statistics # returns { :user_count => 120, :average_age => 28, :subscriber_count => 74 }
#####for a single stat
- Account.get_stat(‘User Count’) # returns 120
+ Account.get_stat(‘:user_count’) # returns 120
### Here are some additional benefits of using this plugin:
@@ -35,14 +35,14 @@ This ActiverRecord plugin allows you to easily define and pull statistics for AR
Note: I found filtering to be an important part of reporting (ie. filtering by date). All filters are optional so even if you define them you don’t have to use them when pulling data. Using the `filter_all_stats_on` method and `:joins` options you can make things filterable by the same things which I found to be extremely useful.
class Account < ActiveRecord::Base
- define_statistic "User Count", :count => :all, , :filter_on => { :state => 'state = ?', :created_after => 'DATE(created_at) > ?'}
- define_statistic "Subcriber Count", :count => :all, :conditions => "subscription_opt_in = true"
+ define_statistic :user_count, :count => :all, , :filter_on => { :state => 'state = ?', :created_after => 'DATE(created_at) > ?'}
+ define_statistic :subscriber_count, :count => :all, :conditions => "subscription_opt_in = true"
filter_all_stats_on(:account_type, "account_type = ?")
end
Account.statistics(:account_type => 'non-admin')
- Account.get_stat(‘User Count’, :account_type => 'non-admin', :created_after => ‘2009-01-01’, :state => 'NY')
+ Account.get_stat(:user_count, :account_type => 'non-admin', :created_after => ‘2009-01-01’, :state => 'NY')
#### Standardized
@@ -60,11 +60,11 @@ You can define calculated metrics in order to perform mathematical calculations
class Account < ActiveRecord::Base
has_many :donations
- define_statistic "User Count", :count => :all
- define_statistic "Total Donations", :sum => :all, :column_name => 'donations.amount', :joins => :donations
+ define_statistic :user_count, :count => :all
+ define_statistic :total_donations, :sum => :all, :column_name => 'donations.amount', :joins => :donations
- define_calculated_statistic "Average Donation per User" do
- defined_stats('Total Donations') / defined_stats('User Count')
+ define_calculated_statistic :average_donation_per_user do
+ defined_stats(:total_donations) / defined_stats(:user_count)
end
filter_all_stats_on(:account_type, "account_type = ?")
@@ -75,8 +75,8 @@ You can define calculated metrics in order to perform mathematical calculations
Pulling stats for calculated metrics is the same as for regular statistics. They also work with filters like regular statistics!
- Account.get_stat('Average Donation Per User', :account_type => 'non-admin', :state => 'NY')
- Account.get_stat('Average Donation Per User', :created_after => '2009-01-01')
+ Account.get_stat(:average_donation_per_user, :account_type => 'non-admin', :state => 'NY')
+ Account.get_stat(:average_donation_per_user, :created_after => '2009-01-01')
#### Reuse scopes you already have defined
@@ -88,15 +88,17 @@ You can reuse the code you have written to do reporting.
named_scope :not_admins, :conditions => “account_type = ‘non-admin’”
named_scope :accounts_with_posts, :joins => :posts
- define_statistic "Active Users Count", :count => [:not_admins, :accounts_with_posts]
+ define_statistic :active_users_count, :count => [:not_admins, :accounts_with_posts]
end
#### Accepts all ActiveRecord::Calculations options
The `:conditions` and `:joins` options are all particularly useful
class Account < ActiveRecord::Base
- define_statistic "Active Accounts With Posts", :count => :all, :joins => :posts, :conditions => "status = 'active'"
+ has_many :posts
+
+ define_statistic :active_users_count, :count => :all, :joins => :posts, :conditions => "account_type = 'non-admin'"
end
###### Copyright (c) 2009 Alexandru Catighera, released under MIT license
View
@@ -41,7 +41,7 @@ module HasStats
# define_statistic "Custom Filter", :count => :all, :filter_on => { :channel => 'channel = ?', :start_date => 'DATE(created_at) > ?' }
# end
def define_statistic(name, options)
- method_name = name.gsub(" ", "").underscore + "_stat"
+ method_name = name.to_s.gsub(" ", "").underscore + "_stat"
@statistics ||= {}
@filter_all_on ||= ActiveRecord::Base.instance_eval { @filter_all_on }
@@ -91,7 +91,7 @@ def define_statistic(name, options)
# defined_stats('Basic Sum') * defined_stats('Basic Count')
# end
def define_calculated_statistic(name, &block)
- method_name = name.gsub(" ", "").underscore + "_stat"
+ method_name = name.to_s.gsub(" ", "").underscore + "_stat"
@statistics ||= {}
@statistics[name] = method_name
View
@@ -14,6 +14,7 @@ class StatisticsTest < Test::Unit::TestCase
class MockModel < ActiveRecord::Base
define_statistic "Basic Count", :count => :all
+ define_statistic :symbol_count, :count => :all
define_statistic "Basic Sum", :sum => :all, :column_name => 'amount'
define_statistic "Chained Scope Count", :count => [:all, :named_scope]
define_statistic "Default Filter", :count => :all
@@ -28,20 +29,25 @@ class MockModel < ActiveRecord::Base
def test_statistics
MockModel.expects(:basic_count_stat).returns(2)
+ MockModel.expects(:symbol_count_stat).returns(2)
MockModel.expects(:basic_sum_stat).returns(27)
MockModel.expects(:chained_scope_count_stat).returns(4)
MockModel.expects(:default_filter_stat).returns(5)
MockModel.expects(:custom_filter_stat).returns(3)
MockModel.expects(:total_amount_stat).returns(54)
- assert_equal(["Basic Count",
- "Basic Sum",
- "Chained Scope Count",
- "Default Filter",
- "Custom Filter",
- "Total Amount"].sort, MockModel.statistics_keys.sort)
+ ["Basic Count",
+ :symbol_count,
+ "Basic Sum",
+ "Chained Scope Count",
+ "Default Filter",
+ "Custom Filter",
+ "Total Amount"].each do |key|
+ assert MockModel.statistics_keys.include?(key)
+ end
assert_equal({ "Basic Count" => 2,
+ :symbol_count => 2,
"Basic Sum" => 27,
"Chained Scope Count" => 4,
"Default Filter" => 5,

0 comments on commit 1a687fd

Please sign in to comment.