added a rails toggle class in order to make mongodb work
This commit is contained in:
parent
38d01e9c69
commit
50cebdcb23
|
@ -1,17 +1,10 @@
|
||||||
require "impressionist"
|
|
||||||
require "rails"
|
|
||||||
|
|
||||||
module Impressionist
|
module Impressionist
|
||||||
class Engine < Rails::Engine
|
class Engine < ::Rails::Engine
|
||||||
attr_accessor :orm
|
attr_accessor :orm
|
||||||
|
|
||||||
def initialize
|
|
||||||
define_orm_type(Impressionist.orm)
|
|
||||||
end
|
|
||||||
|
|
||||||
initializer 'impressionist.model' do |app|
|
initializer 'impressionist.model' do |app|
|
||||||
require_and_include_orm
|
@orm = Impressionist.orm
|
||||||
|
include_orm
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,22 +19,12 @@ module Impressionist
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def require_and_include_orm
|
|
||||||
|
def include_orm
|
||||||
require "#{root}/app/models/impressionist/impressionable.rb"
|
require "#{root}/app/models/impressionist/impressionable.rb"
|
||||||
require "impressionist/models/#{orm}/impression.rb"
|
require "impressionist/models/#{orm}/impression.rb"
|
||||||
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
|
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_orm_type(str)
|
|
||||||
@orm = matcher(str.to_s)
|
|
||||||
end
|
|
||||||
|
|
||||||
def matcher(str)
|
|
||||||
matched = str.match(/active_record|mongo_mapper|mongoid|/)
|
|
||||||
matched[0]
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,3 +5,6 @@ require "impressionist/set_up_association"
|
||||||
require "impressionist/counter_cache"
|
require "impressionist/counter_cache"
|
||||||
|
|
||||||
require "impressionist/update_counters"
|
require "impressionist/update_counters"
|
||||||
|
|
||||||
|
require "impressionist/rails_toggle"
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,7 @@ class Impression
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
include Mongoid::Timestamps
|
include Mongoid::Timestamps
|
||||||
|
|
||||||
attr_accessible :impressionable_type, :impressionable_field, :impressionable_id, :user_id,
|
include Impressionist::SetUpAssociation
|
||||||
:controller_name, :action_name, :view_name, :request_hash, :ip_address,
|
|
||||||
:session_hash, :message, :referrer
|
|
||||||
|
|
||||||
belongs_to :impressionable, polymorphic: true
|
belongs_to :impressionable, polymorphic: true
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,26 @@
|
||||||
module Impressionist
|
module Impressionist
|
||||||
|
# Responsability
|
||||||
|
# Toggles between rails > 3.1 < 4
|
||||||
|
# In order to make attr_accessible available in a rails app < 4
|
||||||
class RailsToggle
|
class RailsToggle
|
||||||
|
|
||||||
|
attr_reader :r_version
|
||||||
|
|
||||||
|
def initialize(version)
|
||||||
|
@r_version = version.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# if Rails 4, it does not include attr_accessible
|
||||||
|
# and it returns false
|
||||||
|
def valid?
|
||||||
|
greater_than_4? ? false : true
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def greater_than_4?
|
||||||
|
r_version == "4" ? true : false
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,11 +2,25 @@ module Impressionist
|
||||||
module SetUpAssociation
|
module SetUpAssociation
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
|
# include attr_accessible base on
|
||||||
|
# rails version
|
||||||
|
should_include?
|
||||||
|
base.
|
||||||
|
belongs_to(:impressionable,:polymorphic => true)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.should_include?
|
||||||
|
toggle = Impressionist::RailsToggle.new(Rails::VERSION::MAJOR)
|
||||||
|
self.include_attr_accessible unless toggle.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.include_attr_accessible
|
||||||
base.attr_accessible(:impressionable_type,:impressionable_id,
|
base.attr_accessible(:impressionable_type,:impressionable_id,
|
||||||
:user_id,:controller_name,:action_name,:view_name,:request_hash,
|
:user_id,:controller_name,:action_name,:view_name,:request_hash,
|
||||||
:ip_address,:session_hash,:message,:referrer)
|
:ip_address,:session_hash,:message,:referrer)
|
||||||
|
|
||||||
base.belongs_to(:impressionable, :polymorphic => true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Binary file not shown.
|
@ -7,6 +7,32 @@ require 'impressionist/rails_toggle'
|
||||||
module Impressionist
|
module Impressionist
|
||||||
describe RailsToggle do
|
describe RailsToggle do
|
||||||
|
|
||||||
|
describe "Rails 4" do
|
||||||
|
before do
|
||||||
|
@toggle = RailsToggle.new("4")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "return current rails version" do
|
||||||
|
@toggle.r_version.must_equal "4"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "must not load attr_accessible" do
|
||||||
|
@toggle.valid?.must_equal false
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "Rails 3.1.x" do
|
||||||
|
|
||||||
|
before do
|
||||||
|
@toggle = RailsToggle.new("3")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "includes" do
|
||||||
|
@toggle.valid?.must_equal true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
$:.unshift(File.dirname __FILE__)
|
$:.unshift(File.dirname __FILE__)
|
||||||
|
|
||||||
require "minitest/autorun"
|
require "minitest/autorun"
|
||||||
|
require "minitest/pride"
|
||||||
|
|
|
@ -2,7 +2,7 @@ source 'https://rubygems.org'
|
||||||
|
|
||||||
gem 'rails', '3.2.12'
|
gem 'rails', '3.2.12'
|
||||||
|
|
||||||
gem 'impressionist', :path => '../'
|
gem 'impressionist', :path => '../../'
|
||||||
|
|
||||||
platforms :jruby do
|
platforms :jruby do
|
||||||
gem 'activerecord-jdbcsqlite3-adapter'
|
gem 'activerecord-jdbcsqlite3-adapter'
|
||||||
|
@ -14,6 +14,8 @@ platforms :ruby, :mswin, :mingw do
|
||||||
gem 'pg'
|
gem 'pg'
|
||||||
gem 'sqlite3'
|
gem 'sqlite3'
|
||||||
gem 'mysql2'
|
gem 'mysql2'
|
||||||
|
gem "mongoid", "~> 3.1.4"
|
||||||
|
gem 'bson_ext'
|
||||||
end
|
end
|
||||||
|
|
||||||
gem 'json'
|
gem 'json'
|
||||||
|
@ -44,17 +46,3 @@ end
|
||||||
|
|
||||||
gem 'jquery-rails'
|
gem 'jquery-rails'
|
||||||
|
|
||||||
# To use ActiveModel has_secure_password
|
|
||||||
# gem 'bcrypt-ruby', '~> 3.0.0'
|
|
||||||
|
|
||||||
# To use Jbuilder templates for JSON
|
|
||||||
# gem 'jbuilder'
|
|
||||||
|
|
||||||
# Use unicorn as the app server
|
|
||||||
# gem 'unicorn'
|
|
||||||
|
|
||||||
# Deploy with Capistrano
|
|
||||||
# gem 'capistrano'
|
|
||||||
|
|
||||||
# To use debugger
|
|
||||||
# gem 'ruby-debug'
|
|
||||||
|
|
|
@ -1,5 +1,2 @@
|
||||||
# Use this hook to configure impressionist parameters
|
# Define ORM. Could be :active_record (default) and :mongo_mapper
|
||||||
Impressionist.setup do |config|
|
#Impressionist.config.orm = :mongoid
|
||||||
# Define ORM. Could be :active_record (default) and :mongo_mapper
|
|
||||||
# config.orm = :active_record
|
|
||||||
end
|
|
||||||
|
|
Loading…
Reference in New Issue