Skip to content

Commit

Permalink
added a rails toggle class in order to make mongodb work
Browse files Browse the repository at this point in the history
  • Loading branch information
acnalesso committed Jul 11, 2013
1 parent 38d01e9 commit 50cebdc
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 49 deletions.
33 changes: 8 additions & 25 deletions lib/impressionist/engine.rb
@@ -1,17 +1,10 @@
require "impressionist"
require "rails"

module Impressionist
class Engine < Rails::Engine
class Engine < ::Rails::Engine
attr_accessor :orm

def initialize
define_orm_type(Impressionist.orm)
end

initializer 'impressionist.model' do |app|
require_and_include_orm

@orm = Impressionist.orm
include_orm
end


Expand All @@ -26,22 +19,12 @@ def initialize


private
def require_and_include_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)
@orm = matcher(str.to_s)
end

def matcher(str)
matched = str.match(/active_record|mongo_mapper|mongoid|/)
matched[0]
end

def include_orm
require "#{root}/app/models/impressionist/impressionable.rb"
require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
end

end
end
3 changes: 3 additions & 0 deletions lib/impressionist/load.rb
Expand Up @@ -5,3 +5,6 @@
require "impressionist/counter_cache"

require "impressionist/update_counters"

require "impressionist/rails_toggle"

4 changes: 1 addition & 3 deletions lib/impressionist/models/mongoid/impression.rb
Expand Up @@ -2,9 +2,7 @@ class Impression
include Mongoid::Document
include Mongoid::Timestamps

attr_accessible :impressionable_type, :impressionable_field, :impressionable_id, :user_id,
:controller_name, :action_name, :view_name, :request_hash, :ip_address,
:session_hash, :message, :referrer
include Impressionist::SetUpAssociation

belongs_to :impressionable, polymorphic: true

Expand Down
21 changes: 21 additions & 0 deletions lib/impressionist/rails_toggle.rb
@@ -1,5 +1,26 @@
module Impressionist
# Responsability
# Toggles between rails > 3.1 < 4
# In order to make attr_accessible available in a rails app < 4
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
16 changes: 15 additions & 1 deletion lib/impressionist/set_up_association.rb
Expand Up @@ -2,11 +2,25 @@ module Impressionist
module SetUpAssociation

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,
:user_id,:controller_name,:action_name,:view_name,:request_hash,
:ip_address,:session_hash,:message,:referrer)

base.belongs_to(:impressionable, :polymorphic => true)
end

end
Expand Down
Binary file removed tests/spec/.rails_toggle_spec.rb.swp
Binary file not shown.
26 changes: 26 additions & 0 deletions tests/spec/rails_toggle_spec.rb
Expand Up @@ -7,6 +7,32 @@
module Impressionist
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
1 change: 1 addition & 0 deletions tests/spec/test_helper.rb
@@ -1,3 +1,4 @@
$:.unshift(File.dirname __FILE__)

require "minitest/autorun"
require "minitest/pride"
18 changes: 3 additions & 15 deletions tests/test_app/Gemfile
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'

gem 'rails', '3.2.12'

gem 'impressionist', :path => '../'
gem 'impressionist', :path => '../../'

platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
Expand All @@ -14,6 +14,8 @@ platforms :ruby, :mswin, :mingw do
gem 'pg'
gem 'sqlite3'
gem 'mysql2'
gem "mongoid", "~> 3.1.4"
gem 'bson_ext'
end

gem 'json'
Expand Down Expand Up @@ -44,17 +46,3 @@ end

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'
7 changes: 2 additions & 5 deletions tests/test_app/config/initializers/impression.rb
@@ -1,5 +1,2 @@
# 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
# Define ORM. Could be :active_record (default) and :mongo_mapper
#Impressionist.config.orm = :mongoid

0 comments on commit 50cebdc

Please sign in to comment.