Upgrade to rails 4.1

This commit is contained in:
wvengen 2014-11-21 14:37:56 +01:00
parent 6188567931
commit 6e990fed4c
23 changed files with 179 additions and 197 deletions

View file

@ -2,15 +2,24 @@ require 'factory_girl'
FactoryGirl.define do
factory :article do
sequence(:name) { |n| Faker::Lorem.words(rand(2..4)).join(' ') + " ##{n}" }
factory :_article do
unit { Faker::Unit.unit }
price { rand(2600) / 100 }
tax { [6, 21].sample }
deposit { rand(10) < 8 ? 0 : [0.0, 0.80, 1.20, 12.00].sample }
unit_quantity { rand(5) < 3 ? 1 : rand(1..20) }
supplier { create :supplier }
article_category { create :article_category }
factory :article do
sequence(:name) { |n| Faker::Lorem.words(rand(2..4)).join(' ') + " ##{n}" }
supplier { create :supplier }
article_category { create :article_category }
end
factory :shared_article, class: SharedArticle do
sequence(:name) { |n| Faker::Lorem.words(rand(2..4)).join(' ') + " s##{n}" }
order_number { Faker::Lorem.characters(rand(1..12)) }
supplier { create :shared_supplier }
end
end
factory :article_category do

View file

@ -7,7 +7,7 @@ FactoryGirl.define do
supplier { create :supplier, article_count: (article_count.nil? ? true : article_count) }
article_ids { supplier.articles.map(&:id) unless supplier.nil? }
ignore do
transient do
article_count true
end

View file

@ -7,7 +7,7 @@ FactoryGirl.define do
phone { Faker::PhoneNumber.phone_number }
address { Faker::Address.street_address }
ignore do
transient do
article_count 0
end
@ -16,6 +16,8 @@ FactoryGirl.define do
article_count = rand(1..99) if article_count == true
create_list :article, article_count, supplier: supplier
end
factory :shared_supplier, class: SharedSupplier
end
end

View file

@ -59,9 +59,8 @@ describe Article do
end
describe 'connected to a shared database', :type => :feature do
let(:shared_supplier) { create(:supplier) }
let(:shared_article) { create :article, supplier: shared_supplier, order_number: Faker::Lorem.characters(rand(1..12)) }
let(:supplier) { create :supplier, shared_supplier_id: shared_supplier.id }
let(:shared_article) { create :shared_article }
let(:supplier) { create :supplier, shared_supplier_id: shared_article.supplier_id }
let(:article) { create :article, supplier: supplier, order_number: shared_article.order_number }
it 'can be found in the shared database' do
@ -85,7 +84,7 @@ describe Article do
end
it 'does not need to synchronise an imported article' do
article = SharedArticle.find(shared_article.id).build_new_article(supplier)
article = shared_article.build_new_article(supplier)
expect(article.shared_article_changed?).to be_falsey
end
@ -93,7 +92,7 @@ describe Article do
shared_article.unit = '1kg'
shared_article.unit_quantity = 1
shared_article.save!
article = SharedArticle.find(shared_article.id).build_new_article(supplier)
article = shared_article.build_new_article(supplier)
article.article_category = create :article_category
article.unit = '200g'
article.shared_updated_on -= 1 # to make update do something

View file

@ -1,29 +1,16 @@
# http://stackoverflow.com/questions/8774227
# http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
ActiveSupport.on_load(:after_initialize) do
# We simulate the shared database by pointing to our own database.
# This allows running tests without additional database setup.
# But take care when designing tests using the shared database.
SharedSupplier.establish_connection Rails.env
SharedArticle.establish_connection Rails.env
SharedSupplier.establish_connection Rails.env.to_sym
SharedArticle.establish_connection Rails.env.to_sym
# hack for different structure of shared database
SharedArticle.class_eval do
belongs_to :supplier, class_name: 'SharedSupplier'
alias_attribute :number, :order_number
alias_attribute :updated_on, :updated_at
def category
ArticleCategory.find(article_category_id).name
ArticleCategory.where(id: article_category_id).first
end
def self.find_by_number(n)
find_by_order_number(n)