rails_optimizer 0.1.4 → 0.1.9

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: db7b90fbb9525326b32fbd31cf0a6e441e358ab6
4
- data.tar.gz: 59e7600fd887d47e4ed0f26c469c7fcc6a0a56d4
3
+ metadata.gz: db80c5f0e72c9cb288873b9f318e5dc3baafdd67
4
+ data.tar.gz: 0a101ae1ad6cb41b9243aebbd8760ed61c74ce7b
5
5
  SHA512:
6
- metadata.gz: 19c63413174c06fbb9e8687e0fcf449fe03e95b29a2d6bbeaccde376f0c84d55ffb4d3041dfd68448151a8444e242d1018032a447fafd82f6de1e3301d5aaf56
7
- data.tar.gz: 4db6afa31f33630649aea0db69dbae324ed0bbf03531e2d52e923a9585a6f7235ae244c7baabf321826e1e078644e13107971f04447cb69a6cae00bd3d2f6746
6
+ metadata.gz: 76a6df566e037c10afd6c2b79567ae9bb1fcebbc440a3cbad10ea66cf5cb0512faf3f53ba1aab51049736c6af69588eaf2646400f3ebaf9cb10bb01d850690b4
7
+ data.tar.gz: 8fec9fdae029505fdfd1d4814ea618a751649d69f643619310236704df2081a668ad809bcce459c5f8c1be88419993e5fc10910df63364e67915da800b05c408
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_optimizer (0.1.3)
4
+ rails_optimizer (0.1.8)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -1,4 +1,8 @@
1
1
  require "rails_optimizer/version"
2
+ require "rails_optimizer/association"
3
+ require "rails_optimizer/belongs_to"
4
+ require "rails_optimizer/has_one"
5
+ require "rails_optimizer/reflection"
2
6
 
3
7
  module RailsOptimizer
4
8
  class Error < StandardError; end
@@ -13,46 +17,20 @@ module RailsOptimizer
13
17
  end
14
18
  end
15
19
 
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
20
+ def self.has_one(name, scope = nil, **options)
21
+ super
22
+ RailsOptimizer::HasOne._define(self,name)
28
23
  end
29
24
 
30
25
  def self.belongs_to(name, scope = nil, **options)
31
26
  super
32
- define_method name.to_s do |*args|
33
- klass = if options[:polymorphic]
34
- read_attribute("#{name}_type".to_sym).classify
35
- else
36
- name.to_s.classify
37
- end
38
- if args.empty?
39
- klass.select('*').scoped(scope).finded(self, name)
40
- else
41
- klass.constantize.select(*args).scoped(scope).finded(self, name)
42
- end
43
- end
27
+ RailsOptimizer::BelongsTo._define(self, name)
44
28
  end
45
29
 
46
- def self.has_one(name, scope = nil, **options)
47
- super
48
- define_method name.to_s do |*args|
49
- fk = self.class.foreign_key(name).to_sym
50
- if args.empty?
51
- name.to_s.classify.constantize.select("*").scoped(scope).find_by(fk => id)
52
- else
53
- name.to_s.classify.constantize.select(:id, *args).scoped(scope).find_by(fk => id)
54
- end
55
- end
30
+ def self.execute(&method)
31
+ return all unless block_given?
32
+ class_eval(&method)
56
33
  end
34
+
57
35
  end
58
36
  end
@@ -0,0 +1,23 @@
1
+ module RailsOptimizer
2
+ class Association
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 reflection
12
+ @reflection ||= RailsOptimizer::Reflection.new(owner, name)
13
+ end
14
+
15
+ def klass
16
+ reflection.klass
17
+ end
18
+
19
+ def reflection_scope
20
+ reflection.scope
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module RailsOptimizer
2
+ class BelongsTo < Association
3
+
4
+ def self._define(owner, name )
5
+ owner.__send__(:define_method, name) do |*args|
6
+ return RailsOptimizer::BelongsTo.new(self, name, *args).get_target
7
+ end
8
+ end
9
+
10
+ def get_target
11
+ if args.empty?
12
+ klass
13
+ else
14
+ klass.select(*args)
15
+ end
16
+ .execute(&reflection_scope)
17
+ .execute(&finded)
18
+ end
19
+
20
+ private
21
+
22
+ def finded
23
+ id = owner.read_attribute(foreign_key)
24
+ proc { send(:find, id) }
25
+ end
26
+
27
+ def foreign_key
28
+ reflection.foreign_key.to_sym
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ module RailsOptimizer
2
+ class HasOne < Association
3
+
4
+
5
+ def self._define(owner, name )
6
+ owner.__send__(:define_method, name) do |*args|
7
+ return RailsOptimizer::HasOne.new(self, name, *args).get_target
8
+ end
9
+ end
10
+
11
+ def get_target
12
+ if args.empty?
13
+ klass
14
+ else
15
+ klass.select(*args)
16
+ end.execute(&reflection_scope).execute(&finded)
17
+ end
18
+
19
+ private
20
+
21
+ def finded
22
+ finder = {reflection.foreign_key.to_sym => owner.id}
23
+ proc { send(:find_by, **finder) }
24
+ end
25
+ end
26
+
27
+ define_method name.to_s do |*args|
28
+ fk = self.class.foreign_key(name).to_sym
29
+ if args.empty?
30
+ name.to_s.classify.constantize.select("*").scoped(scope).find_by(fk => id)
31
+ else
32
+ name.to_s.classify.constantize.select(:id, *args).scoped(scope).find_by(fk => id)
33
+ end
34
+ end
35
+ 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.4"
2
+ VERSION = "0.1.9"
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.4
4
+ version: 0.1.9
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-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -57,6 +57,10 @@ files:
57
57
  - bin/console
58
58
  - bin/setup
59
59
  - lib/rails_optimizer.rb
60
+ - lib/rails_optimizer/association.rb
61
+ - lib/rails_optimizer/belongs_to.rb
62
+ - lib/rails_optimizer/has_one.rb
63
+ - lib/rails_optimizer/reflection.rb
60
64
  - lib/rails_optimizer/version.rb
61
65
  - rails_optimizer.gemspec
62
66
  homepage: https://github.com/ITSesocio/rails_optimizer