immortal 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -41,6 +41,7 @@ If you want to improve immortal
41
41
 
42
42
  ## CHANGELOG
43
43
 
44
+ - 1.0.4 Extract with_deleted singular assoc readers to separate module
44
45
  - 1.0.3 Added back feature where using immortal finders doesn't unscope association scopes.
45
46
  - 1.0.2 Added with/only_deleted singular association readers (see specs)
46
47
  - 1.0.1 Made compatible with Rails 3.1.X
data/immortal.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "immortal"
6
- s.version = '1.0.3'
6
+ s.version = '1.0.4'
7
7
  s.authors = ["Jordi Romero", "Saimon Moore"]
8
8
  s.email = ["jordi@jrom.net", "saimon@saimonmoore.net"]
9
9
  s.homepage = "http://github.com/teambox/immortal"
data/lib/immortal.rb CHANGED
@@ -1,18 +1,14 @@
1
- require 'immortal/belongs_to_builder'
1
+ require 'immortal/belongs_to'
2
2
 
3
3
  module Immortal
4
4
 
5
5
  def self.included(base)
6
6
  base.send :extend, ClassMethods
7
7
  base.send :include, InstanceMethods
8
+ base.send :include, BelongsTo
8
9
  base.class_eval do
9
10
  class << self
10
11
 
11
- # Add with/how_deleted singular association readers
12
- def belongs_to_mortal(name, options = {})
13
- ::Immortal::BelongsToBuilder.build(self, name, options)
14
- end
15
-
16
12
  # In has_many :through => join_model we have to explicitly add
17
13
  # the 'not deleted' scope, otherwise it will take all the rows
18
14
  # from the join model
@@ -25,10 +21,6 @@ module Immortal
25
21
  alias_method :has_many_immortal, :has_many
26
22
  alias_method :has_many, :has_many_mortal
27
23
 
28
- alias_method :belongs_to_immortal, :belongs_to
29
- alias_method :belongs_to, :belongs_to_mortal
30
-
31
-
32
24
  alias :mortal_delete_all :delete_all
33
25
  alias :delete_all :immortal_delete_all
34
26
  end
@@ -0,0 +1,21 @@
1
+ require 'immortal/belongs_to_builder'
2
+
3
+ #Include this to add {with/only}_deleted_ accessors for singular associations
4
+ module Immortal
5
+ module BelongsTo
6
+ def self.included(base)
7
+ base.class_eval do
8
+ class << self
9
+
10
+ # Add with/how_deleted singular association readers
11
+ def belongs_to_mortal(name, options = {})
12
+ ::Immortal::BelongsToBuilder.build(self, name, options)
13
+ end
14
+
15
+ alias_method :belongs_to_immortal, :belongs_to
16
+ alias_method :belongs_to, :belongs_to_mortal
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,6 +1,5 @@
1
1
  module Immortal
2
2
  module SingularAssociation
3
- attr_reader :deleted_target
4
3
 
5
4
  def with_deleted_reader(force_reload = false)
6
5
  deleted_reader('with', force_reload)
@@ -14,24 +13,8 @@ module Immortal
14
13
 
15
14
  def deleted_reader(how, force_reload = false)
16
15
  klass.uncached do
17
- send(:"reload_#{how}_deleted")
16
+ send(:"find_#{how}_deleted_target")
18
17
  end
19
-
20
- deleted_target
21
- end
22
-
23
- def reload_with_deleted
24
- reset
25
- reset_scope
26
- load_deleted_target('with')
27
- self unless deleted_target.nil?
28
- end
29
-
30
- def reload_only_deleted
31
- reset
32
- reset_scope
33
- load_deleted_target('only')
34
- self unless deleted_target.nil?
35
18
  end
36
19
 
37
20
  def find_with_deleted_target
@@ -46,12 +29,5 @@ module Immortal
46
29
  end
47
30
  end
48
31
 
49
- def load_deleted_target(how)
50
- @deleted_target ||= send(:"find_#{how}_deleted_target")
51
- deleted_target
52
- rescue ActiveRecord::RecordNotFound
53
- reset
54
- end
55
-
56
32
  end
57
33
  end
@@ -247,6 +247,38 @@ describe Immortal do
247
247
  node.target.should be_nil
248
248
  end
249
249
 
250
+ it "should reload immortal polymorphic associations using deleted reader (direct assignment)" do
251
+ #setup
252
+ node = ImmortalNode.create! :title => 'testing association 1'
253
+ target_1 = ImmortalSomeTarget.create! :title => 'target 1'
254
+ target_2 = ImmortalSomeOtherTarget.create! :title => 'target 2'
255
+
256
+ #confirm initial state
257
+ node.target.should be_nil
258
+
259
+ #load target & confirm
260
+ node.target = target_1
261
+ node.target.should == target_1
262
+
263
+ #switch target indirectly
264
+ node.target = target_2
265
+
266
+ node.target.should == target_2
267
+ node.target_with_deleted.should == target_2
268
+
269
+ #don't assign directly and destroy new target
270
+ target_2.destroy
271
+
272
+ #Respect what's expected
273
+ node.target(true).should be_nil
274
+
275
+ #Ask for deleted target (or not deleted). Will NOT cache
276
+ node.target_with_deleted.should == target_2
277
+
278
+ #Confirm we haven't invaded the target namespace
279
+ node.target.should be_nil
280
+ end
281
+
250
282
  it "should not unscope associations when using with_deleted scope" do
251
283
  m1 = ImmortalModel.create! :title => 'previously created model'
252
284
  n1 = ImmortalNode.create! :title => 'previously created association'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immortal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2011-10-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- requirement: &70129308919760 !ruby/object:Gem::Requirement
17
+ requirement: &70364462440520 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 3.1.1
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70129308919760
25
+ version_requirements: *70364462440520
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70129308919280 !ruby/object:Gem::Requirement
28
+ requirement: &70364462440040 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 2.6.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70129308919280
36
+ version_requirements: *70364462440040
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sqlite3
39
- requirement: &70129308918900 !ruby/object:Gem::Requirement
39
+ requirement: &70364462439660 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,7 +44,7 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70129308918900
47
+ version_requirements: *70364462439660
48
48
  description: Typical paranoid gem built for Rails 3 and with the minimum code needed
49
49
  to satisfy acts_as_paranoid's API
50
50
  email:
@@ -61,6 +61,7 @@ files:
61
61
  - Rakefile
62
62
  - immortal.gemspec
63
63
  - lib/immortal.rb
64
+ - lib/immortal/belongs_to.rb
64
65
  - lib/immortal/belongs_to_builder.rb
65
66
  - lib/immortal/singular_association.rb
66
67
  - spec/immortal_spec.rb