From 2a3b6a96d6e09e5748aea3061110a9071852ffbb Mon Sep 17 00:00:00 2001 From: "M.Shibuya" Date: Thu, 17 May 2012 12:23:38 +0900 Subject: [PATCH] Keep up with the recent changes of Mongoid master. Fixes #1136 --- Gemfile | 4 +++- app/views/rails_admin/main/index.html.haml | 2 +- lib/rails_admin/adapters/mongoid.rb | 14 ++++++++++---- .../config/fields/types/bson_object_id.rb | 7 +++++-- spec/controllers/main_controller_spec.rb | 2 +- spec/dummy_app/.gitignore | 2 ++ spec/dummy_app/Gemfile | 2 +- spec/dummy_app/app/mongoid/team.rb | 2 +- spec/dummy_app/config/initializers/devise.rb | 11 ++--------- spec/dummy_app/config/initializers/dragonfly.rb | 5 +---- spec/dummy_app/config/mongoid.yml | 11 +++++++++++ spec/orm/mongoid.rb | 2 ++ 12 files changed, 40 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 49343e347b..d598ac1dd0 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/app/views/rails_admin/main/index.html.haml b/app/views/rails_admin/main/index.html.haml index e5696d007b..8c8c0769a6 100644 --- a/app/views/rails_admin/main/index.html.haml +++ b/app/views/rails_admin/main/index.html.haml @@ -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}" diff --git a/lib/rails_admin/adapters/mongoid.rb b/lib/rails_admin/adapters/mongoid.rb index f0939205bf..13c993cea1 100644 --- a/lib/rails_admin/adapters/mongoid.rb +++ b/lib/rails_admin/adapters/mongoid.rb @@ -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 @@ -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 }, @@ -118,7 +124,7 @@ def properties end def table_name - model.collection.name + model.collection.name.to_s end def serialized_attributes @@ -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 diff --git a/lib/rails_admin/config/fields/types/bson_object_id.rb b/lib/rails_admin/config/fields/types/bson_object_id.rb index 9aae085543..b78abe613d 100644 --- a/lib/rails_admin/config/fields/types/bson_object_id.rb +++ b/lib/rails_admin/config/fields/types/bson_object_id.rb @@ -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 diff --git a/spec/controllers/main_controller_spec.rb b/spec/controllers/main_controller_spec.rb index aa781f748d..62de161c92 100644 --- a/spec/controllers/main_controller_spec.rb +++ b/spec/controllers/main_controller_spec.rb @@ -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 diff --git a/spec/dummy_app/.gitignore b/spec/dummy_app/.gitignore index eb3489a986..dd71abbe4e 100644 --- a/spec/dummy_app/.gitignore +++ b/spec/dummy_app/.gitignore @@ -13,3 +13,5 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp + +/public/system diff --git a/spec/dummy_app/Gemfile b/spec/dummy_app/Gemfile index e6f3e3f36e..1e633265bf 100644 --- a/spec/dummy_app/Gemfile +++ b/spec/dummy_app/Gemfile @@ -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 diff --git a/spec/dummy_app/app/mongoid/team.rb b/spec/dummy_app/app/mongoid/team.rb index 89de416b26..92d4c0755a 100644 --- a/spec/dummy_app/app/mongoid/team.rb +++ b/spec/dummy_app/app/mongoid/team.rb @@ -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 diff --git a/spec/dummy_app/config/initializers/devise.rb b/spec/dummy_app/config/initializers/devise.rb index 2ba548e7d1..d5a8f9931e 100644 --- a/spec/dummy_app/config/initializers/devise.rb +++ b/spec/dummy_app/config/initializers/devise.rb @@ -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 @@ -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. @@ -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. diff --git a/spec/dummy_app/config/initializers/dragonfly.rb b/spec/dummy_app/config/initializers/dragonfly.rb index c19a118252..0eef6a3b0c 100644 --- a/spec/dummy_app/config/initializers/dragonfly.rb +++ b/spec/dummy_app/config/initializers/dragonfly.rb @@ -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) diff --git a/spec/dummy_app/config/mongoid.yml b/spec/dummy_app/config/mongoid.yml index 16b7dde23d..6dc1f28127 100644 --- a/spec/dummy_app/config/mongoid.yml +++ b/spec/dummy_app/config/mongoid.yml @@ -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 diff --git a/spec/orm/mongoid.rb b/spec/orm/mongoid.rb index 09a29cbcdf..4e0768f817 100644 --- a/spec/orm/mongoid.rb +++ b/spec/orm/mongoid.rb @@ -1,5 +1,7 @@ require 'rails_admin/adapters/mongoid' +Paperclip.logger = Logger.new(nil) + class Tableless include Mongoid::Document