diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6553133 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: ruby +rvm: + - 2.0.0 + - 1.9.3 \ No newline at end of file diff --git a/Gemfile b/Gemfile index 896d547..07db6e7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,8 @@ -source "https://rubygems.org" +source 'https://rubygems.org' -gemspec \ No newline at end of file +gemspec + +group :development, :test do + gem 'rspec' + gem 'sqlite3' +end \ No newline at end of file diff --git a/Rakefile b/Rakefile index 13a79ed..5786e6a 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,7 @@ -require "bundler/gem_tasks" \ No newline at end of file +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new + +task :default => :spec +task :test => :spec \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..470e96a --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,47 @@ +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift(File.dirname(__FILE__)) + +require 'active_record' +require 'unscoped_associations' + +ActiveRecord::Base.logger = Logger.new(STDOUT) + +ActiveRecord::Base.establish_connection( + :adapter => 'sqlite3', + :database => ':memory:' +) + +ActiveRecord::Schema.define do + self.verbose = false + + create_table :users, :force => true do |t| + t.boolean :active + end + + create_table :comments, :force => true do |t| + t.integer :user_id + t.boolean :public + end +end + +class User < ActiveRecord::Base + has_many :comments + has_many :all_comments, class_name: 'Comment', unscoped: true + has_one :last_comment, class_name: 'Comment', order: 'id DESC', unscoped: true + + default_scope where( active: true ) +end + +class Comment < ActiveRecord::Base + belongs_to :user, unscoped: true + + default_scope where( public: true ) +end + +RSpec.configure do |config| + config.treat_symbols_as_metadata_keys_with_true_values = true + config.run_all_when_everything_filtered = true + config.filter_run :focus + config.mock_with :rspec + config.order = 'random' +end diff --git a/spec/unscoped_associations_spec.rb b/spec/unscoped_associations_spec.rb new file mode 100644 index 0000000..6dd74ba --- /dev/null +++ b/spec/unscoped_associations_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe UnscopedAssociations do + it 'belongs_to with unscoped option should skip default_scope' do + user = User.create(:active => false) + comment = Comment.create(:user_id => user.id) + + expect(comment.user).to eq(user) + end +end \ No newline at end of file diff --git a/unscoped_associations.gemspec b/unscoped_associations.gemspec index 70d19db..5f3d8d9 100644 --- a/unscoped_associations.gemspec +++ b/unscoped_associations.gemspec @@ -19,5 +19,7 @@ Gem::Specification.new do |spec| spec.add_dependency "activerecord", ">= 0" spec.add_development_dependency "debugger" + spec.add_development_dependency "rspec" + spec.add_development_dependency "sqlite3" end