rails_optimizer 0.1.7 → 0.2.2

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
- SHA1:
3
- metadata.gz: 0dc3f68cf2eb84764efca80777738c551537ee33
4
- data.tar.gz: beb196f15f49ee0c8c48d37e6950c490ed4fcbbd
2
+ SHA256:
3
+ metadata.gz: 3a7633b51e3c67f1fc7db25cc71c81c8063e6508d9b037439ec489c644913911
4
+ data.tar.gz: 106cbb41b697e40dea8ad8e5198b14b9649faa88bb747f1fd39c956414679706
5
5
  SHA512:
6
- metadata.gz: a35ebf74d2a112055ec3b33b68411a6ff714710b85c44add6d1851980e17346fb2795cb7e94e42f03bb09fd2fc0786122fd2a57454e0282fa40756bdf55b2e43
7
- data.tar.gz: 1850a36c5e5532467bc65da93beae6bdb6deccb66c94aeb7d3ac6e215d7914f7c0ce44b98726557e902b96dce0aba391fd93190becdb6958a98b2b3bf657bb39
6
+ metadata.gz: 9e4e5de83c7051397292493d446eb3dc0f85f475d86aa9b5798d20d55c04a12cc30c4fa0dcea4c5efa52b3d6820ae0fe73b4512a5e2b311e911d52f4d8a97aac
7
+ data.tar.gz: 9150b3e18ed64ad38643c219c1d53f36f93737c020bd6fe3fd35d98d50664b2898b15e5436d7acf43604a5915d856e55c67c6abefd1174037eddbfa8041c7364
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_optimizer (0.1.6)
4
+ rails_optimizer (0.2.1)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -1,67 +1,29 @@
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
5
9
  extend ActiveSupport::Concern
6
10
 
7
11
  included do
8
- def save
9
- if has_changes_to_save?
10
- super
11
- else
12
- true
13
- end
14
- end
15
-
12
+
16
13
  def self.has_one(name, scope = nil, **options)
17
14
  super
18
- define_method name.to_s do |*args|
19
- fk = self.class.foreign_key(name).to_sym
20
- if args.empty?
21
- name.to_s.classify.constantize.select("*").scoped(scope).find_by(fk => id)
22
- else
23
- name.to_s.classify.constantize.select(:id, *args).scoped(scope).find_by(fk => id)
24
- end
25
- end
15
+ RailsOptimizer::HasOne._define(self,name)
26
16
  end
27
17
 
28
18
  def self.belongs_to(name, scope = nil, **options)
29
19
  super
30
- _define(name, scope, options)
20
+ RailsOptimizer::BelongsTo._define(self, name)
31
21
  end
32
22
 
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
23
+ def self.execute(&method)
24
+ return all unless block_given?
25
+ class_eval(&method)
26
+ end
65
27
 
66
28
  end
67
29
  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
@@ -0,0 +1,34 @@
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
+ super do
12
+ if args.empty?
13
+ klass
14
+ else
15
+ klass.select(*args)
16
+ end
17
+ .execute(&reflection_scope)
18
+ .execute(&finded)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def finded
25
+ id = owner.read_attribute(foreign_key)
26
+ proc { send(:find_by, {id: id}) }
27
+ end
28
+
29
+ def foreign_key
30
+ reflection.foreign_key.to_sym
31
+ end
32
+
33
+ end
34
+ end
@@ -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
@@ -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.7"
2
+ VERSION = "0.2.2"
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.7
4
+ version: 0.2.2
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-01-08 00:00:00.000000000 Z
11
+ date: 2021-03-25 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
@@ -66,7 +70,7 @@ metadata:
66
70
  allowed_push_host: https://rubygems.org
67
71
  homepage_uri: https://github.com/ITSesocio/rails_optimizer
68
72
  source_code_uri: https://github.com/ITSesocio/rails_optimizer
69
- post_install_message:
73
+ post_install_message:
70
74
  rdoc_options: []
71
75
  require_paths:
72
76
  - lib
@@ -81,9 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
85
  - !ruby/object:Gem::Version
82
86
  version: '0'
83
87
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.6.14.1
86
- signing_key:
88
+ rubygems_version: 3.0.3
89
+ signing_key:
87
90
  specification_version: 4
88
91
  summary: Optimze some rails methods
89
92
  test_files: []