Permalink
Browse files

Renaming Project to make it more magical

  • Loading branch information...
1 parent de306ca commit 3a07465f0eb1e6b0ba9deccf799e6ee1fb1e4f5c @jah2488 committed Feb 26, 2012
View
@@ -1,4 +1,4 @@
source "http://rubygems.org"
-# Specify your gem's dependencies in mongoid_counter_cache.gemspec
+# Specify your gem's dependencies in mongoid_magic_counter_cache.gemspec
gemspec
View
@@ -1,4 +1,4 @@
-Mongoid Counter Cache
+Mongoid Magic Counter Cache
=======
## DESCRIPTION
@@ -10,11 +10,11 @@ Mongoid Counter Cache is a simple mongoid extension to add basic counter cache f
### RubyGems
- $ [sudo] gem install mongoid_counter_cache
+ $ [sudo] gem install mongoid_magic_counter_cache
### GemFile
- gem 'mongoid_counter_cache'
+ gem 'mongoid_magic_counter_cache'
## USAGE
@@ -30,17 +30,17 @@ First add a field to the document where you will be accessing the counter cache
end
-Then in the referrenced/Embedded document. Include `Mongoid::CounterCache`
+Then in the referrenced/Embedded document. Include `Mongoid::MagicCounterCache`
class Book
include Mongoid::Document
- include Mongoid::CounterCache
+ include Mongoid::MagicCounterCache
field :first
field :last
belongs_to :library
- counter_cache :library
+ magic_counter_cache :library
end
@@ -51,13 +51,13 @@ Then in the referrenced/Embedded document. Include `Mongoid::CounterCache`
If you do not wish to use the `model_count` naming convention, you can override the defaults by specifying the `:field` parameter.
- counter_cache :library, :field => "total_amount_of_books"
+ magic_counter_cache :library, :field => "total_amount_of_books"
## TODO
1. Thoroughly Test embedded associations
2. Add additional options parameters
- 3. Simplify syntax (I.E. including CounterCache will add counts for all `belongs_to` associations on a document
+ 3. Simplify syntax (I.E. including MagicCounterCache will add counts for all `belongs_to` associations on a document
@@ -0,0 +1,5 @@
+module Mongoid
+ module MagicCounterCache
+ VERSION = "0.0.1.beta"
+ end
+end
@@ -0,0 +1,84 @@
+require 'mongoid'
+require 'pry'
+module Mongoid #:nodoc:
+
+ # The Counter Cache will yada yada
+ #
+ # class Person
+ # include Mongoid::Document
+ #
+ # field :name
+ # field :feeling_count
+ # has_many :feelings
+ # end
+ #
+ # class Feeling
+ # include Mongoid::Document
+ # include Mongoid::MagicCounterCache
+ #
+ # field :name
+ # belongs_to :person
+ # counter_cache :person
+ # end
+ #
+ # Alternative Syntax
+ #
+ # class Person
+ # include Mongoid::Document
+ #
+ # field :name
+ # field :all_my_feels
+ # has_many :feelings
+ # end
+ #
+ # class Feeling
+ # include Mongoid::Document
+ # include Mongoid::MagicCounterCache
+ #
+ # field :name
+ # belongs_to :person
+ # counter_cache :person, :field => "all_my_feels"
+ # end
+ module MagicCounterCache
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ def counter_cache(*args, &block)
+ options = args.extract_options!
+ name = options[:class] || args.first.to_s
+
+ if options[:field]
+ counter_name = "#{options[:field].to_s}"
+ else
+ counter_name = "#{model_name.downcase}_count"
+ end
+ after_create do |doc|
+ if doc.embedded?
+ parent = doc._parent
+ parent.inc(counter_name.to_sym, 1) if parent.respond_to? counter_name
+ else
+ relation = doc.send(name)
+ if relation && relation.class.fields.keys.include?(counter_name)
+ relation.inc(counter_name.to_sym, 1)
+ end
+ end
+ end
+
+ after_destroy do |doc|
+ if doc.embedded?
+ parent = doc._parent
+ parent.inc(counter_name.to_sym, -1) if parent.respond_to? counter_name
+ else
+ relation = doc.send(name)
+ if relation && relation.class.fields.keys.include?(counter_name)
+ relation.inc(counter_name.to_sym, -1)
+ end
+ end
+ end
+
+ end
+ end
+
+ end
+end
@@ -0,0 +1 @@
+require "mongoid/magic_counter_cache"
@@ -0,0 +1,3 @@
+module MongoidMagicCounterCache
+ VERSION = "0.0.1.beta"
+end
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "mongoid/magic-counter-cache/version"
+
+Gem::Specification.new do |s|
+ s.name = "mongoid_magic_counter_cache"
+ s.version = Mongoid::MagicCounterCache::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Justin Herrick"]
+ s.email = ["justin@justinherrick.com"]
+ s.homepage = "https://github.com/jah2488/mongoid-magic-counter-cache"
+ s.summary = %q{Setup Counter Caches in Mongoid Documents}
+ s.description = %q{A quick and easy way to add counter cache functionality to model/document associations in Mongoid}
+
+ s.rubyforge_project = "mongoid_magic_counter_cache"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency("mongoid", "2.2.6")
+ s.add_dependency("rake")
+ s.add_dependency("bson_ext","~> 1.5")
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "pry"
+end
View
@@ -1,6 +1,6 @@
class Book
include Mongoid::Document
- include Mongoid::CounterCache
+ include Mongoid::MagicCounterCache
belongs_to :library
embeds_many :pages
View
@@ -1,6 +1,6 @@
class Feeling
include Mongoid::Document
- include Mongoid::CounterCache
+ include Mongoid::MagicCounterCache
belongs_to :person
counter_cache :person, :field => "all_my_feels"
View
@@ -1,6 +1,6 @@
class Page
include Mongoid::Document
- include Mongoid::CounterCache
+ include Mongoid::MagicCounterCache
embedded_in :book
counter_cache :book, :type => Integer, :default => 0
View
@@ -1,6 +1,6 @@
class Song
include Mongoid::Document
- include Mongoid::CounterCache
+ include Mongoid::MagicCounterCache
embedded_in :album
counter_cache :album
field :title
Oops, something went wrong.

0 comments on commit 3a07465

Please sign in to comment.