diff --git a/Gemfile b/Gemfile index b25de8f..9101b91 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,5 @@ source "http://rubygems.org" # Specify your gem's dependencies in record_with_operator.gemspec gemspec + +gem 'minitest', '~> 5.7.0' diff --git a/README.rdoc b/README.rdoc index 4da0dbd..84b8199 100644 --- a/README.rdoc +++ b/README.rdoc @@ -97,7 +97,7 @@ It's also possible to modify options for creator/updater/deleter associations. For example, in the case you use acts_as_paranoid and want to get creator/updater/deleter even if they are deleted, the configuration will be look like this. - RecordWithOperator.config[:operator_association_options] = {:with_deleted => true} + RecordWithOperator.config[:operator_association_scope] = -> { with_deleted } You can cange created_by, updated_by, deleted_by column name by changing RecordWithOperator.config with keys :creator_column, :updater_column and :deleter_column. diff --git a/lib/record_with_operator.rb b/lib/record_with_operator.rb index 569c409..c3ba0ff 100644 --- a/lib/record_with_operator.rb +++ b/lib/record_with_operator.rb @@ -5,7 +5,8 @@ def self.config :creator_column => "created_by", :updater_column => "updated_by", :deleter_column => "deleted_by", - :operator_association_options => {}} + :operator_association_options => {}, + :operator_association_scope => nil} @config end diff --git a/lib/record_with_operator/recorder.rb b/lib/record_with_operator/recorder.rb index 1816403..4735566 100644 --- a/lib/record_with_operator/recorder.rb +++ b/lib/record_with_operator/recorder.rb @@ -7,7 +7,7 @@ def self.included(base) private def set_creator - send("#{RecordWithOperator.creator_column}=", operator.try(:id)) + send("#{RecordWithOperator.creator_column}=", operator.try!(:id)) end def set_updater @@ -19,7 +19,9 @@ def set_updater def update_deleter return if frozen? return unless operator - self.class.update_all("#{RecordWithOperator.deleter_column} = #{operator.id}", ["#{self.class.primary_key} = ?", id]) + self.class + .where(self.class.primary_key => id) + .update_all(RecordWithOperator.deleter_column => operator.id) send("#{RecordWithOperator.deleter_column}=", operator.id) end @@ -36,9 +38,9 @@ def records_with_operator_on(*args) before_destroy :update_deleter if records_deleter? if self.table_exists? - belongs_to :creator, {:foreign_key => "created_by", :class_name => RecordWithOperator.config[:operator_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if records_creator? - belongs_to :updater, {:foreign_key => "updated_by", :class_name => RecordWithOperator.config[:operator_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if records_updater? - belongs_to :deleter, {:foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:operator_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if records_deleter? + belongs_to :creator, RecordWithOperator.config[:operator_association_scope], {:foreign_key => "created_by", :class_name => RecordWithOperator.config[:operator_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if records_creator? + belongs_to :updater, RecordWithOperator.config[:operator_association_scope], {:foreign_key => "updated_by", :class_name => RecordWithOperator.config[:operator_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if records_updater? + belongs_to :deleter, RecordWithOperator.config[:operator_association_scope], {:foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:operator_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if records_deleter? end end diff --git a/test/record_with_operator_belongs_to_association_test.rb b/test/record_with_operator_belongs_to_association_test.rb index 3a62ea7..6d0f44c 100644 --- a/test/record_with_operator_belongs_to_association_test.rb +++ b/test/record_with_operator_belongs_to_association_test.rb @@ -4,14 +4,14 @@ class User < ActiveRecord::Base end class NoteBelongsToMemo < ActiveRecord::Base - set_table_name "notes" + self.table_name = "notes" belongs_to :memo records_with_operator_on :create, :update, :destroy end class Memo < ActiveRecord::Base - set_table_name "memos" + self.table_name = "memos" records_with_operator_on :create, :update, :destroy end diff --git a/test/record_with_operator_has_many_dependent_test.rb b/test/record_with_operator_has_many_dependent_test.rb index 746dd98..448951d 100644 --- a/test/record_with_operator_has_many_dependent_test.rb +++ b/test/record_with_operator_has_many_dependent_test.rb @@ -4,7 +4,7 @@ class User < ActiveRecord::Base end class NoteWithUserWithDependency < ActiveRecord::Base - set_table_name "notes" + self.table_name = "notes" has_many :memos, :class_name => "MemoWithUserWithDependency", :foreign_key => "note_id", :dependent => :destroy before_destroy :check_operator @@ -16,7 +16,7 @@ def check_operator end class MemoWithUserWithDependency < ActiveRecord::Base - set_table_name "memos" + self.table_name = "memos" before_destroy :check_operator @@ -42,8 +42,8 @@ def setup def test_memos_should_be_destroyed_when_note_is_destroyed @note_created_by_user1.destroy - assert_nil NoteWithUserWithDependency.find_by_id(@note_created_by_user1.id) - assert MemoWithUserWithDependency.find_all_by_note_id(@note_created_by_user1.id).empty? + assert_nil NoteWithUserWithDependency.find_by(:id => @note_created_by_user1.id) + assert MemoWithUserWithDependency.where(:note_id => @note_created_by_user1.id).empty? end end diff --git a/test/record_with_operator_has_one_association_test.rb b/test/record_with_operator_has_one_association_test.rb index e5953fd..3355e26 100644 --- a/test/record_with_operator_has_one_association_test.rb +++ b/test/record_with_operator_has_one_association_test.rb @@ -4,14 +4,14 @@ class User < ActiveRecord::Base end class NoteHasOneMemo < ActiveRecord::Base - set_table_name "notes" + self.table_name = "notes" has_one :memo, :class_name => "Memo", :foreign_key => "note_id" records_with_operator_on :create, :update, :destroy end class Memo < ActiveRecord::Base - set_table_name "memos" + self.table_name = "memos" records_with_operator_on :create, :update, :destroy end diff --git a/test/record_with_operator_reflection_test.rb b/test/record_with_operator_reflection_test.rb index 43c89d2..adcc857 100644 --- a/test/record_with_operator_reflection_test.rb +++ b/test/record_with_operator_reflection_test.rb @@ -4,13 +4,13 @@ class User < ActiveRecord::Base end class NoteForReflectionTest < ActiveRecord::Base - set_table_name "notes" + self.table_name = "notes" records_with_operator_on :create, :update, :destroy end class CreatorNoteForReflectionTest < ActiveRecord::Base - set_table_name "creator_notes" + self.table_name = "creator_notes" records_with_operator_on :create end @@ -26,18 +26,18 @@ def setup end def test_include - assert NoteForReflectionTest.find(:all, :include => [:creator, :updater]) + assert NoteForReflectionTest.includes(:creator, :updater).all.to_a end def test_joins - assert NoteForReflectionTest.find(:all, :joins => [:creator, :updater]) + assert NoteForReflectionTest.joins(:creator, :updater).all.to_a end def test_include_missing_association - assert_raise(ActiveRecord::ConfigurationError) { CreatorNoteForReflectionTest.find(:all, :include => [:updater]) } + assert_raise(ActiveRecord::ConfigurationError) { CreatorNoteForReflectionTest.includes(:updater).all.to_a } end def test_joins_missing_association - assert_raise(ActiveRecord::ConfigurationError) { CreatorNoteForReflectionTest.find(:all, :joins => [:updater]) } + assert_raise(ActiveRecord::ConfigurationError) { CreatorNoteForReflectionTest.joins(:updater).all.to_a } end end diff --git a/test/record_with_operator_test.rb b/test/record_with_operator_test.rb index a345800..b61b483 100644 --- a/test/record_with_operator_test.rb +++ b/test/record_with_operator_test.rb @@ -4,10 +4,10 @@ class User < ActiveRecord::Base end class NoteWithUser < ActiveRecord::Base - set_table_name "notes" + self.table_name = "notes" has_many :memos, :class_name => "MemoWithUser", :foreign_key => "note_id" - scope :new_arrivals, {:order => "updated_at desc"} + scope :new_arrivals, -> { order("updated_at" => "desc") } alias_method :destroy!, :destroy @@ -16,7 +16,7 @@ class NoteWithUser < ActiveRecord::Base def destroy with_transaction_returning_status do _run_destroy_callbacks do - self.class.update_all({:deleted_at => Time.now}, {self.class.primary_key => self.id}) + self.class.where(self.class.primary_key => self.id).update_all(:deleted_at => Time.now) end end freeze @@ -28,25 +28,25 @@ def deleted? end class SimpleNoteWithUser < ActiveRecord::Base - set_table_name "simple_notes" + self.table_name = "simple_notes" end class MemoWithUser < ActiveRecord::Base - set_table_name "memos" + self.table_name = "memos" - scope :new_arrivals, {:order => "updated_at desc"} + scope :new_arrivals, -> { order("updated_at" => "desc") } records_with_operator_on :create, :update, :destroy end class UpdaterNoteWithUser < ActiveRecord::Base - set_table_name "updater_notes" + self.table_name = "updater_notes" records_with_operator_on :update end class DeleterNoteWithUser < ActiveRecord::Base - set_table_name "deleter_notes" + self.table_name = "deleter_notes" records_with_operator_on :destroy @@ -199,21 +199,21 @@ def test_manualy_found_memo_should_have_operator RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) note.memos.create!(:body => "memo") - assert_equal @user2, note.memos.find(:first).operator + assert_equal @user2, note.memos.first.operator end def test_dynamically_found_memo_should_have_operator RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) note.memos.create!(:body => "memo") - assert_equal @user2, note.memos.find_by_body("memo").operator + assert_equal @user2, note.memos.find_by(:body => "memo").operator end def test_dynamically_found_memos_should_have_operator RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) note.memos.create!(:body => "memo") - assert note.memos.find_all_by_body("memo").all?{|m| m.operator == @user2} + assert note.memos.where(:body => "memo").all?{|m| m.operator == @user2} end def test_found_memo_through_named_scope_should_have_operator @@ -227,7 +227,7 @@ def test_dynamically_found_memo_through_named_scope_should_have_operator RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) note.memos.create!(:body => "memo") - assert_equal @user2, note.memos.new_arrivals.find_by_body("memo").operator + assert_equal @user2, note.memos.new_arrivals.find_by(:body => "memo").operator end def test_auto_found_memo_first_should_be_nil_if_note_has_no_memo @@ -247,19 +247,19 @@ def test_auto_found_memo_last_should_be_nil_if_note_has_no_memo def test_manualy_found_memo_first_should_be_nil_if_note_has_no_memo RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) - assert_equal nil, note.memos.find(:first) + assert_equal nil, note.memos.first end def test_manualy_found_memo_last_should_be_nil_if_note_has_no_memo RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) - assert_equal nil, note.memos.find(:last) + assert_equal nil, note.memos.last end def test_dynamically_found_memos_first_should_be_nil_if_note_has_no_memo RecordWithOperator.operator = @user2 note = NoteWithUser.find(@note_created_by_user1.id) - assert_equal nil, note.memos.find_all_by_body("memo").first + assert_equal nil, note.memos.where(:body => "memo").first end def test_found_memo_through_named_scope_last_should_be_nil_if_note_has_no_memo diff --git a/test/record_with_operator_user_class_name_test.rb b/test/record_with_operator_user_class_name_test.rb index e5570ea..ba85e65 100644 --- a/test/record_with_operator_user_class_name_test.rb +++ b/test/record_with_operator_user_class_name_test.rb @@ -4,7 +4,7 @@ RecordWithOperator.config[:operator_class_name] = "AdminUser" class NoteWithAdminUser < ActiveRecord::Base - set_table_name "notes" + self.table_name = "notes" records_with_operator_on :create, :update, :destroy @@ -22,7 +22,7 @@ def deleted? end class AdminUser < ActiveRecord::Base - set_table_name "users" + self.table_name = "users" end class RecordWithOperatorUserClassNameTest < ActiveSupport::TestCase diff --git a/test/test_helper.rb b/test/test_helper.rb index fd0e4f2..f021221 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,7 @@ $:.push File.expand_path('../../', __FILE__) require 'logger' -require 'test/unit' +require 'minitest/autorun' require 'active_record' require 'active_support' require 'active_support/test_case' @@ -11,5 +11,6 @@ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") ActiveRecord::Base.establish_connection(config['sqlite3']) +ActiveSupport::TestCase.test_order = :random load(File.dirname(__FILE__) + "/schema.rb")