rails_optimizer 0.1.3 → 0.1.8

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: 4dbcf811feb6eb7b70788be32435558802e7a318
4
- data.tar.gz: 11a915e895561996d898497b4de9cb0ff98b89e3
3
+ metadata.gz: 07471bbbc0f17cc67af8e6a2aa3ca044ae3c3583
4
+ data.tar.gz: 542301109d50edcc8312f6b15f63ce0549dc41ee
5
5
  SHA512:
6
- metadata.gz: aee3b50dfd806091de19ce996854a7a05029f97f4e6fc72d0ed54f322a3f77ed07284f652a9576fdc8ef5fc6ce237f2172080e1729ae17223340b2830a8d1afc
7
- data.tar.gz: 4fc5b1d865a4dbb59987dd6b36c9c8f900a3c5c7e856643f8bb1fbccf45f6efe5c514a1a50adc29531658d8e87219d3db36695fc293e311e63a0f0f8dceca6f7
6
+ metadata.gz: 50a778ca853bf9f22c62c58c64de441b9d66f0ca69038137f6f54175878ea9f791f84f699f1cea384fc26434eb568922e22bcfd030ff94e3acdbc94a0d4ccf1f
7
+ data.tar.gz: 5752120565190145cbadcb7336045f28c9345504a1571e2b2b8a342cc1886fa4f6107714779234b491b274237f7785b0158e2045798cbaa207e7ba837e3a115e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_optimizer (0.1.2)
4
+ rails_optimizer (0.1.7)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -1,4 +1,6 @@
1
1
  require "rails_optimizer/version"
2
+ require "rails_optimizer/belongs_to"
3
+ require "rails_optimizer/reflection"
2
4
 
3
5
  module RailsOptimizer
4
6
  class Error < StandardError; end
@@ -13,33 +15,7 @@ module RailsOptimizer
13
15
  end
14
16
  end
15
17
 
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
- if options[:polymorphic]
35
- read_attribute("#{name}_type".to_sym).classify
36
- else
37
- name.to_s.classify
38
- end.constantize.select(*args).scoped(scope).finded(self, name)
39
- end
40
- end
41
-
42
- def self.has_one(name, scope = nil, **options)
18
+ def self.has_one(name, scope = nil, **options)
43
19
  super
44
20
  define_method name.to_s do |*args|
45
21
  fk = self.class.foreign_key(name).to_sym
@@ -50,5 +26,15 @@ module RailsOptimizer
50
26
  end
51
27
  end
52
28
  end
29
+
30
+ def self.belongs_to(name, scope = nil, **options)
31
+ super
32
+ RailsOptimizer::BelongsTo._define(self, name)
33
+ end
34
+
35
+ def self.execute(&block)
36
+ block.call(self) || all
37
+ end
38
+
53
39
  end
54
40
  end
@@ -0,0 +1,49 @@
1
+ module RailsOptimizer
2
+ class BelongsTo
3
+ attr_accessor :klass, :name, :args, :reflection, :owner
4
+
5
+ def initialize(owner, name, *args)
6
+ @owner = owner
7
+ @name = name.to_s
8
+ @args = *args
9
+ end
10
+
11
+ def self._define(owner, name )
12
+ owner.__send__(:define_method, name) do |*args|
13
+ return RailsOptimizer::BelongsTo.new(self, name, *args).get_target
14
+ end
15
+ end
16
+
17
+ def get_target
18
+ if args.empty?
19
+ klass
20
+ else
21
+ klass.select(*args)
22
+ end
23
+ .execute{reflection_scope}
24
+ .execute{ |obj| finded(obj)}
25
+ end
26
+
27
+ private
28
+ def reflection
29
+ @reflection ||= RailsOptimizer::Reflection.new(owner, name)
30
+ end
31
+
32
+ def klass
33
+ reflection.klass
34
+ end
35
+
36
+ def reflection_scope
37
+ reflection.scope
38
+ end
39
+
40
+ def finded(obj)
41
+ obj.find( owner.read_attribute(foreign_key) )
42
+ end
43
+
44
+ def foreign_key
45
+ reflection.foreign_key.to_sym
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,34 @@
1
+ module RailsOptimizer
2
+ class Reflection
3
+ attr_accessor :owner, :name, :foreign_key
4
+
5
+ def initialize(owner, name)
6
+ @owner = owner
7
+ @name = name
8
+ end
9
+
10
+ def klass
11
+ if reflection.polymorphic?
12
+ owner.read_attribute(foreign_type).constantize
13
+ else
14
+ reflection.klass
15
+ end
16
+ end
17
+
18
+ def scope
19
+ reflection.scope
20
+ end
21
+
22
+ def reflection
23
+ @reflection ||= owner._reflections[name]
24
+ end
25
+
26
+ def foreign_type
27
+ reflection.foreign_type
28
+ end
29
+
30
+ def foreign_key
31
+ reflection.foreign_key
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsOptimizer
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Facundo A. Díaz Martínez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-08 00:00:00.000000000 Z
11
+ date: 2021-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -57,6 +57,8 @@ files:
57
57
  - bin/console
58
58
  - bin/setup
59
59
  - lib/rails_optimizer.rb
60
+ - lib/rails_optimizer/belongs_to.rb
61
+ - lib/rails_optimizer/reflection.rb
60
62
  - lib/rails_optimizer/version.rb
61
63
  - rails_optimizer.gemspec
62
64
  homepage: https://github.com/ITSesocio/rails_optimizer