Skip to content

Commit

Permalink
Bumped version to 0.4. Remove dependency on database_cleaner. Removed…
Browse files Browse the repository at this point in the history
… internal Mongoid field for tracking fields.
  • Loading branch information
twoixter committed Oct 11, 2012
1 parent 933465c commit e54a8b8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion config/mongoid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ test:
database: trackoid_test
hosts:
- 127.0.0.1:27017

options:
allow_dynamic_fields: false
29 changes: 5 additions & 24 deletions lib/mongoid/tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/mongoid/tracking/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/trackoid/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8

module Trackoid #:nodoc:
VERSION = "0.3.8"
end
VERSION = "0.4.0"
end
6 changes: 2 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!

This comment has been minimized.

Copy link
@mattiassvedhem

mattiassvedhem Oct 12, 2012

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

end
end
end
37 changes: 15 additions & 22 deletions spec/trackoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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") }

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions trackoid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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

0 comments on commit e54a8b8

Please sign in to comment.