Permalink
Browse files

Updated our Rails app data example

  • Loading branch information...
1 parent e2078d4 commit 2f8041b253c3ec75911b8bf45738ab3c17e9ede2 @itsderek23 itsderek23 committed Feb 12, 2010
Showing with 42 additions and 9 deletions.
  1. +42 −9 rails_app_data_example/rails_app_data_example.rb
@@ -1,14 +1,47 @@
+# This is an example plugin for grabbing data from a Rails application and sending it to Scout.
+# Note that we are only loading ActiveRecord, not the entire Rails environment so we have a smaller footprint.
+#
+# To test:
+#
+# scout test rails_app_data_example/rails_app_data_example.rb path_to_app=APP_PATH
+
+$VERBOSE=false # sometimes loading ActiveRecord can be noisy
+
class RailsAppDataExample < Scout::Plugin
- def run
- # load the rails app env
- require "#{@options['path_to_app']}/config/environment"
- data = Hash.new
- data[:shoutouts] = Shoutout.count
- data[:votes] = Vote.count
+ needs 'active_record', 'yaml', 'erb'
+ require 'active_record'
- {:report => data}
- rescue
- {:error => {:subject => "Couldn't retrieve app data as expected.", :body => $!.message} }
+ # define any AR models you need access to
+ class User < ActiveRecord::Base; end
+
+ OPTIONS=<<-EOS
+ path_to_app:
+ name: Full Path to the Rails Application
+ notes: "The full path to the Rails application (ex: /var/www/apps/APP_NAME/current)."
+ rails_env:
+ name: Rails environment that should be used
+ default: production
+ EOS
+
+ def build_report
+ establish_connection
+
+ report_hash = Hash.new
+
+ # grab your datas!
+ report_hash[:users] = User.count
+
+ # send 'em to Scout!
+ report(report_hash)
+ end
+
+ # establish a conection to AR with config options from the Rails App's database.yml file
+ # and provided Rails Environment.
+ def establish_connection
+ app_path = option(:path_to_app)
+ db_config_path = app_path + '/config/database.yml'
+ db_config = YAML::load(ERB.new(File.read(db_config_path)).result)
+ ActiveRecord::Base.establish_connection(db_config[option(:rails_env)])
end
end

0 comments on commit 2f8041b

Please sign in to comment.