Permalink
Browse files

initial commit

  • Loading branch information...
1 parent e306646 commit 6f6ac0701cb2284ef6168996b3ef3d6ad5adb851 @kristianmandrup committed Sep 23, 2012
Showing with 172 additions and 47 deletions.
  1. +1 −1 .gitignore
  2. +7 −10 Gemfile
  3. +54 −0 Gemfile.lock
  4. +52 −0 README.md
  5. +0 −19 README.rdoc
  6. +2 −16 Rakefile
  7. +1 −1 VERSION
  8. +55 −0 lib/mongoid/indexing.rb
View
@@ -28,7 +28,7 @@ pkg
#
# For MacOS:
#
-#.DS_Store
+.DS_Store
# For TextMate
#*.tmproj
View
@@ -1,14 +1,11 @@
source "http://rubygems.org"
-# Add dependencies required to use your gem here.
-# Example:
-# gem "activesupport", ">= 2.3.5"
-# Add dependencies to develop your gem here.
-# Include everything needed to run rake, tests, features, etc.
+gem 'mongoid'
+
group :development do
- gem "rspec", "~> 2.8.0"
- gem "rdoc", "~> 3.12"
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.8.4"
- gem "rcov", ">= 0"
+ gem "rspec", ">= 2.8.0"
+ gem "rdoc", ">= 3.12"
+ gem "bundler", ">= 1.0.0"
+ gem "jeweler", ">= 1.8.4"
+ gem "simplecov",">= 0.5"
end
View
@@ -0,0 +1,54 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ builder (3.0.3)
+ diff-lcs (1.1.3)
+ git (1.2.5)
+ i18n (0.6.1)
+ jeweler (1.8.4)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ json (1.7.5)
+ mongoid (3.0.6)
+ activemodel (~> 3.1)
+ moped (~> 1.1)
+ origin (~> 1.0)
+ tzinfo (~> 0.3.22)
+ moped (1.2.3)
+ multi_json (1.3.6)
+ origin (1.0.9)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.3)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.3)
+ simplecov (0.6.4)
+ multi_json (~> 1.0)
+ simplecov-html (~> 0.5.3)
+ simplecov-html (0.5.3)
+ tzinfo (0.3.33)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (>= 1.0.0)
+ jeweler (>= 1.8.4)
+ mongoid
+ rdoc (>= 3.12)
+ rspec (>= 2.8.0)
+ simplecov (>= 0.5)
View
@@ -0,0 +1,52 @@
+# Mongoid Indexing
+
+Programmatic index creation and removal for Mongoid.
+
+## Install
+
+`gem 'mongoid_indexing'`
+
+Then bundle it!
+
+## Usage
+
+Programmatic access to index creation and removal is useful in project where you don't have access to rake tasks such as those for `mongoid:db`. This is fx the case when using Rails engines.
+
+Scenario: Using Mongoid Geospatial to create geo positions for properties. For geo searches to work, you need to create 2d indexes!
+
+```ruby
+# RANDOM PROPERTIES
+describe Property do
+ before :all do
+ Mongoid::Indexing.create_indexes
+ end
+
+ after :all do
+ Mongoid::Indexing.remove_indexes
+ end
+
+ before :each do
+ 10.times do
+ Property.create type: 'apartment', position: random_point_within(10.kms)
+ end
+ end
+end
+```
+
+Enjoy ;)
+
+## Contributing to mongoid_indexing
+
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
+* Fork the project.
+* Start a feature/bugfix branch.
+* Commit and push until you are happy with your contribution.
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+## Copyright
+
+Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
+further details.
+
View
@@ -1,19 +0,0 @@
-= mongoid_indexing
-
-Description goes here.
-
-== Contributing to mongoid_indexing
-
-* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
-* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
-* Fork the project.
-* Start a feature/bugfix branch.
-* Commit and push until you are happy with your contribution.
-* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
-
-== Copyright
-
-Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
-further details.
-
View
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "mongoid_indexing"
gem.homepage = "http://github.com/kristianmandrup/mongoid_indexing"
gem.license = "MIT"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Programmatic index creation and removal for Mongoid}
+ gem.description = %Q{Mongoid index creation and removal the nice and easy way :)}
gem.email = "kmandrup@gmail.com"
gem.authors = ["Kristian Mandrup"]
# dependencies defined in Gemfile
@@ -31,19 +31,5 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
-RSpec::Core::RakeTask.new(:rcov) do |spec|
- spec.pattern = 'spec/**/*_spec.rb'
- spec.rcov = true
-end
-
task :default => :spec
-require 'rdoc/task'
-Rake::RDocTask.new do |rdoc|
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "mongoid_indexing #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
View
@@ -1 +1 @@
-0.0.0
+0.1.0
@@ -0,0 +1,55 @@
+module Mongoid
+ module Indexing
+ class << self
+ attr_writer :logging
+
+ def log_on!
+ @logging = true
+ end
+
+ def log_off!
+ @logging = false
+ end
+
+ def log?
+ @logging
+ end
+
+ def create_indexes models_paths = nil
+ models_paths ||= rails_models_paths
+
+ raise ArgumentError, "No model paths for creating mongoid indexes" if models_paths.blank?
+ message "CREATE INDEXES: #{models_paths}"
+
+ models_paths.each do |path|
+ ::Rails::Mongoid.create_indexes("#{path}/**/*.rb")
+ end
+ end
+
+ def remove_indexes models_paths = nil
+ models_paths ||= rails_models_paths
+
+ raise ArgumentError, "No model paths for creating mongoid indexes" if models_paths.blank?
+ message "REMOVING INDEXES: #{models_paths}"
+
+ models_paths.each do |path|
+ ::Rails::Mongoid.remove_indexes("#{path}/**/*.rb")
+ end
+ end
+
+ def rails_models_paths
+ engines_models_paths = Rails.application.railties.engines.map do |engine|
+ engine.paths["app/models"].expanded
+ end
+ root_models_paths = Rails.application.paths["app/models"]
+ engines_models_paths.push(root_models_paths).flatten
+ end
+
+ protected
+
+ def message txt
+ puts txt if log?
+ end
+ end
+ end
+end

0 comments on commit 6f6ac07

Please sign in to comment.