Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made a few changes to the examples in the README and also added a lic…

…ense
  • Loading branch information...
commit 40b5d66d19c6e066418a1adfe5db1f858250a539 1 parent 28b513b
alex authored
Showing with 64 additions and 20 deletions.
  1. +22 −0 MIT-LICENSE
  2. +42 −20 README.markdown
View
22 MIT-LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2009 Alexandru Catighera
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
62 README.markdown
@@ -3,25 +3,30 @@
This ActiverRecord plugin allows you to easily define and pull statistics for AR models. This plugin was built with reporting in mind.
## Installation
-script/plugin install git://github.com/acatighera/statistics.git
+ script/plugin install git://github.com/acatighera/statistics.git
## Examples
#### Defining statistics is similar to defining named scopes:
class Account < ActiveRecord::Base
- define_statistic "Basic Count", :count => :all
- define_statistic "Basic Sum", :sum => :all, :column_name => 'amount'
+ 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"
+ end
+
+ class Donations < ActiveRecord::Base
+ define_statistic "Total Donations", :sum => :all, :column_name => "amount"
end
#### Actually pulling the numbers is simple:
#####for all stats
- Account.statistics
+ Account.statistics # returns { 'User Count' => 120, 'Average Age' => 28, 'Subscriber Count' => 74 }
#####for a single stat
- Account.get_stat(‘Basic Count’)
+ Account.get_stat(‘User Count’) # returns 120
### Here are some additional benefits of using this plugin:
@@ -30,13 +35,14 @@ script/plugin install git://github.com/acatighera/statistics.git
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 "Basic Count", :count => :all
- define_statistic "Custom Count", :count => :all, :filter_on => { :channel => 'channel = ?', :start_date => 'DATE(created_at) > ?'}
- filter_all_stats_on(:user_id, "user_id = ?")
+ 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"
+
+ filter_all_stats_on(:account_type, "account_type = ?")
end
- Account.statistics(:user_id => 5)
- Account.get_stat(‘Custom Count’, :user_id => 5, :start_date => ‘2009-01-01’)
+ Account.statistics(:account_type => 'non-admin')
+ Account.get_stat(‘User Count’, :account_type => 'non-admin', :created_after => ‘2009-01-01’, :state => 'NY')
#### Standardized
@@ -47,28 +53,42 @@ All ActiveRecord classes now respond to `statistics` and `get_stat` methods
all_stats << ar.statistics
end
- Account.get_stat(“Basic Count’)
-
#### Calculated statistics (DRY)
-You can define calculated metrics in order to perform mathematical calculations on one or more defined statistics. (These calculated metrics also work with filters!)
+You can define calculated metrics in order to perform mathematical calculations on one or more defined statistics.
class Account < ActiveRecord::Base
- define_statistic "Basic Count", :count => :all
- define_statistic "Basic Sum", :sum => :all, :column_name => 'amount'
- define_calculated_statistic "Total Amount" do
- defined_stats('Basic Sum') * defined_stats('Basic Count')
+ has_many :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')
end
+
+ filter_all_stats_on(:account_type, "account_type = ?")
+ filter_all_stats_on(:state, "state = ?")
+ filter_all_stats_on(:created_after, "DATE(created_at) > ?")
end
+
+
+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')
#### Reuse scopes you already have defined
You can reuse the code you have written to do reporting.
class Account < ActiveRecord::Base
- named_scope :scope1, :conditions => “status = ‘active’”
- named_scope :scope2, :joins => :posts
- define_statistic "Chained Scope Count", :count => [:scope1, :scope2]
+ has_many :posts
+
+ 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]
end
#### Accepts all ActiveRecord::Calculations options
@@ -78,3 +98,5 @@ 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'"
end
+
+###### Copyright (c) 2009 Alexandru Catighera, released under MIT license
Please sign in to comment.
Something went wrong with that request. Please try again.