scope_chain 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/scope_chain.rb +18 -7
- data/lib/scope_chain/version.rb +1 -1
- data/spec/scope_chain_spec.rb +4 -0
- metadata +1 -1
data/lib/scope_chain.rb
CHANGED
@@ -16,32 +16,43 @@ module ScopeChain
|
|
16
16
|
end
|
17
17
|
|
18
18
|
class AssociationChain
|
19
|
-
|
19
|
+
class MissingAssociationError < StandardError; end
|
20
|
+
attr_accessor :instance, :association_name, :association
|
20
21
|
|
21
22
|
def initialize(instance)
|
22
23
|
self.instance = instance
|
23
24
|
end
|
24
25
|
|
25
26
|
def as(association_name)
|
26
|
-
self.
|
27
|
+
self.association_name = association_name
|
27
28
|
|
28
29
|
chain
|
29
30
|
end
|
30
31
|
|
31
32
|
private
|
32
|
-
def association_name(klass)
|
33
|
-
klass.name.underscore.pluralize
|
34
|
-
end
|
35
|
-
|
36
33
|
def chain
|
34
|
+
raise MissingAssociationError.new("Can't find association #{association_name} on #{instance.class.name}") if association.nil?
|
35
|
+
|
37
36
|
ScopeChain::Chain.new(association.klass).tap do |chain|
|
38
37
|
instance.stubs(association.name => association.klass)
|
38
|
+
|
39
|
+
# This is a nasty hack
|
40
|
+
chain.define_singleton_method(:build) do |*arguments|
|
41
|
+
add_link :build, *arguments
|
42
|
+
end
|
43
|
+
|
44
|
+
association.klass.define_singleton_method(:build) {}
|
39
45
|
end
|
40
46
|
end
|
47
|
+
|
48
|
+
def association
|
49
|
+
# TODO See if we can just mock for the reflection
|
50
|
+
@association ||= instance.class.reflect_on_association(association_name)
|
51
|
+
end
|
41
52
|
end
|
42
53
|
|
43
54
|
class Chain
|
44
|
-
LINKS = [:select, :where, :includes, :order, :find, :sum, :new, :create, :create
|
55
|
+
LINKS = [:select, :where, :includes, :order, :find, :sum, :new, :create, :create!, :limit, :joins]
|
45
56
|
ALIASES = {}
|
46
57
|
|
47
58
|
class ConflictedExistenceError < StandardError
|
data/lib/scope_chain/version.rb
CHANGED
data/spec/scope_chain_spec.rb
CHANGED
@@ -147,6 +147,10 @@ describe ScopeChain::Chain do
|
|
147
147
|
end
|
148
148
|
|
149
149
|
context "with associations" do
|
150
|
+
it "raises an error with a missing association" do
|
151
|
+
expect { ScopeChain.on(Owner.new).as(:missing_association)}.to raise_error(ScopeChain::AssociationChain::MissingAssociationError)
|
152
|
+
end
|
153
|
+
|
150
154
|
describe "has_many" do
|
151
155
|
it "properly sets stuff up" do
|
152
156
|
source = Owner.new
|