Skip to content

MongoMapper Example

Florian R. Hanke edited this page · 12 revisions

Setting up Picky to use MongoDB with MongoMapper

Note by Florian: Sorry for the weird example (Discussed in: https://github.com/floere/picky/issues/113). Provided by the community, and I appreciate the work even though the example is very old-fashioned – please improve if you have the time.

For MongoDB awesomeness, just add the following files to your picky-server application.

Add the mongomapper gem to your Gemfile

Gemfile

    gem 'mongo_mapper'

Run

bundle install

lib/initializers/mongo.rb

    settings = YAML.load(File.read(File.expand_path('../../config/mongo.yml', File.dirname(__FILE__))))["#{PICKY_ENVIRONMENT}"]
    MongoMapper.connection = Mongo::Connection.new(settings['host'], settings['port'])
    MongoMapper.database = settings['database']

config/mongo.yml

    development:
      adapter: mongodb
      database: fate_of_men_development
      host: localhost

    production:
      adapter: mongodb
      database: fate_of_men_production
      host: localhost

    test:
      adapter: mongodb
      database: fate_of_men_test
      host: localhost

How to use embedded documents with picky server

There might be several ways to approach this problem, a really cool way is to use a Delegator class. Let us assume, we have a model called man. This man has one wife. The wife has a few wishes.

lib/models/man.rb

    class Man
      include MongoMapper::Document

      key :first_name,     String
      key :last_name,      String
      key :favourite_beer, String
      key :job,            String

      has_one :wife
    end

lib/models/wife.rb

    class Wife
      include MongoMapper::EmbeddedDocument

      key :name,      String

      belongs_to :man
      has_many :wishes      
    end

lib/models/wish.rb

    class Wish
      include MongoMapper::EmbeddedDocument

      key :type,     String
      key :cost,     Integer

      belongs_to :wife
    end

Ok, so how do we help our poor man to keep track of the wishes of his wife with picky power? As mentioned above with a Delegator class

lib/wife_delegator.rb

    module WifeDelegator
      def wife
        wife.name
      end

      def wish_type
        wife.wishes.map(&:type).join(" ") unless wife.wishes.blank?
      end

      def wish_cost
        wife.wishes.map(&:cost).join(" ") unless wife.wishes.blank?
      end
    end

Change your man class to look like this:

lib/models/man.rb

    require "wife_delegator"

    class man
        include MongoMapper::Document
        include WifeDelegator

        ...
    end

Last step is to change the application.rb accordingly:

app/application.rb

    require "lib/models/man"
    require "lib/models/wife"
    require "lib/models/wish"

    class PickySearch < Application   
      ...

      men = Index::Memory.new(:men) do
        source Man.all

        key_format :to_s
        category :first_name,       partial: Partial::Substring.new(from: 1)
        category :last_name,        partial: Partial::Substring.new(from: 1)
        category :favourite_beer,   partial: Partial::Substring.new(from: 3)
        category :job,              partial: Partial::Substring.new(from: 3)

        category :wife,             partial: Partial::Substring.new(from: 1)
        category :wish_type,        partial: Partial::Substring.new(from: 3)
        category :wish_cost,        partial: Partial::Substring.new(from: 3)
      end

      complete_search = Search.new men
      route %r{^/men$} => complete_search
    end

Well, that's all folks.

Something went wrong with that request. Please try again.