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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 251bda842b9bd7397e47653d3295e8870df5e98f
4
- data.tar.gz: 9fb57d460e2d263244106de84543738c2e2464db
3
+ metadata.gz: 0dc3f68cf2eb84764efca80777738c551537ee33
4
+ data.tar.gz: beb196f15f49ee0c8c48d37e6950c490ed4fcbbd
5
5
  SHA512:
6
- metadata.gz: 6c156ac6ca99836791406c79d3e9b0615caf1f469d4e0b49ca20b39906cb555eb91ed69008dcf70d57aebd0da87a97386ea84fae0ac1f82945bf4995589f9940
7
- data.tar.gz: 80610029667cf7451440ddf7505f4d0dcb2f8dfbfd7db37379d075d56f3fa5cccada3771cbe863ce291edd14c0cc507a8e80945f9882da41c07f80e4f1d52396
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.5)
4
+ rails_optimizer (0.1.6)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -13,33 +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
- 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
@@ -1,3 +1,3 @@
1
1
  module RailsOptimizer
2
- VERSION = "0.1.6"
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.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Facundo A. Díaz Martínez