From 1c3f809da8ebd3ced8cb9bda8caee89012091a10 Mon Sep 17 00:00:00 2001 From: Tute Costa Date: Mon, 12 Mar 2012 19:06:16 -0300 Subject: [PATCH] Moves ORM specific code to lib/impressionist/:orm/models. Adds mongo_mapper models. Adds config/initializers/impression.rb file on install. --- .gitignore | 1 + app/models/impression.rb | 19 +------------------ app/models/impressionist/impressionable.rb | 5 ----- lib/generators/impressionist_generator.rb | 5 +++++ .../mongo_mapper/impressionist_generator.rb | 8 ++++++++ lib/generators/templates/impression.rb | 5 +++++ lib/impressionist.rb | 8 ++++++++ lib/impressionist/engine.rb | 12 ++++++++++-- .../models/active_record/impression.rb | 18 ++++++++++++++++++ .../impressionist/impressionable.rb | 12 ++++++++++++ .../models/mongo_mapper/impression.rb | 17 +++++++++++++++++ .../impressionist/impressionable.rb | 12 ++++++++++++ test_app/config/initializers/impression.rb | 5 +++++ 13 files changed, 102 insertions(+), 25 deletions(-) create mode 100644 lib/generators/mongo_mapper/impressionist_generator.rb create mode 100644 lib/generators/templates/impression.rb create mode 100644 lib/impressionist/models/active_record/impression.rb create mode 100644 lib/impressionist/models/active_record/impressionist/impressionable.rb create mode 100644 lib/impressionist/models/mongo_mapper/impression.rb create mode 100644 lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb create mode 100644 test_app/config/initializers/impression.rb diff --git a/.gitignore b/.gitignore index 685130c..c90dc04 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *~ +*.DS_Store /coverage /pkg /rdoc diff --git a/app/models/impression.rb b/app/models/impression.rb index 25f8aeb..b4d81ec 100644 --- a/app/models/impression.rb +++ b/app/models/impression.rb @@ -1,20 +1,3 @@ -class Impression < ActiveRecord::Base +class Impression belongs_to :impressionable, :polymorphic=>true - - after_save :update_impressions_counter_cache - - attr_accessible :impressionable_type, :impressionable_id, :user_id, - :controller_name, :action_name, :view_name, :request_hash, :ip_address, - :session_hash, :message, :referrer - - private - - def update_impressions_counter_cache - impressionable_class = self.impressionable_type.constantize - - if impressionable_class.counter_cache_options - resouce = impressionable_class.find(self.impressionable_id) - resouce.try(:update_counter_cache) - end - end end diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index e4aeae3..e09addd 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -6,11 +6,6 @@ module Impressionist attr_accessor :cache_options @cache_options = nil - def is_impressionable(options={}) - has_many :impressions, :as => :impressionable, :dependent => :destroy - @cache_options = options[:counter_cache] - end - def counter_cache_options if @cache_options options = { :column_name => :impressions_count, :unique => false } diff --git a/lib/generators/impressionist_generator.rb b/lib/generators/impressionist_generator.rb index 69763b7..30290af 100644 --- a/lib/generators/impressionist_generator.rb +++ b/lib/generators/impressionist_generator.rb @@ -2,6 +2,11 @@ module Impressionist module Generators class ImpressionistGenerator < Rails::Generators::Base hook_for :orm + source_root File.expand_path('../templates', __FILE__) + + def copy_config_file + template 'impression.rb', 'config/initializers/impression.rb' + end end end end diff --git a/lib/generators/mongo_mapper/impressionist_generator.rb b/lib/generators/mongo_mapper/impressionist_generator.rb new file mode 100644 index 0000000..eaa1b65 --- /dev/null +++ b/lib/generators/mongo_mapper/impressionist_generator.rb @@ -0,0 +1,8 @@ +module MongoMapper + module Generators + class ImpressionistGenerator < Rails::Generators::Base + # Empty for now, need it for generating the config file without + # triggering other ORM's generators. + end + end +end diff --git a/lib/generators/templates/impression.rb b/lib/generators/templates/impression.rb new file mode 100644 index 0000000..fbdee91 --- /dev/null +++ b/lib/generators/templates/impression.rb @@ -0,0 +1,5 @@ +# Use this hook to configure impressionist parameters +Impressionist.setup do |config| + # Define ORM. Could be :active_record (default) and :mongo_mapper + # config.orm = :active_record +end diff --git a/lib/impressionist.rb b/lib/impressionist.rb index bb4dc4b..7ead8fe 100644 --- a/lib/impressionist.rb +++ b/lib/impressionist.rb @@ -1,4 +1,12 @@ require "impressionist/engine.rb" module Impressionist + # Define ORM + mattr_accessor :orm + @@orm = :active_record + + # Load configuration from initializer + def self.setup + yield self + end end diff --git a/lib/impressionist/engine.rb b/lib/impressionist/engine.rb index a4ecfb0..197a16c 100644 --- a/lib/impressionist/engine.rb +++ b/lib/impressionist/engine.rb @@ -3,8 +3,16 @@ require "rails" module Impressionist class Engine < Rails::Engine - initializer 'impressionist.extend_ar' do |app| - ActiveRecord::Base.send(:include, Impressionist::Impressionable) + initializer 'impressionist.model' do |app| + if Impressionist.orm == :active_record + 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 + end end initializer 'impressionist.controller' do diff --git a/lib/impressionist/models/active_record/impression.rb b/lib/impressionist/models/active_record/impression.rb new file mode 100644 index 0000000..4d13373 --- /dev/null +++ b/lib/impressionist/models/active_record/impression.rb @@ -0,0 +1,18 @@ +class Impression < ActiveRecord::Base + attr_accessible :impressionable_type, :impressionable_id, :user_id, + :controller_name, :action_name, :view_name, :request_hash, :ip_address, + :session_hash, :message, :referrer + + after_save :update_impressions_counter_cache + + private + + def update_impressions_counter_cache + impressionable_class = self.impressionable_type.constantize + + if impressionable_class.counter_cache_options + resouce = impressionable_class.find(self.impressionable_id) + resouce.try(:update_counter_cache) + end + end +end diff --git a/lib/impressionist/models/active_record/impressionist/impressionable.rb b/lib/impressionist/models/active_record/impressionist/impressionable.rb new file mode 100644 index 0000000..d9339fb --- /dev/null +++ b/lib/impressionist/models/active_record/impressionist/impressionable.rb @@ -0,0 +1,12 @@ +module Impressionist + module Impressionable + extend ActiveSupport::Concern + + module ClassMethods + def is_impressionable(options={}) + has_many :impressions, :as => :impressionable, :dependent => :destroy + @cache_options = options[:counter_cache] + end + end + end +end diff --git a/lib/impressionist/models/mongo_mapper/impression.rb b/lib/impressionist/models/mongo_mapper/impression.rb new file mode 100644 index 0000000..ce1ec0f --- /dev/null +++ b/lib/impressionist/models/mongo_mapper/impression.rb @@ -0,0 +1,17 @@ +class Impression + include MongoMapper::Document + + key :impressionable_type, String + key :impressionable_id, String + key :user_id, String + key :controller_name, String + key :action_name, String + key :view_name, String + key :request_hash, String + key :ip_address, String + key :session_hash, String + key :message, String + key :referrer, String + + timestamps! +end diff --git a/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb b/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb new file mode 100644 index 0000000..9e0dd52 --- /dev/null +++ b/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb @@ -0,0 +1,12 @@ +module Impressionist + module Impressionable + extend ActiveSupport::Concern + + module ClassMethods + def is_impressionable(options={}) + many :impressions, :as => :impressionable, :dependent => :destroy + @cache_options = options[:counter_cache] + end + end + end +end diff --git a/test_app/config/initializers/impression.rb b/test_app/config/initializers/impression.rb new file mode 100644 index 0000000..fbdee91 --- /dev/null +++ b/test_app/config/initializers/impression.rb @@ -0,0 +1,5 @@ +# Use this hook to configure impressionist parameters +Impressionist.setup do |config| + # Define ORM. Could be :active_record (default) and :mongo_mapper + # config.orm = :active_record +end