machinist_mongo icon indicating copy to clipboard operation
machinist_mongo copied to clipboard

Working with mixed ActiveRecord and Mongid blueprints

Open gudata opened this issue 14 years ago • 0 comments

To make macjomost_mongo work with both ActiveRecord, Mongoid at the same time i have to put the following code in my blueprints.rb and then the spec runs only with spork, if test launched via spec .../some_spec.rb I got some error.

require 'machinist/active_record'
require 'machinist/mongoid'

require 'sham'
require 'faker'
require 'md5'

module Machinist
  class Lathe
    def assign_attribute(key, value)
      if @object.kind_of? ActiveRecord::Base
        assign_attribute_active_record(key, value)
      elsif @object.class.include? Mongoid::Document
        assign_attribute_mongoid(key, value)
      elsif @object.class.include? MongoMapper::Document or @object.class.include? MongoMapper::EmbeddedDocument
        assign_attribute_mongo_mapper(key, value)
      else
        raise Exception("Unknown mapper")
      end
    end

    # mongoid
    def assign_attribute_mongoid(key, value)
      assigned_attributes[key.to_sym] = value
      @object.process(key => value)
    end

    # mongo_mapper
    def assign_attribute_mongo_mapper(key, value)
      assigned_attributes[key.to_sym] = value
      if @object.respond_to? "#{key}="
        @object.send("#{key}=", value)
      else
        @object[key] = value
      end
    end

    def assign_attribute_active_record(key, value)
      assigned_attributes[key.to_sym] = value
      @object.send("#{key}=", value)
    end
    
  end
end

Blueprints bellow ....

gudata avatar Apr 01 '10 13:04 gudata