From 50cebdcb239486b788e7f8ed0abc18e90849d272 Mon Sep 17 00:00:00 2001 From: Antonio C Nalesso Moreira Date: Thu, 11 Jul 2013 05:43:04 +0100 Subject: [PATCH] added a rails toggle class in order to make mongodb work --- lib/impressionist/engine.rb | 33 +++++------------- lib/impressionist/load.rb | 3 ++ .../models/mongoid/impression.rb | 4 +-- lib/impressionist/rails_toggle.rb | 21 +++++++++++ lib/impressionist/set_up_association.rb | 16 ++++++++- tests/spec/.rails_toggle_spec.rb.swp | Bin 12288 -> 0 bytes tests/spec/rails_toggle_spec.rb | 26 ++++++++++++++ tests/spec/test_helper.rb | 1 + tests/test_app/Gemfile | 18 ++-------- .../config/initializers/impression.rb | 7 ++-- 10 files changed, 80 insertions(+), 49 deletions(-) delete mode 100644 tests/spec/.rails_toggle_spec.rb.swp diff --git a/lib/impressionist/engine.rb b/lib/impressionist/engine.rb index 7cac03e..68a31ce 100644 --- a/lib/impressionist/engine.rb +++ b/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 @@ -26,22 +19,12 @@ module Impressionist 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 diff --git a/lib/impressionist/load.rb b/lib/impressionist/load.rb index dfeac3a..5712d40 100644 --- a/lib/impressionist/load.rb +++ b/lib/impressionist/load.rb @@ -5,3 +5,6 @@ require "impressionist/set_up_association" require "impressionist/counter_cache" require "impressionist/update_counters" + +require "impressionist/rails_toggle" + diff --git a/lib/impressionist/models/mongoid/impression.rb b/lib/impressionist/models/mongoid/impression.rb index 5c10785..55330c4 100644 --- a/lib/impressionist/models/mongoid/impression.rb +++ b/lib/impressionist/models/mongoid/impression.rb @@ -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 diff --git a/lib/impressionist/rails_toggle.rb b/lib/impressionist/rails_toggle.rb index 25b1d07..4b88c7c 100644 --- a/lib/impressionist/rails_toggle.rb +++ b/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 diff --git a/lib/impressionist/set_up_association.rb b/lib/impressionist/set_up_association.rb index 9aee85d..5a5ba0f 100644 --- a/lib/impressionist/set_up_association.rb +++ b/lib/impressionist/set_up_association.rb @@ -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 diff --git a/tests/spec/.rails_toggle_spec.rb.swp b/tests/spec/.rails_toggle_spec.rb.swp deleted file mode 100644 index 1625b198196155673223de8077b9f9ef8e597dff..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&Pfo%>6bA6Ywf~|Qct|2EXaN({7}v6JAwjpKEe{w|+B!36bm0}ehI_B!8Qkl} zBj_s<#RamKe3N`B&^K-R+W}wbU$joCS#2m;n@T+uU(Q}niiKuHsU+0S_iOo?Ki71w zax-MUyPZFJKOOW2JhGl1WrnTQX`-$399w5Sn{nisK*zT0(!n6+ZvIKtg!3wI$yj86A*v^1Rwwb2tWV=5P-l^3An7FKIDm3B&(9bG;+X# z00bZa0SG_<0uX=z1Rwwb2tZ&-1cIJYZ|h1`H{^lj|Ns0qfOmQQEP0eXNG>H>(vciW z4kQgpU6SwHm0&>t0uX=z1Rwwb2tWV=5P$##{=EQmB43ksTdu$`4L#B4#Sno^< z>(ootDD917rq*o1T`9v?V{Mqqzl-m)ZTIreP~L~{4mr-)Nd3JmH!qP+qIfJfrNBAU y4WcNo&xdj+-KQhko6K}%A4w744cXA-+H}n(Z`sqajFhO&HcJy5ggVylR(%3A(R>sD diff --git a/tests/spec/rails_toggle_spec.rb b/tests/spec/rails_toggle_spec.rb index 330e63d..5bead74 100644 --- a/tests/spec/rails_toggle_spec.rb +++ b/tests/spec/rails_toggle_spec.rb @@ -7,6 +7,32 @@ require 'impressionist/rails_toggle' 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 diff --git a/tests/spec/test_helper.rb b/tests/spec/test_helper.rb index 76af35e..eeb22af 100644 --- a/tests/spec/test_helper.rb +++ b/tests/spec/test_helper.rb @@ -1,3 +1,4 @@ $:.unshift(File.dirname __FILE__) require "minitest/autorun" +require "minitest/pride" diff --git a/tests/test_app/Gemfile b/tests/test_app/Gemfile index 19de4ca..dab2f36 100644 --- a/tests/test_app/Gemfile +++ b/tests/test_app/Gemfile @@ -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' @@ -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' @@ -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' diff --git a/tests/test_app/config/initializers/impression.rb b/tests/test_app/config/initializers/impression.rb index fbdee91..a2fcb52 100644 --- a/tests/test_app/config/initializers/impression.rb +++ b/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