activerecord-autoscope 0.2.1 → 0.3.0

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
  SHA256:
3
- metadata.gz: 6ebf49c0e9411d8e3276fc893fe9ed8719f6ab1acda91d63657008799be67dd7
4
- data.tar.gz: 937fe74e98aa59b7e048dde8df4dedcf66c8f713f460ef9750c092b1bb997397
3
+ metadata.gz: 1a1b8d23ca764a24f3d6a8c9b050d494ec96c6adf7ffd21ce4f85e982a0ac4fc
4
+ data.tar.gz: a7ab35b21e0c3ac910dd9171afd9eb616bf9fcbc4807d62dfb2584c3df37f3f3
5
5
  SHA512:
6
- metadata.gz: 805a2109457614814a9e7740a521b67fd8c6dd0515329fdfc850849611575763655e892013538496805eaa7e6efe851ca052949123edc987132649c52cb0fe02
7
- data.tar.gz: 9bee74f15a103f81c766e7a87e8d90982ca317e7eb890c4ab502a909268aefbc2aa0f318192971bc1efab07ff1a31088a2af111a48d36e0809b33025fb78aebc
6
+ metadata.gz: b5521ec5915dad75454d9a429a6e2c6f89b03d595f6dd449533cdc4c7d81db44dbbc8558ec8c73b7c34cbd496da9377e00df85f6cdd37545180dd4c14db2fd50
7
+ data.tar.gz: 5b0e30d55dfff96c32399018e0c8de151d439e47d641cfb1dd9ec6ed74a0e60d9c179eb7f2b531b997c5025982177b3387771850db9b6ce552bd0fcc00352d5d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-autoscope (0.2.1)
4
+ activerecord-autoscope (0.3.0)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -58,7 +58,7 @@ Please check its [implementation](https://github.com/yhirano55/activerecord-auto
58
58
 
59
59
  ### Manually settings
60
60
 
61
- If you want to define these scope methods manually, you have to add this initializer file:
61
+ This gem provides automatically each scopes with `method_missing` If you don't any configuration, you can use it. But if you want to define these scope methods manually, you have to add this initializer file:
62
62
 
63
63
  ```ruby
64
64
  # config/initializers/activerecord_autoscope.rb
@@ -75,4 +75,4 @@ end
75
75
 
76
76
  ## License
77
77
 
78
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+ [MIT License](https://opensource.org/licenses/MIT)
@@ -3,6 +3,7 @@
3
3
  require 'active_record'
4
4
 
5
5
  require_relative 'auto_scope/config'
6
+ require_relative 'auto_scope/hook_methods'
6
7
  require_relative 'auto_scope/scope_methods'
7
8
  require_relative 'auto_scope/version'
8
9
 
@@ -15,14 +16,6 @@ end
15
16
 
16
17
  module ActiveRecord
17
18
  module AutoScope # :nodoc:
18
- def self.tracer
19
- @tracer ||= TracePoint.trace(:end) do |event|
20
- next unless event.self.respond_to?(:enable_auto_scopes!)
21
-
22
- event.self.enable_auto_scopes!
23
- end
24
- end
25
-
26
19
  def self.configure
27
20
  yield config
28
21
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module AutoScope
5
+ module HookMethods # :nodoc:
6
+ def self.prepended(child)
7
+ child.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods # :nodoc:
11
+ private
12
+
13
+ def method_missing(method_name, *args, &block)
14
+ if enable_auto_define_scopes? && enable_auto_scopes!
15
+ public_send(method_name, *args, &block)
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ def respond_to_missing?(method_name, include_private = false)
22
+ super
23
+ end
24
+
25
+ def enable_auto_define_scopes?
26
+ ::ActiveRecord::AutoScope.config.auto_define_scopes && !abstract_class?
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,20 +3,12 @@
3
3
  module ActiveRecord
4
4
  module AutoScope
5
5
  class Railtie < ::Rails::Railtie # :nodoc:
6
- config.before_initialize do
6
+ config.after_initialize do
7
7
  ActiveSupport.on_load :active_record do
8
8
  ActiveRecord::Base.extend ::ActiveRecord::AutoScope::ScopeMethods
9
+ ActiveRecord::Base.prepend ::ActiveRecord::AutoScope::HookMethods
9
10
  end
10
11
  end
11
-
12
- config.to_prepare do
13
- ::ActiveRecord::AutoScope.tracer.enable if ::ActiveRecord::AutoScope.config.auto_define_scopes
14
- end
15
-
16
- # TODO: Disable trace point on development and test
17
- config.after_initialize do
18
- ::ActiveRecord::AutoScope.tracer.disable if ::ActiveRecord::AutoScope.config.auto_define_scopes && ::Rails.application.config.eager_load
19
- end
20
12
  end
21
13
  end
22
14
  end
@@ -6,10 +6,18 @@ module ActiveRecord
6
6
  def enable_auto_scopes!
7
7
  return if abstract_class?
8
8
 
9
+ already_defined_scope = false
10
+
9
11
  columns.each do |column|
10
12
  attr_name = column.name
11
13
  table_name = column.table_name
12
14
 
15
+ # NOTE: if it has already defined methods, return false
16
+ if respond_to?("#{attr_name}_eq") && respond_to?("#{attr_name}_not_eq")
17
+ already_defined_scope = true
18
+ break
19
+ end
20
+
13
21
  scope("#{attr_name}_eq", ->(value) { where(attr_name => value) })
14
22
  scope("#{attr_name}_not_eq", ->(value) { where.not(attr_name => value) })
15
23
  scope("#{attr_name}_is", ->(value) { where(attr_name => value) })
@@ -45,6 +53,8 @@ module ActiveRecord
45
53
  scope("not_#{attr_name}", -> { where(attr_name => false) })
46
54
  end
47
55
  end
56
+
57
+ !already_defined_scope
48
58
  end
49
59
  end
50
60
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module AutoScope
5
- VERSION = '0.2.1'.freeze
5
+ VERSION = '0.3.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-autoscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiyuki Hirano
@@ -88,6 +88,7 @@ files:
88
88
  - lib/activerecord-autoscope.rb
89
89
  - lib/activerecord/auto_scope.rb
90
90
  - lib/activerecord/auto_scope/config.rb
91
+ - lib/activerecord/auto_scope/hook_methods.rb
91
92
  - lib/activerecord/auto_scope/railtie.rb
92
93
  - lib/activerecord/auto_scope/scope_methods.rb
93
94
  - lib/activerecord/auto_scope/version.rb