added minitest tests, added 2 entities in order to handle compability between rails versions
This commit is contained in:
parent
558340a610
commit
b05aa79ad3
5
Rakefile
5
Rakefile
|
@ -27,7 +27,8 @@ end
|
||||||
# Rake libs default is lib
|
# Rake libs default is lib
|
||||||
# libs << path to load test_helper, etc..
|
# libs << path to load test_helper, etc..
|
||||||
Rake::TestTask.new do |t|
|
Rake::TestTask.new do |t|
|
||||||
t.libs << 'tests/spec'
|
t.libs << "tests/spec"
|
||||||
t.pattern = FileList['tests/spec/*_spec.rb']
|
t.pattern = "tests/spec/**/*_spec.rb"
|
||||||
|
t.test_files = FileList["tests/spec/**/*_spec.rb"]
|
||||||
t.verbose = true
|
t.verbose = true
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
||||||
# Capybara has dropped support for Ruby 1.8.7 onwards version 2.0.3
|
# 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 'capybara', '>= 2.0.3'
|
||||||
s.add_development_dependency 'rake', '>= 0.9'
|
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 'rdoc', '>= 2.4.2'
|
||||||
s.add_development_dependency 'rspec-rails'
|
s.add_development_dependency 'rspec-rails'
|
||||||
s.add_development_dependency 'simplecov'
|
s.add_development_dependency 'simplecov'
|
||||||
|
|
|
@ -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/update_counters'
|
||||||
|
|
||||||
require "impressionist/rails_toggle"
|
|
||||||
|
|
||||||
|
require 'impressionist/rails_toggle'
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
# Responsability
|
# Responsability
|
||||||
|
## See CounterCache TODO: extract it into a class
|
||||||
# * be able to update_counters
|
# * be able to update_counters
|
||||||
# * log an error if imps_id and imps_type can not be found
|
# * 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
|
# FIX exeception raising when no imps_id is found
|
||||||
|
|
||||||
class Impression < ActiveRecord::Base
|
class Impression < ActiveRecord::Base
|
||||||
|
|
||||||
include Impressionist::CounterCache
|
include Impressionist::CounterCache
|
||||||
include Impressionist::SetUpAssociation
|
# sets belongs_to and attr_accessible
|
||||||
|
Impressionist::SetupAssociation.new(self).set
|
||||||
|
|
||||||
after_save :impressionable_counter_cache_updatable?
|
after_save :impressionable_counter_cache_updatable?
|
||||||
|
|
||||||
|
|
|
@ -2,25 +2,21 @@ module Impressionist
|
||||||
# Responsability
|
# Responsability
|
||||||
# Toggles between rails > 3.1 < 4
|
# Toggles between rails > 3.1 < 4
|
||||||
# In order to make attr_accessible available in a rails app < 4
|
# In order to make attr_accessible available in a rails app < 4
|
||||||
|
|
||||||
class RailsToggle
|
class RailsToggle
|
||||||
|
# decides where or not to include attr_accessible
|
||||||
attr_reader :r_version
|
def should_include?
|
||||||
|
ask_rails || false
|
||||||
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def greater_than_4?
|
# returns false if rails >= 4
|
||||||
r_version == "4" ? true : false
|
# true if rails < 4
|
||||||
|
def ask_rails
|
||||||
|
::Rails::VERSION::MAJOR.to_i >= 4 ? false : true
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -1,38 +1,26 @@
|
||||||
# Responsability
|
# Responsability
|
||||||
# Test whether rails version > 4
|
# Test whether rails version > 4
|
||||||
# includes attr_accessible if < 4
|
# includes attr_accessible if < 4
|
||||||
require 'test_helper.rb'
|
require 'minitest_helper'
|
||||||
require 'impressionist/rails_toggle'
|
require 'impressionist/rails_toggle'
|
||||||
|
|
||||||
module Impressionist
|
module Impressionist
|
||||||
describe RailsToggle do
|
describe RailsToggle do
|
||||||
|
|
||||||
|
before {
|
||||||
|
@toggle = RailsToggle.new
|
||||||
|
}
|
||||||
|
|
||||||
describe "Rails 4" do
|
describe "Rails 4" do
|
||||||
before do
|
|
||||||
@toggle = RailsToggle.new("4")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "return current rails version" do
|
# see your_minitest_path/lib/minitest/mock.rb
|
||||||
@toggle.r_version.must_equal "4"
|
it "must not include attr_accessible" do
|
||||||
end
|
@toggle.stub :ask_rails, false do
|
||||||
|
refute @toggle.should_include?
|
||||||
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue