selfish_associations 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37dbe42af345cf9c60fff0b7dde1d3c2fee952a0
|
4
|
+
data.tar.gz: 6a31f1c1d716d52783ff858e395a944a628b2b00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc986cfd6243a3f802aee3c7f1d3cd46bb8516ddebf48e8d0145e3ebd7d416ef4f08581b0124ad3e4cbaeadc58d3e7b2dede36c807d7a7a03c4307b9d7ac5a5f
|
7
|
+
data.tar.gz: 9406b9a11d824ecfbc2db5292bfe03e63d0736da2a9f7607df365600fe24f835575ad3489fc5f86115f41e5ca5a355f0fa358c582788e96cfd6e4ba45be6a7c4
|
@@ -13,10 +13,12 @@ module SelfishAssociations
|
|
13
13
|
options = options.symbolize_keys
|
14
14
|
@name = name.to_s
|
15
15
|
@model = model
|
16
|
-
@scope = scope
|
17
16
|
@foreign_class_name = (options[:class_name] || name.to_s.classify).to_s
|
18
17
|
@foreign_key = options[:foreign_key] == false ? false : (options[:foreign_key] || @model.name.foreign_key).to_sym
|
19
|
-
@
|
18
|
+
@scopes = []
|
19
|
+
|
20
|
+
add_scope(scope) if scope.present?
|
21
|
+
add_scope(foreign_key_scope) if @foreign_key.present?
|
20
22
|
validate
|
21
23
|
end
|
22
24
|
|
@@ -38,15 +40,22 @@ module SelfishAssociations
|
|
38
40
|
foreign_class.where(instance_find_conditions(instance))
|
39
41
|
end
|
40
42
|
|
43
|
+
def initialize_for(instance)
|
44
|
+
foreign_class.new(instance_create_attributes(instance))
|
45
|
+
end
|
46
|
+
|
41
47
|
def create_for(instance)
|
42
48
|
foreign_class.create(instance_create_attributes(instance))
|
43
49
|
end
|
44
50
|
|
45
51
|
private
|
46
52
|
|
53
|
+
def add_scope(scope)
|
54
|
+
@scopes << scope
|
55
|
+
end
|
56
|
+
|
47
57
|
def apply_scopes(reader)
|
48
|
-
reader.read(
|
49
|
-
reader.read(@foreign_key_scope) if @foreign_key.present?
|
58
|
+
@scopes.each{|scope| reader.read(scope) }
|
50
59
|
return reader
|
51
60
|
end
|
52
61
|
|
@@ -111,9 +120,9 @@ module SelfishAssociations
|
|
111
120
|
end
|
112
121
|
|
113
122
|
def validate
|
114
|
-
|
115
|
-
|
116
|
-
|
123
|
+
@scopes.each do |scope|
|
124
|
+
scope.is_a?(Proc) or raise SelfishAssociations::SelfishException, "Scope must be a Proc"
|
125
|
+
scope.arity == 0 || scope.arity == 1 or raise SelfishAssociations::SelfishException, "Scope must have arity of 0 or 1"
|
117
126
|
end
|
118
127
|
@model.is_a?(Class) && @model < ActiveRecord::Base or raise SelfishAssociations::SelfishException, "Tried to define a SelfishAssociation for an invalid object (#{@model})"
|
119
128
|
end
|
@@ -54,14 +54,11 @@ module SelfishAssociations
|
|
54
54
|
merge ? PathMerger.new(@associations).merge : @associations
|
55
55
|
end
|
56
56
|
|
57
|
-
#
|
58
|
-
# Really we could initialize anew at each node and pre-define all methods
|
59
|
-
# But this actually seems more lightweight.
|
60
|
-
# Intercept any method to check if it is an association or a column
|
57
|
+
# Check if method is an association or a column
|
61
58
|
# If Association, store current node and iterate to the association.
|
62
59
|
# If it is a column, return an Arel::Node representing that value
|
63
60
|
# Else, raise NoMethodError
|
64
|
-
def
|
61
|
+
def send(method, *args)
|
65
62
|
if @klass.column_names.include?(method.to_s)
|
66
63
|
@associations << @path if @path.present?
|
67
64
|
node = @klass.arel_table[method]
|
@@ -81,5 +78,12 @@ module SelfishAssociations
|
|
81
78
|
::Kernel.raise ::NoMethodError, message
|
82
79
|
end
|
83
80
|
end
|
81
|
+
|
82
|
+
# Method Missing pattern (reluctantly).
|
83
|
+
# Really we could initialize a new at each node and pre-define all methods
|
84
|
+
# But this actually seems more lightweight.
|
85
|
+
def method_missing(method, *args)
|
86
|
+
send(method, *args)
|
87
|
+
end
|
84
88
|
end
|
85
|
-
end
|
89
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: selfish_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Schwartz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Use instance-level where conditions with joinable associations
|
14
14
|
email: ozydingo@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|