rails_optimizer 0.1.2 → 0.1.7

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: 51932ccdf691ba5d2b226ea43f5fb68dc1e45aad
4
- data.tar.gz: 9529d9c3dd83c7f08ba150e42197d6dd2986e355
3
+ metadata.gz: 0dc3f68cf2eb84764efca80777738c551537ee33
4
+ data.tar.gz: beb196f15f49ee0c8c48d37e6950c490ed4fcbbd
5
5
  SHA512:
6
- metadata.gz: 2f4f82db45e9fde9d63043102358e65a2b854de76f51c118056cfb237c6f0e779a424e5009759e35e0ffb7327cf6235c9eec46eca3ac7fe2939b9cd6ebd3275a
7
- data.tar.gz: 803ddf88add1e53c52b6118676a821e51fe28b6a3efd30918c9774fc6ebd2f082fdcd73f51883aaf2810646003c51b8710be00383bdfa727b502630fcb990343
6
+ metadata.gz: a35ebf74d2a112055ec3b33b68411a6ff714710b85c44add6d1851980e17346fb2795cb7e94e42f03bb09fd2fc0786122fd2a57454e0282fa40756bdf55b2e43
7
+ data.tar.gz: 1850a36c5e5532467bc65da93beae6bdb6deccb66c94aeb7d3ac6e215d7914f7c0ce44b98726557e902b96dce0aba391fd93190becdb6958a98b2b3bf657bb39
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_optimizer (0.1.1)
4
+ rails_optimizer (0.1.6)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -13,32 +13,7 @@ module RailsOptimizer
13
13
  end
14
14
  end
15
15
 
16
- def self.scoped proc
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
- if options[:polymorphic]
34
- read_attribute("#{name}_type".to_sym).classify
35
- else
36
- name.to_s.classify
37
- end.constantize.select(:id, *args).scoped(scope).finded(self, name)
38
- end
39
- end
40
-
41
- def self.has_one(name, scope = nil, **options)
16
+ def self.has_one(name, scope = nil, **options)
42
17
  super
43
18
  define_method name.to_s do |*args|
44
19
  fk = self.class.foreign_key(name).to_sym
@@ -49,5 +24,44 @@ module RailsOptimizer
49
24
  end
50
25
  end
51
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
+
52
66
  end
53
67
  end
@@ -1,3 +1,3 @@
1
1
  module RailsOptimizer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Facundo A. Díaz Martínez