Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ source "http://rubygems.org"

# Specify your gem's dependencies in record_with_operator.gemspec
gemspec

gem 'minitest', '~> 5.7.0'
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion lib/record_with_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions lib/record_with_operator/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/record_with_operator_belongs_to_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/record_with_operator_has_many_dependent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +16,7 @@ def check_operator
end

class MemoWithUserWithDependency < ActiveRecord::Base
set_table_name "memos"
self.table_name = "memos"

before_destroy :check_operator

Expand All @@ -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
4 changes: 2 additions & 2 deletions test/record_with_operator_has_one_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/record_with_operator_reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
30 changes: 15 additions & 15 deletions test/record_with_operator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/record_with_operator_user_class_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,7 +22,7 @@ def deleted?
end

class AdminUser < ActiveRecord::Base
set_table_name "users"
self.table_name = "users"
end

class RecordWithOperatorUserClassNameTest < ActiveSupport::TestCase
Expand Down
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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")