From b05aa79ad39237e30dd89c1b6cb80e0d79d385c5 Mon Sep 17 00:00:00 2001 From: Antonio C Nalesso Moreira Date: Fri, 12 Jul 2013 02:48:23 +0100 Subject: [PATCH] added minitest tests, added 2 entities in order to handle compability between rails versions --- Rakefile | 5 +- impressionist.gemspec | 2 +- lib/impressionist/load.rb | 11 ++-- .../models/active_record/impression.rb | 7 ++- lib/impressionist/rails_toggle.rb | 22 +++----- lib/impressionist/set_up_association.rb | 28 ---------- lib/impressionist/setup_association.rb | 48 ++++++++++++++++ .../{test_helper.rb => minitest_helper.rb} | 0 tests/spec/rails_toggle_spec.rb | 34 ++++-------- tests/spec/setup_association_spec.rb | 55 +++++++++++++++++++ 10 files changed, 137 insertions(+), 75 deletions(-) delete mode 100644 lib/impressionist/set_up_association.rb create mode 100644 lib/impressionist/setup_association.rb rename tests/spec/{test_helper.rb => minitest_helper.rb} (100%) create mode 100644 tests/spec/setup_association_spec.rb diff --git a/Rakefile b/Rakefile index bc38e61..c638c0b 100644 --- a/Rakefile +++ b/Rakefile @@ -27,7 +27,8 @@ end # Rake libs default is lib # libs << path to load test_helper, etc.. Rake::TestTask.new do |t| - t.libs << 'tests/spec' - t.pattern = FileList['tests/spec/*_spec.rb'] + t.libs << "tests/spec" + t.pattern = "tests/spec/**/*_spec.rb" + t.test_files = FileList["tests/spec/**/*_spec.rb"] t.verbose = true end diff --git a/impressionist.gemspec b/impressionist.gemspec index 52fc3bc..8bec8b6 100644 --- a/impressionist.gemspec +++ b/impressionist.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |s| # Capybara has dropped support for Ruby 1.8.7 onwards version 2.0.3 s.add_development_dependency 'capybara', '>= 2.0.3' s.add_development_dependency 'rake', '>= 0.9' - s.add_development_dependency 'rails', '3.1.12' + s.add_development_dependency 'rails', '3.2.12' s.add_development_dependency 'rdoc', '>= 2.4.2' s.add_development_dependency 'rspec-rails' s.add_development_dependency 'simplecov' diff --git a/lib/impressionist/load.rb b/lib/impressionist/load.rb index 5712d40..d046ba6 100644 --- a/lib/impressionist/load.rb +++ b/lib/impressionist/load.rb @@ -1,10 +1,9 @@ -require "impressionist/engine" +require 'impressionist/engine' -require "impressionist/set_up_association" +require 'impressionist/setup_association' -require "impressionist/counter_cache" +require 'impressionist/counter_cache' -require "impressionist/update_counters" - -require "impressionist/rails_toggle" +require 'impressionist/update_counters' +require 'impressionist/rails_toggle' diff --git a/lib/impressionist/models/active_record/impression.rb b/lib/impressionist/models/active_record/impression.rb index c9a7c9e..252a9cc 100644 --- a/lib/impressionist/models/active_record/impression.rb +++ b/lib/impressionist/models/active_record/impression.rb @@ -1,12 +1,15 @@ # Responsability +## See CounterCache TODO: extract it into a class # * be able to update_counters # * log an error if imps_id and imps_type can not be found -# asks updatable? whether it may or may not be updated +# * asks updatable? whether it may or may not be updated # FIX exeception raising when no imps_id is found class Impression < ActiveRecord::Base + include Impressionist::CounterCache - include Impressionist::SetUpAssociation + # sets belongs_to and attr_accessible + Impressionist::SetupAssociation.new(self).set after_save :impressionable_counter_cache_updatable? diff --git a/lib/impressionist/rails_toggle.rb b/lib/impressionist/rails_toggle.rb index 4b88c7c..5dbe06c 100644 --- a/lib/impressionist/rails_toggle.rb +++ b/lib/impressionist/rails_toggle.rb @@ -2,25 +2,21 @@ 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 + # decides where or not to include attr_accessible + def should_include? + ask_rails || false end private - def greater_than_4? - r_version == "4" ? true : false + # returns false if rails >= 4 + # true if rails < 4 + def ask_rails + ::Rails::VERSION::MAJOR.to_i >= 4 ? false : true end end + end diff --git a/lib/impressionist/set_up_association.rb b/lib/impressionist/set_up_association.rb deleted file mode 100644 index 12dd2f0..0000000 --- a/lib/impressionist/set_up_association.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Impressionist - module SetUpAssociation - - def self.included(base) - # include attr_accessible base on - # rails version - @klass = base - 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 - @klass.attr_accessible(:impressionable_type,:impressionable_id, - :user_id,:controller_name,:action_name,:view_name,:request_hash, - :ip_address,:session_hash,:message,:referrer) - - end - - end - -end diff --git a/lib/impressionist/setup_association.rb b/lib/impressionist/setup_association.rb new file mode 100644 index 0000000..2cb6350 --- /dev/null +++ b/lib/impressionist/setup_association.rb @@ -0,0 +1,48 @@ +module Impressionist + # Impressionist::SetupAssociation.new(entity).set + class SetupAssociation + def initialize(receiver) + @receiver = receiver + end + + # True or False + # Note toggle returns false if rails >= 4 + def include_attr_acc? + toggle && make_accessible + end + + def define_belongs_to + receiver.belongs_to(:impressionable, :polymorphic => true) + end + + # returns done if thruthy + def set + :done if (include_attr_acc? && define_belongs_to) + end + + private + attr_reader :receiver, :toggle + + def make_accessible + receiver. + attr_accessible(:impressionable_type, + :impressionable_id, + :controller_name, + :request_hash, + :session_hash, + :action_name, + :ip_address, + :view_name, + :referrer, + :message, + :user_id) + end + + def toggle + t = RailsToggle.new + t.should_include? + end + end +end + + diff --git a/tests/spec/test_helper.rb b/tests/spec/minitest_helper.rb similarity index 100% rename from tests/spec/test_helper.rb rename to tests/spec/minitest_helper.rb diff --git a/tests/spec/rails_toggle_spec.rb b/tests/spec/rails_toggle_spec.rb index 5bead74..b74c76a 100644 --- a/tests/spec/rails_toggle_spec.rb +++ b/tests/spec/rails_toggle_spec.rb @@ -1,38 +1,26 @@ # Responsability # Test whether rails version > 4 # includes attr_accessible if < 4 -require 'test_helper.rb' +require 'minitest_helper' require 'impressionist/rails_toggle' module Impressionist describe RailsToggle do + before { + @toggle = RailsToggle.new + } + describe "Rails 4" do - before do - @toggle = RailsToggle.new("4") + + # see your_minitest_path/lib/minitest/mock.rb + it "must not include attr_accessible" do + @toggle.stub :ask_rails, false do + refute @toggle.should_include? + end 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/setup_association_spec.rb b/tests/spec/setup_association_spec.rb new file mode 100644 index 0000000..3ddc7f3 --- /dev/null +++ b/tests/spec/setup_association_spec.rb @@ -0,0 +1,55 @@ +require 'minitest_helper' +require 'impressionist/setup_association' + +module Impressionist + describe SetupAssociation do + + let(:mock) { Minitest::Mock.new } + let(:set_up) { SetupAssociation.new(mock) } + + before do + # expects attr_accessible to return true + # and pass 11 arguments + mock. + expect(:attr_accessible, true) do |args| + args.size == 11 + end + + end + + describe "attr_accessible" do + + it "includes" do + set_up.stub :toggle, true do + set_up.include_attr_acc?.must_equal true + + mock.verify + end + end + + end + + describe "belongs_to" do + + it "active_record" do + mock.expect(:belongs_to, true, [Symbol, Hash]) + set_up.define_belongs_to.must_equal true + end + + end + + describe "#set" do + + it "sets an association" do + def set_up.include_attr_acc?; true; end + + set_up.stub(:define_belongs_to, true) { + set_up.set.must_equal :done + } + + end + + end + + end +end