rails_optimizer 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/rails_optimizer.rb +40 -27
- data/lib/rails_optimizer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dc3f68cf2eb84764efca80777738c551537ee33
|
4
|
+
data.tar.gz: beb196f15f49ee0c8c48d37e6950c490ed4fcbbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a35ebf74d2a112055ec3b33b68411a6ff714710b85c44add6d1851980e17346fb2795cb7e94e42f03bb09fd2fc0786122fd2a57454e0282fa40756bdf55b2e43
|
7
|
+
data.tar.gz: 1850a36c5e5532467bc65da93beae6bdb6deccb66c94aeb7d3ac6e215d7914f7c0ce44b98726557e902b96dce0aba391fd93190becdb6958a98b2b3bf657bb39
|
data/Gemfile.lock
CHANGED
data/lib/rails_optimizer.rb
CHANGED
@@ -13,33 +13,7 @@ module RailsOptimizer
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.
|
17
|
-
return proc.call if proc
|
18
|
-
all
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.finded obj, relation_name
|
22
|
-
fk = foreign_key(obj, relation_name)
|
23
|
-
find(obj.read_attribute(fk.to_sym)) unless obj.read_attribute(fk.to_sym).blank?
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.foreign_key(obj, relation_name)
|
27
|
-
obj.class.reflections[relation_name.to_s].foreign_key
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.belongs_to(name, scope = nil, **options)
|
31
|
-
super
|
32
|
-
define_method name.to_s do |*args|
|
33
|
-
super if args.empty?
|
34
|
-
klass = if options[:polymorphic]
|
35
|
-
read_attribute("#{name}_type".to_sym)
|
36
|
-
else
|
37
|
-
name.to_s
|
38
|
-
end.classify.constantize.select(*args).scoped(scope).finded(self, name)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.has_one(name, scope = nil, **options)
|
16
|
+
def self.has_one(name, scope = nil, **options)
|
43
17
|
super
|
44
18
|
define_method name.to_s do |*args|
|
45
19
|
fk = self.class.foreign_key(name).to_sym
|
@@ -50,5 +24,44 @@ module RailsOptimizer
|
|
50
24
|
end
|
51
25
|
end
|
52
26
|
end
|
27
|
+
|
28
|
+
def self.belongs_to(name, scope = nil, **options)
|
29
|
+
super
|
30
|
+
_define(name, scope, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.scoped proc
|
36
|
+
return proc.call if proc
|
37
|
+
all
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.finded obj, relation_name
|
41
|
+
fk = foreign_key(obj, relation_name)
|
42
|
+
find(obj.read_attribute(fk.to_sym)) unless obj.read_attribute(fk.to_sym).blank?
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.foreign_key(obj, relation_name)
|
46
|
+
obj.class.reflections[relation_name.to_s].foreign_key
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def self._define(name, scope=nil, **options)
|
52
|
+
define_method name.to_s do |*args|
|
53
|
+
klass = if options[:polymorphic]
|
54
|
+
read_attribute("#{name}_type".to_sym).constantize
|
55
|
+
else
|
56
|
+
_reflections[name.to_s].klass
|
57
|
+
end
|
58
|
+
if args.empty?
|
59
|
+
klass.select('*').scoped(scope).finded(self, name)
|
60
|
+
else
|
61
|
+
klass.select(*args).scoped(scope).finded(self, name)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
53
66
|
end
|
54
67
|
end
|