rails_optimizer 0.1.8 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +1 -1
- data/lib/rails_optimizer.rb +7 -18
- data/lib/rails_optimizer/association.rb +28 -0
- data/lib/rails_optimizer/belongs_to.rb +13 -28
- data/lib/rails_optimizer/has_one.rb +29 -0
- data/lib/rails_optimizer/version.rb +1 -1
- data/rails_optimizer-0.2.2.gem +0 -0
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 13f81883ca5abcc97b363818ed7d08a73a3d69fd3679f6741b62ab09fdd08c71
|
4
|
+
data.tar.gz: 38698dcc08709b8b791e284b57b7ca03e2ec282a0e74ea997c7d40e365dbb185
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27a4f2e4d60ee240dca864c1ffc7d5eadc9bb60b292e2cd10d1780c0ca890f949d773228c5fc6b51597b78db70a485b12984acf6c5c7ab1298137669f2459fa3
|
7
|
+
data.tar.gz: bd659a74fe311cc0bedf2e0b2d818ee0ac0cc4cc5628beb226d566eb9228156e34cd728dddc0cd71ae7807074536f7d996ac8415da4928147bf118fb612a2afb
|
data/Gemfile.lock
CHANGED
data/lib/rails_optimizer.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require "rails_optimizer/version"
|
2
|
+
require "rails_optimizer/association"
|
2
3
|
require "rails_optimizer/belongs_to"
|
4
|
+
require "rails_optimizer/has_one"
|
3
5
|
require "rails_optimizer/reflection"
|
4
6
|
|
5
7
|
module RailsOptimizer
|
@@ -7,24 +9,10 @@ module RailsOptimizer
|
|
7
9
|
extend ActiveSupport::Concern
|
8
10
|
|
9
11
|
included do
|
10
|
-
|
11
|
-
if has_changes_to_save?
|
12
|
-
super
|
13
|
-
else
|
14
|
-
true
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
12
|
+
|
18
13
|
def self.has_one(name, scope = nil, **options)
|
19
14
|
super
|
20
|
-
|
21
|
-
fk = self.class.foreign_key(name).to_sym
|
22
|
-
if args.empty?
|
23
|
-
name.to_s.classify.constantize.select("*").scoped(scope).find_by(fk => id)
|
24
|
-
else
|
25
|
-
name.to_s.classify.constantize.select(:id, *args).scoped(scope).find_by(fk => id)
|
26
|
-
end
|
27
|
-
end
|
15
|
+
RailsOptimizer::HasOne._define(self,name)
|
28
16
|
end
|
29
17
|
|
30
18
|
def self.belongs_to(name, scope = nil, **options)
|
@@ -32,8 +20,9 @@ module RailsOptimizer
|
|
32
20
|
RailsOptimizer::BelongsTo._define(self, name)
|
33
21
|
end
|
34
22
|
|
35
|
-
def self.execute(&
|
36
|
-
|
23
|
+
def self.execute(&method)
|
24
|
+
return all unless block_given?
|
25
|
+
class_eval(&method)
|
37
26
|
end
|
38
27
|
|
39
28
|
end
|
@@ -0,0 +1,28 @@
|
|
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
|
+
|
23
|
+
def get_target
|
24
|
+
return owner.association(name.to_sym).target if owner.association(name.to_sym).loaded?
|
25
|
+
yield if block_given?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,12 +1,5 @@
|
|
1
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
|
2
|
+
class BelongsTo < Association
|
10
3
|
|
11
4
|
def self._define(owner, name )
|
12
5
|
owner.__send__(:define_method, name) do |*args|
|
@@ -15,30 +8,22 @@ module RailsOptimizer
|
|
15
8
|
end
|
16
9
|
|
17
10
|
def get_target
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
11
|
+
super do
|
12
|
+
if args.empty?
|
13
|
+
klass
|
14
|
+
else
|
15
|
+
klass.select(*args)
|
16
|
+
end
|
17
|
+
.execute(&reflection_scope)
|
18
|
+
.execute(&finded)
|
22
19
|
end
|
23
|
-
.execute{reflection_scope}
|
24
|
-
.execute{ |obj| finded(obj)}
|
25
20
|
end
|
26
21
|
|
27
22
|
private
|
28
|
-
|
29
|
-
|
30
|
-
|
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) )
|
23
|
+
|
24
|
+
def finded
|
25
|
+
id = owner.read_attribute(foreign_key)
|
26
|
+
proc { send(:find_by, {id: id}) }
|
42
27
|
end
|
43
28
|
|
44
29
|
def foreign_key
|
@@ -0,0 +1,29 @@
|
|
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
|
+
super do
|
13
|
+
if args.empty?
|
14
|
+
klass
|
15
|
+
else
|
16
|
+
klass.select(*args)
|
17
|
+
end.execute(&reflection_scope).execute(&finded)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def finded
|
24
|
+
finder = {reflection.foreign_key.to_sym => owner.id}
|
25
|
+
proc { send(:find_by, **finder) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
Binary file
|
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.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Facundo A. Díaz Martínez
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -57,9 +57,12 @@ files:
|
|
57
57
|
- bin/console
|
58
58
|
- bin/setup
|
59
59
|
- lib/rails_optimizer.rb
|
60
|
+
- lib/rails_optimizer/association.rb
|
60
61
|
- lib/rails_optimizer/belongs_to.rb
|
62
|
+
- lib/rails_optimizer/has_one.rb
|
61
63
|
- lib/rails_optimizer/reflection.rb
|
62
64
|
- lib/rails_optimizer/version.rb
|
65
|
+
- rails_optimizer-0.2.2.gem
|
63
66
|
- rails_optimizer.gemspec
|
64
67
|
homepage: https://github.com/ITSesocio/rails_optimizer
|
65
68
|
licenses:
|
@@ -68,7 +71,7 @@ metadata:
|
|
68
71
|
allowed_push_host: https://rubygems.org
|
69
72
|
homepage_uri: https://github.com/ITSesocio/rails_optimizer
|
70
73
|
source_code_uri: https://github.com/ITSesocio/rails_optimizer
|
71
|
-
post_install_message:
|
74
|
+
post_install_message:
|
72
75
|
rdoc_options: []
|
73
76
|
require_paths:
|
74
77
|
- lib
|
@@ -83,9 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
86
|
- !ruby/object:Gem::Version
|
84
87
|
version: '0'
|
85
88
|
requirements: []
|
86
|
-
|
87
|
-
|
88
|
-
signing_key:
|
89
|
+
rubygems_version: 3.0.3
|
90
|
+
signing_key:
|
89
91
|
specification_version: 4
|
90
92
|
summary: Optimze some rails methods
|
91
93
|
test_files: []
|