Created
April 14, 2011 11:57
-
-
Save timotta/919334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
class Conteudo | |
include Mongoid::Document | |
field :titulo, :type => String | |
field :tags, :type => Array, :default => [] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
class Tag | |
include Mongoid::Document | |
collection_name = "tags" | |
def self.gerar | |
map = <<-JAVASCRIPT | |
function() { | |
this.tags.forEach( function(tag) { | |
emit(tag, { quantidade: 1 } ); | |
}) | |
} | |
JAVASCRIPT | |
reduce = <<-JAVASCRIPT | |
function(key, values) { | |
var soma = 0; | |
values.forEach( function(value) { | |
soma += value.quantidade; | |
} ); | |
return { quantidade: soma }; | |
} | |
JAVASCRIPT | |
Conteudo.collection.map_reduce(map, reduce, {out: collection_name}) | |
end | |
def self.instantiate(attrs) | |
super(attrs['value'].merge('valor' => attrs['_id'])) | |
end | |
def self.hereditary? | |
return false | |
end | |
Mongoid::Persistence.instance_methods.each do |method| | |
undef_method method | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment