Merge pull request #31 from tute/master

Adds mongo_mapper ORM support
This commit is contained in:
John McAliley 2012-03-12 20:19:41 -07:00
commit 15f255ff98
13 changed files with 102 additions and 25 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
*~ *~
*.DS_Store
/coverage /coverage
/pkg /pkg
/rdoc /rdoc

View File

@ -1,20 +1,3 @@
class Impression < ActiveRecord::Base class Impression
belongs_to :impressionable, :polymorphic=>true 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 end

View File

@ -6,11 +6,6 @@ module Impressionist
attr_accessor :cache_options attr_accessor :cache_options
@cache_options = nil @cache_options = nil
def is_impressionable(options={})
has_many :impressions, :as => :impressionable, :dependent => :destroy
@cache_options = options[:counter_cache]
end
def counter_cache_options def counter_cache_options
if @cache_options if @cache_options
options = { :column_name => :impressions_count, :unique => false } options = { :column_name => :impressions_count, :unique => false }

View File

@ -2,6 +2,11 @@ module Impressionist
module Generators module Generators
class ImpressionistGenerator < Rails::Generators::Base class ImpressionistGenerator < Rails::Generators::Base
hook_for :orm 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 end
end end

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,12 @@
require "impressionist/engine.rb" require "impressionist/engine.rb"
module Impressionist module Impressionist
# Define ORM
mattr_accessor :orm
@@orm = :active_record
# Load configuration from initializer
def self.setup
yield self
end
end end

View File

@ -3,8 +3,16 @@ require "rails"
module Impressionist module Impressionist
class Engine < Rails::Engine class Engine < Rails::Engine
initializer 'impressionist.extend_ar' do |app| 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) 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 end
initializer 'impressionist.controller' do initializer 'impressionist.controller' do

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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