scopiform 0.1.4 → 0.2.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: 1434c2a39865b2f08dcb918354dad1523cd53d9aa8363f4a699fbcb9ddec537c
4
- data.tar.gz: 7930cc5a93d5f71dfe6e3ced466b3d39b2d68730b65528e2630febbeb8b5388d
3
+ metadata.gz: 934151f9380f5ddc631f49bf933feb2a4763c899a47cae97e17dd682931074c2
4
+ data.tar.gz: 7b11d4ffe28fbc13613932b4b3aad56cd01e3ad76a18c3b90286e652ca80b847
5
5
  SHA512:
6
- metadata.gz: d67daa8c043e7edaaf459da62baa64108b2554f81867ca0a24d97d52816a2b887aaa93249def97b1650d102d98d42149414f5e6740c4687ac2762c96b0d0001c
7
- data.tar.gz: 8b6d543af8a98acf91c5f8d0c4de23a812c40b0304c755412427d860d4ab7ab9baabbe477ead4d43a16a3eca1c61107012ff9a963688b889d029d8e851302a19
6
+ metadata.gz: 28c1c3c6a4dadfa6a3a00271fce88c6471c770e0d8249db754f7720ec2f47af6418bf450fd8bf22a85d995190906cc0f833b310081dd9773f8af33fc17fd6e3d
7
+ data.tar.gz: 726a2fa46e53224a825d55a55d4995198a4458e4a3a12ce54d2d55278cc5a5e04ec8c825682bb6d228207d41810d4bf96d93bf773341e784fa24cf6de8fec637
data/README.md CHANGED
@@ -24,7 +24,7 @@ $ gem install scopiform
24
24
  ## Public ActiveRecord Methods
25
25
  :aliases_for
26
26
  :apply_filters
27
- :apply_orders
27
+ :apply_sorts
28
28
  :association
29
29
  :attribute_aliases_inverted
30
30
  :auto_scope_add
@@ -30,7 +30,7 @@ module Scopiform
30
30
  def setup_association_auto_scopes(association)
31
31
  auto_scope_add(
32
32
  association.name,
33
- Proc.new { |value, joins: nil|
33
+ proc { |value, joins: nil|
34
34
  is_root = joins.nil?
35
35
  joins = {} if is_root
36
36
 
@@ -47,13 +47,13 @@ module Scopiform
47
47
  argument_type: :hash
48
48
  )
49
49
 
50
- # Ordering
50
+ # Sorting
51
51
  auto_scope_add(
52
52
  association.name,
53
- Proc.new { |value| scopiform_joins(association.name).merge(association.klass.apply_orders(value)) },
54
- prefix: 'order_by_',
53
+ proc { |value| scopiform_joins(association.name).merge(association.klass.apply_sorts(value)) },
54
+ prefix: 'sort_by_',
55
55
  argument_type: :hash,
56
- type: :order
56
+ type: :sort
57
57
  )
58
58
  end
59
59
  end
@@ -21,24 +21,24 @@ module Scopiform
21
21
 
22
22
  auto_scope_add(
23
23
  name,
24
- Proc.new { |value| where(name_sym => value) },
24
+ proc { |value| where(name_sym => value) },
25
25
  suffix: '_is',
26
26
  argument_type: type
27
27
  )
28
28
  auto_scope_add(
29
29
  name,
30
- Proc.new { |value| where.not(name_sym => value) },
30
+ proc { |value| where.not(name_sym => value) },
31
31
  suffix: '_not',
32
32
  argument_type: type
33
33
  )
34
34
 
35
- # Ordering
35
+ # Sorting
36
36
  auto_scope_add(
37
37
  name,
38
38
  ->(value = :asc) { order(name_sym => value) },
39
- prefix: 'order_by_',
39
+ prefix: 'sort_by_',
40
40
  argument_type: :string,
41
- type: :order
41
+ type: :sort
42
42
  )
43
43
  end
44
44
  end
@@ -22,12 +22,10 @@ module Scopiform
22
22
  end
23
23
 
24
24
  def auto_scope_add(attribute, block, prefix: nil, suffix: nil, **options)
25
- scope_name = "#{prefix}#{attribute}#{suffix}".underscore
26
- scope_name_sym = scope_name.to_sym
27
- scope scope_name_sym, block
28
-
29
25
  scope_definition = auto_scope_add_definition(attribute, prefix: prefix, suffix: suffix, **options)
30
26
 
27
+ scope scope_definition.name, block
28
+
31
29
  aliases_for(attribute).each do |alias_name|
32
30
  auto_scope_for_alias(alias_name, scope_definition)
33
31
  end
@@ -41,8 +39,31 @@ module Scopiform
41
39
  end
42
40
  end
43
41
 
42
+ def enum(definitions)
43
+ super(definitions)
44
+
45
+ definitions.each_key { |name| update_scope_to_enum(name) }
46
+ end
47
+
44
48
  private
45
49
 
50
+ def update_scope_to_enum(name)
51
+ scopes = auto_scopes_by_attribute(name)
52
+
53
+ scopes.each do |scope|
54
+ if scope.options[:type].blank?
55
+ argument_type = scope.options[:argument_type]
56
+ scope.options[:argument_type] = argument_type.is_a?(Array) ? [:string] : :string if argument_type
57
+ scope.options[:type] = :enum
58
+ end
59
+
60
+ if scope.options[:remove_for_enum]
61
+ singleton_class.remove_method scope.name
62
+ @auto_scopes.delete(scope)
63
+ end
64
+ end
65
+ end
66
+
46
67
  def auto_scope_add_definition(attribute, **options)
47
68
  definition = ScopeDefinition.new(attribute, **options)
48
69
  @auto_scopes ||= []
@@ -52,10 +73,9 @@ module Scopiform
52
73
  end
53
74
 
54
75
  def auto_scope_for_alias(alias_name, scope_definition)
55
- scope_name = "#{scope_definition.prefix}#{scope_definition.attribute}#{scope_definition.suffix}".underscore
56
- alias_method_name = "#{scope_definition.prefix}#{alias_name}#{scope_definition.suffix}".underscore
57
- singleton_class.send(:alias_method, alias_method_name.to_sym, scope_name.to_sym)
58
- auto_scope_add_definition(alias_name, prefix: scope_definition.prefix, suffix: scope_definition.suffix, **scope_definition.options)
76
+ alias_scope_definition = scope_definition.dup
77
+ alias_scope_definition.attribute = alias_name
78
+ singleton_class.send(:alias_method, alias_scope_definition.name, scope_definition.name)
59
79
  end
60
80
  end
61
81
  end
@@ -11,8 +11,8 @@ module Scopiform
11
11
  filters_hash.keys.inject(injecting) { |out, filter_name| resolve_filter(out, filter_name, filters_hash[filter_name], joins) }
12
12
  end
13
13
 
14
- def apply_orders(orders_hash, injecting = all)
15
- orders_hash.keys.inject(injecting) { |out, order_name| resolve_order(out, order_name, orders_hash[order_name]) }
14
+ def apply_sorts(sorts_hash, injecting = all)
15
+ sorts_hash.keys.inject(injecting) { |out, sort_name| resolve_sort(out, sort_name, sorts_hash[sort_name]) }
16
16
  end
17
17
 
18
18
  private
@@ -31,9 +31,9 @@ module Scopiform
31
31
  out.send(filter_name, filter_argument, joins: joins)
32
32
  end
33
33
 
34
- def resolve_order(out, order_name, order_argument)
35
- method_name = "order_by_#{order_name}"
36
- out.send(method_name, order_argument)
34
+ def resolve_sort(out, sort_name, sort_argument)
35
+ method_name = "sort_by_#{sort_name}"
36
+ out.send(method_name, sort_argument)
37
37
  end
38
38
  end
39
39
  end
@@ -51,6 +51,10 @@ module Scopiform
51
51
  nil
52
52
  end
53
53
 
54
+ def enum_attribute?(name)
55
+ defined_enums.include? name.to_s
56
+ end
57
+
54
58
  protected
55
59
 
56
60
  def safe_columns
@@ -1,9 +1,9 @@
1
1
  module Scopiform
2
2
  module ReflectionPlugin
3
- def add_reflection(ar, name, reflection)
4
- super(ar, name, reflection)
3
+ def add_reflection(record, name, reflection)
4
+ super(record, name, reflection)
5
5
 
6
- ar.reflection_added(name, reflection) if ar.respond_to? :reflection_added
6
+ record.reflection_added(name, reflection) if record.respond_to? :reflection_added
7
7
  end
8
8
  end
9
9
  end
@@ -10,7 +10,18 @@ module Scopiform
10
10
  end
11
11
 
12
12
  def name
13
- "#{prefix}#{attribute}#{suffix}".underscore
13
+ name_for(attribute)
14
+ end
15
+
16
+ def name_for(attribute_name)
17
+ "#{prefix}#{attribute}#{suffix}".underscore.to_sym
18
+ end
19
+
20
+ def dup
21
+ duplicate = super
22
+ duplicate.options = options.dup
23
+
24
+ duplicate
14
25
  end
15
26
  end
16
27
  end
@@ -24,42 +24,42 @@ module Scopiform
24
24
 
25
25
  auto_scope_add(
26
26
  name,
27
- Proc.new { |value| where(name_sym => value) },
27
+ proc { |value| where(name_sym => value) },
28
28
  suffix: '_in',
29
29
  argument_type: [type]
30
30
  )
31
31
 
32
32
  auto_scope_add(
33
33
  name,
34
- Proc.new { |value| where.not(name_sym => value) },
34
+ proc { |value| where.not(name_sym => value) },
35
35
  suffix: '_not_in',
36
36
  argument_type: [type]
37
37
  )
38
38
 
39
39
  auto_scope_add(
40
40
  name,
41
- Proc.new { |value| where(arel_column.lt(value)) },
41
+ proc { |value| where(arel_column.lt(value)) },
42
42
  suffix: '_lt',
43
43
  argument_type: type
44
44
  )
45
45
 
46
46
  auto_scope_add(
47
47
  name,
48
- Proc.new { |value| where(arel_column.lteq(value)) },
48
+ proc { |value| where(arel_column.lteq(value)) },
49
49
  suffix: '_lte',
50
50
  argument_type: type
51
51
  )
52
52
 
53
53
  auto_scope_add(
54
54
  name,
55
- Proc.new { |value| where(arel_column.gt(value)) },
55
+ proc { |value| where(arel_column.gt(value)) },
56
56
  suffix: '_gt',
57
57
  argument_type: type
58
58
  )
59
59
 
60
60
  auto_scope_add(
61
61
  name,
62
- Proc.new { |value| where(arel_column.gteq(value)) },
62
+ proc { |value| where(arel_column.gteq(value)) },
63
63
  suffix: '_gte',
64
64
  argument_type: type
65
65
  )
@@ -15,7 +15,9 @@ module Scopiform
15
15
 
16
16
  def setup_string_and_number_auto_scopes
17
17
  string_numbers = Helpers::STRING_TYPES + Helpers::NUMBER_TYPES
18
- string_number_columns = safe_columns.select { |column| string_numbers.include? column.type }
18
+ string_number_columns = safe_columns.select do |column|
19
+ string_numbers.include?(column.type) && !enum_attribute?(column.name)
20
+ end
19
21
  string_number_columns.each do |column|
20
22
  name = column.name
21
23
  type = column.type
@@ -34,44 +36,50 @@ module Scopiform
34
36
 
35
37
  auto_scope_add(
36
38
  name,
37
- Proc.new { |value| where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
39
+ proc { |value| where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
38
40
  suffix: '_contains',
39
- type: type
41
+ argument_type: type,
42
+ remove_for_enum: true
40
43
  )
41
44
 
42
45
  auto_scope_add(
43
46
  name,
44
- Proc.new { |value| where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
47
+ proc { |value| where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
45
48
  suffix: '_not_contains',
46
- type: type
49
+ argument_type: type,
50
+ remove_for_enum: true
47
51
  )
48
52
 
49
53
  auto_scope_add(
50
54
  name,
51
- Proc.new { |value| where(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
55
+ proc { |value| where(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
52
56
  suffix: '_starts_with',
53
- type: type
57
+ argument_type: type,
58
+ remove_for_enum: true
54
59
  )
55
60
 
56
61
  auto_scope_add(
57
62
  name,
58
- Proc.new { |value| where.not(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
63
+ proc { |value| where.not(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
59
64
  suffix: '_not_starts_with',
60
- type: type
65
+ argument_type: type,
66
+ remove_for_enum: true
61
67
  )
62
68
 
63
69
  auto_scope_add(
64
70
  name,
65
- Proc.new { |value| where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}")) },
71
+ proc { |value| where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}")) },
66
72
  suffix: '_ends_with',
67
- type: type
73
+ argument_type: type,
74
+ remove_for_enum: true
68
75
  )
69
76
 
70
77
  auto_scope_add(
71
78
  name,
72
- Proc.new { |value| where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}")) },
79
+ proc { |value| where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}")) },
73
80
  suffix: '_not_ends_with',
74
- type: type
81
+ argument_type: type,
82
+ remove_for_enum: true
75
83
  )
76
84
  end
77
85
  end
@@ -1,3 +1,3 @@
1
1
  module Scopiform
2
- VERSION = '0.1.4'
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scopiform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jayce.pulsipher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-15 00:00:00.000000000 Z
11
+ date: 2020-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: warning
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - jayce.pulsipher@3-form.com