Removed conditional statements and following the DRY concept.

This commit is contained in:
nbit001 2013-05-05 21:47:41 +01:00
parent 23ce2b201e
commit aad703c297
1 changed files with 37 additions and 25 deletions

View File

@ -3,31 +3,43 @@ require "rails"
module Impressionist module Impressionist
class Engine < Rails::Engine class Engine < Rails::Engine
initializer 'impressionist.model' do |app| attr_accessor :orm
require "#{root}/app/models/impressionist/impressionable.rb"
if Impressionist.orm == :active_record && defined? ActiveRecord def initialize
require "impressionist/models/active_record/impression.rb" define_orm_type(Impressionist.orm)
require "impressionist/models/active_record/impressionist/impressionable.rb"
ActiveRecord::Base.send(:include, Impressionist::Impressionable)
elsif Impressionist.orm == :mongo_mapper
require "impressionist/models/mongo_mapper/impression.rb"
require "impressionist/models/mongo_mapper/impressionist/impressionable.rb"
MongoMapper::Document.plugin Impressionist::Impressionable
elsif Impressionist.orm == :mongoid
require 'impressionist/models/mongoid/impression.rb'
require 'impressionist/models/mongoid/impressionist/impressionable.rb'
Mongoid::Document.send(:include, Impressionist::Impressionable)
end
end end
initializer 'impressionist.controller' do initializer 'impressionist.model' do |app|
if Impressionist.orm == :mongoid require_orm
require 'impressionist/controllers/mongoid/impressionist_controller.rb' ActiveRecord::Base.send(:include, Impressionist::Impressionable)
end end
initializer 'impressionist.controller' do
require "impressionist/controllers/mongoid/impressionist_controller.rb" if orm == :mongoid.to_s
ActiveSupport.on_load(:action_controller) do ActiveSupport.on_load(:action_controller) do
include ImpressionistController::InstanceMethods include ImpressionistController::InstanceMethods
extend ImpressionistController::ClassMethods extend ImpressionistController::ClassMethods
end end
end end
private
def require_orm
require "#{root}/app/models/impressionist/impressionable.rb"
require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
end
def define_orm_type(str)
str = str.to_s
@orm = str.match(/active_record|mongo_mapper|mongoid|/)
:matched
end
end end
end end