Skip to content

Commit

Permalink
Keep up with the recent changes of Mongoid master. Fixes #1136
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed May 17, 2012
1 parent a78f183 commit 2a3b6a9
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 24 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Expand Up @@ -36,13 +36,15 @@ group :mongoid do
case ENV['CI_ORM_VERSION']
when 'head'
gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git'
gem 'mongoid-paperclip', :require => 'mongoid_paperclip', :git => 'git://github.com/mshibuya/mongoid-paperclip.git', :branch => 'fix-stop-patching-logger'
# For now, carrierwave-mongoid's mongoid dependency is restricted to '~> 2.1'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid', :git => 'git://github.com/tanordheim/carrierwave-mongoid.git', :branch => 'mongoid_3_0'
gem 'database_cleaner', :git => 'git://github.com/potatosalad/database_cleaner.git'
else
gem 'mongoid'
gem 'mongoid-paperclip', :require => 'mongoid_paperclip'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
end
gem 'mongoid-paperclip', :require => 'mongoid_paperclip'
end

group :debug do
Expand Down
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/index.html.haml
Expand Up @@ -132,7 +132,7 @@
%td.last.links
%ul.inline= menu_for :member, @abstract_model, object, true
- if @objects.respond_to?(:total_count)
- total_count = @objects.total_count
- total_count = @objects.total_count.to_i
= paginate(@objects, :theme => 'twitter-bootstrap', :remote => true)
= link_to(t("admin.misc.show_all"), index_path(params.merge(:all => true)), :class => "show-all btn clearfix pjax") unless total_count > 100 || total_count <= @objects.to_a.size
.clearfix.total-count= "#{total_count} #{@model_config.label_plural.downcase}"
Expand Down
14 changes: 10 additions & 4 deletions lib/rails_admin/adapters/mongoid.rb
Expand Up @@ -15,8 +15,13 @@ def new(params = {})
def get(id)
begin
AbstractObject.new(model.find(id))
rescue BSON::InvalidObjectId, ::Mongoid::Errors::DocumentNotFound
nil
rescue => e
if ['BSON::InvalidObjectId', 'Mongoid::Errors::DocumentNotFound',
'Mongoid::Errors::InvalidFind', 'Moped::Errors::InvalidObjectId'].include? e.class.to_s
nil
else
raise e
end
end
end

Expand Down Expand Up @@ -82,6 +87,7 @@ def properties
"BigDecimal" => { :type => :decimal },
"Boolean" => { :type => :boolean },
"BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
"Moped::BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
"Date" => { :type => :date },
"DateTime" => { :type => :datetime },
"Float" => { :type => :float },
Expand Down Expand Up @@ -118,7 +124,7 @@ def properties
end

def table_name
model.collection.name
model.collection.name.to_s
end

def serialized_attributes
Expand Down Expand Up @@ -261,7 +267,7 @@ def build_statement(column, type, value, operator)
return if value.blank?
{ column => { "$in" => Array.wrap(value) } }
when :belongs_to_association, :bson_object_id
object_id = (BSON::ObjectId(value) rescue nil)
object_id = (BSON::ObjectId.from_string(value) rescue nil)
{ column => object_id } if object_id
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/rails_admin/config/fields/types/bson_object_id.rb
Expand Up @@ -28,8 +28,11 @@ class BsonObjectId < RailsAdmin::Config::Fields::Types::String

def parse_input(params)
begin
params[name] = (params[name].blank? ? nil : BSON::ObjectId(params[name])) if params[name].is_a?(::String)
rescue BSON::InvalidObjectId
params[name] = (params[name].blank? ? nil : BSON::ObjectId.from_string(params[name])) if params[name].is_a?(::String)
rescue => e
unless ['BSON::InvalidObjectId', 'Moped::Errors::InvalidObjectId'].include? e.class.to_s
raise e
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/main_controller_spec.rb
Expand Up @@ -67,7 +67,7 @@
end

it "scopes associated collection records according to bindings" do
@team.revenue = 3
@team.revenue = BigDecimal.new('3')
@team.save

@players = 5.times.map do
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy_app/.gitignore
Expand Up @@ -13,3 +13,5 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

/public/system
2 changes: 1 addition & 1 deletion spec/dummy_app/Gemfile
Expand Up @@ -44,7 +44,7 @@ group :mongoid do
gem 'mongoid'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
end
gem 'mongoid-paperclip', :require => 'mongoid_paperclip'
gem 'mongoid-paperclip', :require => 'mongoid_paperclip', :git => 'git://github.com/mshibuya/mongoid-paperclip.git', :branch => 'fix-stop-patching-logger'
gem 'paperclip', '~> 2.4'
gem 'dragonfly'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/app/mongoid/team.rb
Expand Up @@ -20,7 +20,7 @@ class Team

attr_accessible :name, :division_id, :logo_url, :manager, :ballpark, :mascot, :founded, :wins, :losses, :win_percentage, :revenue, :color, :custom_field, :fan_ids, :player_ids, :comment_ids

has_many :players, :inverse_of => :team, :order => :_id
has_many :players, :inverse_of => :team, :order => :_id.asc
has_and_belongs_to_many :fans
has_many :comments, :as => :commentable

Expand Down
11 changes: 2 additions & 9 deletions spec/dummy_app/config/initializers/devise.rb
Expand Up @@ -9,9 +9,6 @@
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"

# Automatically apply schema changes in tableless databases
config.apply_schema = false

# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
Expand Down Expand Up @@ -95,7 +92,7 @@
# the user cannot access the website without confirming his account.
# config.allow_unconfirmed_access_for = 2.days

# If true, requires any email changes to be confirmed (exctly the same way as
# If true, requires any email changes to be confirmed (exactly the same way as
# initial account confirmation) to be applied. Requires additional unconfirmed_email
# db field (see migrations). Until confirmed new email is stored in
# unconfirmed email column, and copied to email column on successful confirmation.
Expand All @@ -111,13 +108,9 @@
# If true, extends the user's remember period when remembered via cookie.
# config.extend_remember_period = false

# If true, uses the password salt as remember token. This should be turned
# to false if you are not using database authenticatable.
config.use_salt_as_remember_token = true

# Options to be passed to the created cookie. For instance, you can set
# :secure => true in order to force SSL only cookies.
# config.cookie_options = {}
# config.rememberable_options = {}

# ==> Configuration for :validatable
# Range for password length. Default is 6..128.
Expand Down
5 changes: 1 addition & 4 deletions spec/dummy_app/config/initializers/dragonfly.rb
Expand Up @@ -3,11 +3,8 @@

app = Dragonfly[:images]

# Configure to use ImageMagick, Rails defaults, and the Mongo data store
# Configure to use ImageMagick, Rails defaults
app.configure_with(:imagemagick)
app.configure_with(:rails) do |c|
c.datastore = Dragonfly::DataStorage::MongoDataStore.new :db => Mongoid.database
end

# Allow all mongoid models to use the macro 'image_accessor'
app.define_macro_on_include(Mongoid::Document, :image_accessor)
Expand Down
11 changes: 11 additions & 0 deletions spec/dummy_app/config/mongoid.yml
Expand Up @@ -5,7 +5,18 @@ defaults: &defaults
development:
<<: *defaults
database: dummy_app_development
sessions:
default:
database: dummy_app_development
hosts:
- localhost:27017


test:
<<: *defaults
database: dummy_app_test
sessions:
default:
database: dummy_app_test
hosts:
- localhost:27017
2 changes: 2 additions & 0 deletions spec/orm/mongoid.rb
@@ -1,5 +1,7 @@
require 'rails_admin/adapters/mongoid'

Paperclip.logger = Logger.new(nil)

class Tableless
include Mongoid::Document

Expand Down

0 comments on commit 2a3b6a9

Please sign in to comment.