From aad703c297448292c5848e22b86aaa30dc20fbac Mon Sep 17 00:00:00 2001 From: nbit001 Date: Sun, 5 May 2013 21:47:41 +0100 Subject: [PATCH 1/3] Removed conditional statements and following the DRY concept. --- lib/impressionist/engine.rb | 62 ++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/impressionist/engine.rb b/lib/impressionist/engine.rb index fddb04f..b191444 100644 --- a/lib/impressionist/engine.rb +++ b/lib/impressionist/engine.rb @@ -3,31 +3,43 @@ require "rails" module Impressionist class Engine < Rails::Engine - initializer 'impressionist.model' do |app| - require "#{root}/app/models/impressionist/impressionable.rb" - if Impressionist.orm == :active_record && defined? ActiveRecord - require "impressionist/models/active_record/impression.rb" - 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 + attr_accessor :orm + + def initialize + define_orm_type(Impressionist.orm) + end + + initializer 'impressionist.model' do |app| + require_orm + ActiveRecord::Base.send(:include, Impressionist::Impressionable) + + end + + + initializer 'impressionist.controller' do + require "impressionist/controllers/mongoid/impressionist_controller.rb" if orm == :mongoid.to_s + + ActiveSupport.on_load(:action_controller) do + include ImpressionistController::InstanceMethods + extend ImpressionistController::ClassMethods + 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 - initializer 'impressionist.controller' do - if Impressionist.orm == :mongoid - require 'impressionist/controllers/mongoid/impressionist_controller.rb' - end - ActiveSupport.on_load(:action_controller) do - include ImpressionistController::InstanceMethods - extend ImpressionistController::ClassMethods - end - end end end From 14f4097a7b8a201b7fe8800026ff59f0accdad16 Mon Sep 17 00:00:00 2001 From: nbit001 Date: Sun, 5 May 2013 21:53:04 +0100 Subject: [PATCH 2/3] Removed conditional statements and following the DRY concept. --- lib/impressionist/engine.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/impressionist/engine.rb b/lib/impressionist/engine.rb index b191444..e16c8ee 100644 --- a/lib/impressionist/engine.rb +++ b/lib/impressionist/engine.rb @@ -3,7 +3,7 @@ require "rails" module Impressionist class Engine < Rails::Engine - attr_accessor :orm + attr_accessor :orm def initialize define_orm_type(Impressionist.orm) From 1f25e16c0e17a6b9d5149a6ca8a23f872289cc76 Mon Sep 17 00:00:00 2001 From: Nalesso Moreira Date: Mon, 6 May 2013 05:27:10 +0200 Subject: [PATCH 3/3] Update engine.rb I've done a bit more of refactoring. --- lib/impressionist/engine.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/impressionist/engine.rb b/lib/impressionist/engine.rb index e16c8ee..f748f97 100644 --- a/lib/impressionist/engine.rb +++ b/lib/impressionist/engine.rb @@ -35,11 +35,14 @@ module Impressionist end def define_orm_type(str) - str = str.to_s - @orm = str.match(/active_record|mongo_mapper|mongoid|/) - :matched - + @orm = matcher(str.to_s) end + def matcher(str) + matched = str.match(/active_record|mongo_mapper|mongoid|/) + matched[0] + end + + end end