magic_scopes 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/magic_scopes.rb CHANGED
@@ -6,11 +6,12 @@ require 'magic_scopes/scopes_generators/mixins/equality_scopes'
6
6
  require 'magic_scopes/scopes_generators/mixins/presence_scopes'
7
7
  require 'magic_scopes/scopes_generators/base'
8
8
  require 'magic_scopes/scopes_generators/boolean'
9
- require 'magic_scopes/scopes_generators/integer'
9
+ require 'magic_scopes/scopes_generators/numeric'
10
10
  require 'magic_scopes/scopes_generators/string'
11
11
  require 'magic_scopes/scopes_generators/float'
12
12
  require 'magic_scopes/scopes_generators/association'
13
13
  require 'magic_scopes/scopes_generators/state'
14
+ require 'magic_scopes/scopes_generators/compat'
14
15
  require 'magic_scopes/version'
15
16
  require 'magic_scopes/railtie' if defined?(Rails)
16
17
 
@@ -33,14 +33,19 @@ module MagicScopes
33
33
  end
34
34
 
35
35
  def generate_scopes
36
- @attributes.inject({}) { |hsh, attr| hsh[attr] = generate_scopes_for_attr(attr, @needed_scopes); hsh }.merge(
37
- @attributes_with_scopes.inject({}) { |hsh, (attr, attr_scopes)| hsh[attr] = generate_scopes_for_attr(attr, attr_scopes); hsh }
38
- )
36
+ generate_explicitly_passed_scopes.merge(generate_implicitly_specified_scopes)
39
37
  end
40
38
 
41
-
42
39
  private
43
40
 
41
+ def generate_implicitly_specified_scopes
42
+ @attributes.inject({}) { |hsh, attr| hsh[attr] = generate_scopes_for_attr(attr, @needed_scopes); hsh }
43
+ end
44
+
45
+ def generate_explicitly_passed_scopes
46
+ @attributes_with_scopes.inject({}) { |hsh, (attr, attr_scopes)| hsh[attr] = generate_scopes_for_attr(attr, attr_scopes); hsh }
47
+ end
48
+
44
49
  def generate_scopes_for_attr(attr, scopes)
45
50
  scopes.inject([]) do |ar, scope_info|
46
51
  scope_info = Array.wrap(scope_info)
@@ -51,7 +51,7 @@ module MagicScopes
51
51
  val
52
52
  elsif val.is_a?(ActiveRecord::Base)
53
53
  val.id
54
- elsif val.is_a?(Array) && val.all? { |v| v.is_a?(Fixnum) || (v.is_a?(String) && v.to_i != 0) || v.is_a?(ActiveRecord::Base) }
54
+ elsif val.is_a?(Array) && val.all? { |v| v.is_a?(Integer) || (v.is_a?(String) && v.to_i != 0) || v.is_a?(ActiveRecord::Base) }
55
55
  val.is_a?(ActiveRecord::Base) ? val.map(&:id) : val
56
56
  else
57
57
  raise ArgumentError, "Wrong argument type #{attr.class.name} for argument #{attr}"
@@ -13,12 +13,8 @@ module MagicScopes
13
13
  end
14
14
 
15
15
  def self.instance(model, attr)
16
- if @@instances[model.name].try(:has_key?, attr)
17
- @@instances[model.name][attr]
18
- else
19
- @@instances[model.name] ||= {}
20
- @@instances[model.name][attr] = new(model, attr)
21
- end
16
+ @@instances[model.name] ||= {}
17
+ @@instances[model.name][attr] ||= new(model, attr)
22
18
  end
23
19
 
24
20
  delegate :scope, :where, :order, to: :@model
@@ -0,0 +1,9 @@
1
+ module MagicScopes
2
+ TextScopesGenerator = StringScopesGenerator
3
+
4
+ IntegerScopesGenerator = NumericScopesGenerator
5
+ DecimalScopesGenerator = NumericScopesGenerator
6
+ TimeScopesGenerator = NumericScopesGenerator
7
+ DatetimeScopesGenerator = NumericScopesGenerator
8
+ DateScopesGenerator = NumericScopesGenerator
9
+ end
@@ -0,0 +1,17 @@
1
+ module MagicScopes
2
+ class NumericScopesGenerator < ScopesGenerator::Base
3
+
4
+ include EqualityScopes
5
+ include OrderScopes
6
+ include ComparisonScopes
7
+
8
+ def gte(name)
9
+ scope name || "#{@attr}_gte", ->(val) { where("#{@key} >= ?", val) }
10
+ end
11
+
12
+ def lte(name)
13
+ scope name || "#{@attr}_lte", ->(val) { where("#{@key} <= ?", val) }
14
+ end
15
+
16
+ end
17
+ end
@@ -16,12 +16,12 @@ if defined?(StateMachine)
16
16
  end
17
17
 
18
18
  def with(name)
19
- @model.instance_eval("undef :with_#{attr}")
19
+ @model.instance_eval("undef :with_#{attr}") unless name
20
20
  scope name || "with_#{@attr}", ->(*vals) { where(vals.empty? ? "#{@key} IS NOT NULL" : ["#{@key} IN (?)", vals]) }
21
21
  end
22
22
 
23
23
  def without(name)
24
- @model.instance_eval("undef :without_#{attr}")
24
+ @model.instance_eval("undef :without_#{attr}") unless name
25
25
  scope name || "without_#{@attr}", ->(*vals) { where(vals.empty? ? "#{@key} IS NULL" : ["#{@key} NOT IN (?)", vals]) }
26
26
  end
27
27
 
@@ -35,6 +35,4 @@ module MagicScopes
35
35
  @model.connection.adapter_name == 'PostgreSQL'
36
36
  end
37
37
  end
38
-
39
- TextScopesGenerator = StringScopesGenerator
40
38
  end
@@ -1,3 +1,3 @@
1
1
  module MagicScopes
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -135,12 +135,13 @@ files:
135
135
  - lib/magic_scopes/scopes_generators/association.rb
136
136
  - lib/magic_scopes/scopes_generators/base.rb
137
137
  - lib/magic_scopes/scopes_generators/boolean.rb
138
+ - lib/magic_scopes/scopes_generators/compat.rb
138
139
  - lib/magic_scopes/scopes_generators/float.rb
139
- - lib/magic_scopes/scopes_generators/integer.rb
140
140
  - lib/magic_scopes/scopes_generators/mixins/comparison_scopes.rb
141
141
  - lib/magic_scopes/scopes_generators/mixins/equality_scopes.rb
142
142
  - lib/magic_scopes/scopes_generators/mixins/order_scopes.rb
143
143
  - lib/magic_scopes/scopes_generators/mixins/presence_scopes.rb
144
+ - lib/magic_scopes/scopes_generators/numeric.rb
144
145
  - lib/magic_scopes/scopes_generators/state.rb
145
146
  - lib/magic_scopes/scopes_generators/string.rb
146
147
  - lib/magic_scopes/standard_scopes.rb
@@ -1,27 +0,0 @@
1
- module MagicScopes
2
- class IntegerScopesGenerator < ScopesGenerator::Base
3
-
4
- include EqualityScopes
5
- include OrderScopes
6
- include ComparisonScopes
7
-
8
- def gte(name)
9
- scope name || "#{@attr}_gte", ->(val) { where("#{@key} >= ?", val) }
10
- end
11
-
12
- def lte(name)
13
- scope name || "#{@attr}_lte", ->(val) { where("#{@key} <= ?", val) }
14
- end
15
-
16
- end
17
-
18
- # TODO
19
- # make something like:
20
- # %w(integer decimal time datetime date).each do |type|
21
- # send("#{type.classify}=", NumScopesGenerator)
22
- # end
23
- DecimalScopesGenerator = IntegerScopesGenerator
24
- TimeScopesGenerator = IntegerScopesGenerator
25
- DatetimeScopesGenerator = IntegerScopesGenerator
26
- DateScopesGenerator = IntegerScopesGenerator
27
- end