From 2f8c47a46f4276fa155b85e911d7a1a543de28a3 Mon Sep 17 00:00:00 2001 From: JGW Maxwell Date: Wed, 11 Jul 2012 03:45:24 +0100 Subject: [PATCH] fixed for Mongoid 2 support --- lib/generators/impressionist_generator.rb | 1 + lib/generators/templates/impression-mongoid-2.rb | 1 + .../controllers/mongoid/impressionist_controller.rb | 3 ++- .../mongoid/mongoid2-impressionist_controller.rb | 10 ++++++++++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 lib/generators/templates/impression-mongoid-2.rb create mode 100644 lib/impressionist/controllers/mongoid/mongoid2-impressionist_controller.rb diff --git a/lib/generators/impressionist_generator.rb b/lib/generators/impressionist_generator.rb index 30290af..008f1ec 100644 --- a/lib/generators/impressionist_generator.rb +++ b/lib/generators/impressionist_generator.rb @@ -7,6 +7,7 @@ module Impressionist def copy_config_file template 'impression.rb', 'config/initializers/impression.rb' end + end end end diff --git a/lib/generators/templates/impression-mongoid-2.rb b/lib/generators/templates/impression-mongoid-2.rb new file mode 100644 index 0000000..0d1a95e --- /dev/null +++ b/lib/generators/templates/impression-mongoid-2.rb @@ -0,0 +1 @@ +impression-mongoid-2.rb \ No newline at end of file diff --git a/lib/impressionist/controllers/mongoid/impressionist_controller.rb b/lib/impressionist/controllers/mongoid/impressionist_controller.rb index 73b5e92..426c5b8 100644 --- a/lib/impressionist/controllers/mongoid/impressionist_controller.rb +++ b/lib/impressionist/controllers/mongoid/impressionist_controller.rb @@ -1,9 +1,10 @@ ImpressionistController::InstanceMethods.send(:define_method, :direct_create_statement) do |query_params={}| # creates a statment hash that contains default values for creating an impression. # if :impressionable_id is a valid ObjectId then convert it into one + base = (defined? Moped) ? Moped::BSON : BSON query_params.reverse_merge!( :impressionable_type => controller_name.singularize.camelize, - :impressionable_id=> !Moped::BSON::ObjectId.legal?(params[:id]) ? params[:id] : Moped::BSON::ObjectId.from_string(params[:id]) + :impressionable_id=> !base::ObjectId.legal?(params[:id]) ? params[:id] : base::ObjectId.from_string(params[:id]) ) associative_create_statement(query_params) end \ No newline at end of file diff --git a/lib/impressionist/controllers/mongoid/mongoid2-impressionist_controller.rb b/lib/impressionist/controllers/mongoid/mongoid2-impressionist_controller.rb new file mode 100644 index 0000000..de005a7 --- /dev/null +++ b/lib/impressionist/controllers/mongoid/mongoid2-impressionist_controller.rb @@ -0,0 +1,10 @@ +ImpressionistController::InstanceMethods.send(:define_method, :direct_create_statement) do |query_params={}| + # creates a statment hash that contains default values for creating an impression. + # if :impressionable_id is a valid ObjectId then convert it into one + base = (defined? Moped) ? Moped::BSON :: BSON + query_params.reverse_merge!( + :impressionable_type => controller_name.singularize.camelize, + :impressionable_id=> !base::ObjectId.legal?(params[:id]) ? params[:id] : base::ObjectId.from_string(params[:id]) + ) + associative_create_statement(query_params) +end \ No newline at end of file