prim 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,8 @@ module Prim
26
26
  # Store this configuration for global access.
27
27
  Prim.configured_primaries << self.prim_relationships[ singular_name ]
28
28
 
29
+ # Create dynamic methods for getting and setting the
30
+ # primary Collection member.
29
31
  define_method "primary_#{ singular_name }" do
30
32
  prim_collection_for(singular_name).primary
31
33
  end
@@ -33,6 +35,14 @@ module Prim
33
35
  define_method "primary_#{ singular_name }=" do |instance|
34
36
  prim_collection_for(singular_name).primary = instance
35
37
  end
38
+
39
+ # We alias the association's method chain so the original can be accessed.
40
+ # IS THIS IMPORTANT?!
41
+ define_method "#{ association_name }_with_primaries" do
42
+ prim_collection_for(singular_name).all
43
+ end
44
+
45
+ alias_method_chain association_name, :primaries
36
46
  end
37
47
  end
38
48
 
@@ -15,9 +15,6 @@ module Prim
15
15
  def initialize relationship, instance
16
16
  @instance = instance
17
17
  @relationship = relationship
18
-
19
- # Attach the relationship to the mapping class.
20
- relationship.reflected_class.prim_relationship = relationship
21
18
  end
22
19
 
23
20
  # Loads the primary member of this collection.
@@ -48,6 +45,20 @@ module Prim
48
45
  true
49
46
  end
50
47
 
48
+ def all options = {}
49
+ options[:with_primaries] ||= true
50
+
51
+ if options[:with_primaries]
52
+ primary_member = primary
53
+ sources.tap do |records|
54
+ records.find { |r| r == primary_member }.try(:primary=, true)
55
+ records
56
+ end
57
+ else
58
+ sources
59
+ end
60
+ end
61
+
51
62
  private
52
63
 
53
64
  # Creates a new source record and a mapping between it and the owner instance.
@@ -63,12 +74,12 @@ module Prim
63
74
  mappings.create( relationship.foreign_key => source_record.id, primary: true )
64
75
  end
65
76
 
66
- def mappings force_reload = false
67
- instance.send( relationship.collection_label, force_reload )
77
+ def mappings
78
+ instance.association( relationship.collection_label ).proxy
68
79
  end
69
80
 
70
- def sources force_reload = false
71
- instance.send( relationship.association_name, force_reload )
81
+ def sources
82
+ instance.association( relationship.association_name ).proxy
72
83
  end
73
84
 
74
85
  # Returns the mapping for a given source record. If this Relationship doesn't
@@ -5,10 +5,16 @@ module Prim
5
5
  module InstanceMethods
6
6
 
7
7
  module Owner
8
+ extend ActiveSupport::Concern
9
+
8
10
  def prim_collection_for singular_name
9
11
  @_prim_collections ||= {}
10
12
  @_prim_collections[ singular_name ] ||= Prim::Collection.new(self.class.prim_relationships[ singular_name ], self)
11
13
  end
14
+
15
+ included do
16
+ alias_method :assign_prim_collection_for, :prim_collection_for
17
+ end
12
18
  end
13
19
 
14
20
  module Reflected
@@ -25,6 +31,8 @@ module Prim
25
31
  def only_one_primary
26
32
  if self[:primary]
27
33
  siblings.update_all('"primary" = false')
34
+ elsif siblings.where( primary: true ).first.nil?
35
+ self[:primary] = true
28
36
  end
29
37
  end
30
38
 
@@ -35,6 +35,7 @@ module Prim
35
35
 
36
36
  reflected_class.send :include, InstanceMethods::Reflected
37
37
  reflected_class.class_attribute :prim_relationship
38
+ reflected_class.prim_relationship = self
38
39
 
39
40
  source_class.send :include, InstanceMethods::Source if mapping_table?
40
41
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "prim"
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
  s.date = "2013-01-02"
5
5
  s.summary = "Easily manage Rails associations that need a primary member."
6
6
  s.description = "With Prim it's easy to add a primary member to any one-to-many or many-to-many association. " +
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: