Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bumped version to 0.4. Remove dependency on database_cleaner. Removed…

… internal Mongoid field for tracking fields.
  • Loading branch information...
commit e54a8b88cfa9cbdd9341a508b6f13c26881dfe76 1 parent 933465c
@twoixter authored
View
2  Gemfile
@@ -3,4 +3,4 @@ source "http://rubygems.org"
# Declare your gem's dependencies in front.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
-gemspec
+gemspec
View
3  config/mongoid.yml
@@ -4,4 +4,5 @@ test:
database: trackoid_test
hosts:
- 127.0.0.1:27017
-
+ options:
+ allow_dynamic_fields: false
View
29 lib/mongoid/tracking.rb
@@ -10,10 +10,10 @@ def self.included(base)
unless self.ancestors.include? Mongoid::Document
raise Errors::NotMongoid, "Must be included in a Mongoid::Document"
end
-
+
include Aggregates
extend ClassMethods
-
+
class_attribute :tracked_fields
self.tracked_fields = []
delegate :tracked_fields, :internal_track_name, to: "self.class"
@@ -41,15 +41,12 @@ def internal_track_name(name)
# Configures the internal fields for tracking. Additionally also creates
# an index for the internal tracking field.
def set_tracking_field(name)
- field internal_track_name(name), type: Hash # , :default => {}
-
# DONT make an index for this field. MongoDB indexes have limited
# size and seems that this is not a good target for indexing.
# index internal_track_name(name)
-
tracked_fields << name
end
-
+
# Creates the tracking field accessor and also disables the original
# ones from Mongoid. Hidding here the original accessors for the
# Mongoid fields ensures they doesn't get dirty, so Mongoid does not
@@ -58,29 +55,13 @@ def create_tracking_accessors(name)
define_method(name) do |*aggr|
Tracker.new(self, name, aggr)
end
-
- # Should we just "undef" this methods?
- # They override the ones defined from Mongoid
- define_method("#{name}_data") do
- raise NoMethodError
- end
-
- define_method("#{name}_data=") do |m|
- raise NoMethodError
- end
-
- # I think it's important to override also the #{name}_changed? so
- # as to be sure Mongoid never mark this field as dirty.
- define_method("#{name}_changed?") do
- false
- end
end
-
+
# Updates the aggregated class for it to include a new tracking field
def update_aggregates(name)
aggregate_klass.track name
end
-
+
end
end
View
6 lib/mongoid/tracking/tracker.rb
@@ -10,9 +10,7 @@ def initialize(owner, field, aggregate_data)
@owner, @for = owner, field
@for_data = @owner.internal_track_name(@for)
@data = @owner.read_attribute(@for_data)
-
- # The following is needed if the "field" Mongoid definition for our
- # internal tracking field does not include option ":default => {}"
+
if @data.nil?
@owner.write_attribute(@for_data, {})
@data = @owner.read_attribute(@for_data)
@@ -79,7 +77,7 @@ def set(how_much, date = Time.now)
next unless token = v.call(@aggregate_data)
fk = @owner.class.name.to_s.foreign_key.to_sym
selector = { fk => @owner.id, ns: k, key: token.to_s }
-
+
docs = @owner.aggregate_klass.collection.find(selector)
docs.upsert("$set" => update_hash(how_much.abs, date))
end
View
4 lib/trackoid/version.rb
@@ -1,5 +1,5 @@
# encoding: utf-8
module Trackoid #:nodoc:
- VERSION = "0.3.8"
-end
+ VERSION = "0.4.0"
+end
View
6 spec/spec_helper.rb
@@ -4,17 +4,15 @@
require 'mongoid'
require 'trackoid'
-require 'database_cleaner'
require 'rspec'
require 'rspec/autorun'
RSpec.configure do |config|
config.before(:suite) do
Mongoid.load!(File.expand_path(File.dirname(__FILE__) + "/../config/mongoid.yml"), :test)
- DatabaseCleaner[:mongoid].strategy = :truncation
end
config.after(:each) do
- DatabaseCleaner.clean
+ Mongoid::Config.purge!

Ah, yeah, that's a lot better :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
-end
+end
View
37 spec/trackoid_spec.rb
@@ -8,7 +8,11 @@ class Test
track :visits
end
-describe Mongoid::Tracking do
+describe Mongoid::Tracking do
+ it "should ensure allow_dynamic_fields option is turned off" do
+ Mongoid::Config.settings[:allow_dynamic_fields].should == false
+ end
+
it "should raise error when used in a class not of class Mongoid::Document" do
-> {
class NotMongoidClass
@@ -47,11 +51,6 @@ class MongoidedDocument
@mock = Test.new
end
- it "should deny access to the underlying mongoid field" do
- -> { @mock.visits_data }.should raise_error NoMethodError
- -> { @mock.visits_data = {} }.should raise_error NoMethodError
- end
-
it "should create a method for accesing the stats" do
@mock.respond_to?(:visits).should be_true
end
@@ -60,12 +59,6 @@ class MongoidedDocument
@mock.class.index_options.should_not include(:visits_data)
end
- it "should respond 'false' to field_changed? method" do
- # Ok, this test is not very relevant since it will return false even
- # if Trackoid does not override it.
- @mock.visits_changed?.should be_false
- end
-
it "should create a method for accesing the stats of the proper class" do
@mock.visits.class.should == Mongoid::Tracking::Tracker
end
@@ -105,7 +98,7 @@ class Test
@mock.aggregated?.should be_false
end
end
-
+
describe "when using a model in the database" do
let(:test) { Test.create(:name => "test") }
@@ -134,7 +127,7 @@ class Test
test.visits.inc(@today - 1.day)
test.visits.yesterday.should == 2
end
-
+
it "then, the visits of today + yesterday must be the same" do
test.visits.inc
test.visits.inc
@@ -155,7 +148,7 @@ class Test
test.visits.inc
test.visits.inc
test.visits.inc(@today - 1.day)
- test.visits.inc(@today - 1.day)
+ test.visits.inc(@today - 1.day)
test.visits.last_days.should == [0, 0, 0, 0, 0, 2, 2]
end
@@ -179,13 +172,13 @@ class Test
end
it "should give the first value" do
- test.visits.inc("2010-07-11")
+ test.visits.inc("2010-07-11")
test.visits.first_value.should == 1
end
it "should give the last value" do
future = @today + 1.month
- test.visits.set(22, future)
+ test.visits.set(22, future)
test.visits.last_value.should == 22
end
end
@@ -207,33 +200,33 @@ class Test
end
it "'add' operator must work" do
- test.visits.set(5)
+ test.visits.set(5)
test.visits.add(5)
test.visits.today.should == 10
Test.find(object_id).visits.today.should == 10
end
it "'add' operator must work on arbitrary days" do
- test.visits.set(5, Time.parse("2010-05-01"))
+ test.visits.set(5, Time.parse("2010-05-01"))
test.visits.add(5, Time.parse("2010-05-01"))
test.visits.on(Time.parse("2010-05-01")).should == 10
Test.find(object_id).visits.on(Time.parse("2010-05-01")).should == 10
end
it "on() accessor must work on dates as String" do
- test.visits.set(5, Time.parse("2010-05-01"))
+ test.visits.set(5, Time.parse("2010-05-01"))
test.visits.add(5, Time.parse("2010-05-01"))
test.visits.on("2010-05-01").should == 10
end
it "on() accessor must work on Date descendants" do
- test.visits.set(5, Time.parse("2010-05-01"))
+ test.visits.set(5, Time.parse("2010-05-01"))
test.visits.add(5, Time.parse("2010-05-01"))
test.visits.on(Date.parse("2010-05-01")).should == 10
end
it "on() accessor must work on dates as Ranges" do
- test.visits.set(5, Time.parse("2010-05-01"))
+ test.visits.set(5, Time.parse("2010-05-01"))
test.visits.add(5, Time.parse("2010-05-01"))
test.visits.on(Time.parse("2010-04-30")..Time.parse("2010-05-02")).should == [0, 10, 0]
end
View
3  trackoid.gemspec
@@ -5,7 +5,7 @@ require "trackoid/version"
Gem::Specification.new do |s|
s.name = "trackoid"
- s.description = "Trackoid uses an embeddable approach to track analytics data using the poweful features of MongoDB for scalability"
+ s.description = "Trackoid uses an embeddable approach to track analytics data using the poweful features of MongoDB for scalability"
s.summary = "Trackoid is an easy scalable analytics tracker using MongoDB and Mongoid"
s.version = Trackoid::VERSION
s.authors = ["Jose Miguel Perez"]
@@ -46,6 +46,5 @@ Gem::Specification.new do |s|
s.add_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'mocha'
- s.add_development_dependency 'database_cleaner'
end
@mattiassvedhem

Ah, yeah, that's a lot better :).

Please sign in to comment.
Something went wrong with that request. Please try again.