activeset 0.6.3 → 0.6.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f863b0e0436325554096d5b387ff337f833498f0
4
- data.tar.gz: 88b9619be39f0cf578f4d93e453a5dce53eebaab
3
+ metadata.gz: a825246f73039a0fc8fa629354cc0d803eee7398
4
+ data.tar.gz: d8fdbef2dcc49e136d4f5b4aec3f1b708cc6b026
5
5
  SHA512:
6
- metadata.gz: 5f1435bf42f919b9beb32c6f96359c2f0d3464ed9b0756450eefe8146cd7574b518196d86f486a8e8ec6165ee80e6562fa994873d568d4c89c0d821c7327cfde
7
- data.tar.gz: f5d14ae25144cb6145fbe9a790489573c76ab60863abb26b3cc4407f069f19f44fba0d49013a3b53699fed7d29862ea4e3867d919f29d2990c6d892bb2648af4
6
+ metadata.gz: 696bb548f4bfe7d44b425fbbf39ed6e522ee4806629f96b2ad5f0bcbe7d34b5c96af41cd15a56d6bff6dda3d9690eb424886ed4b6a5f66a3efa7849e1e600187
7
+ data.tar.gz: 98dafa877023ef43d2d939ad3e23102a29ac85dc340ca39ca0076e355d767422bcae8c9bd01f39964f899383ab9927b19aa552835592a89d49054b4926e91d6a
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ v 0.6.4
2
+ - Add multi-column sorting for ActiveRecord collections
3
+ - Fix the ActiveRecord `attribute_model` method to always return the class, and not an ActiveRecord::Relation object
1
4
  v 0.6.3
2
5
  - Fix a typo in the CSV transform adapter
3
6
  v 0.6.2
@@ -51,8 +51,8 @@ class ActiveSet
51
51
  .reduce(@set) do |obj, assoc|
52
52
  obj.reflections[assoc.to_s]&.klass
53
53
  end
54
- # return tmp_model.klass if tmp_model.is_a?(ActiveRecord::Relation)
55
- # tmp_model
54
+ return tmp_model.klass if tmp_model.is_a?(ActiveRecord::Relation)
55
+ tmp_model
56
56
  end
57
57
  end
58
58
  end
@@ -44,7 +44,9 @@ class ActiveSet
44
44
  def value_for(item:)
45
45
  resource_for(item: item).public_send(attribute)
46
46
  rescue
47
+ # :nocov:
47
48
  nil
49
+ # :nocov:
48
50
  end
49
51
 
50
52
  def resource_for(item:)
@@ -54,7 +56,9 @@ class ActiveSet
54
56
  resource.public_send(association)
55
57
  end
56
58
  rescue
59
+ # :nocov:
57
60
  nil
61
+ # :nocov:
58
62
  end
59
63
 
60
64
  private
@@ -17,13 +17,21 @@ class ActiveSet
17
17
  @instructions.each(&block)
18
18
  end
19
19
 
20
+ # :nocov:
20
21
  def method_missing(method_name, *args, &block)
21
22
  @instructions.send(method_name, *args, &block) || super
22
23
  end
24
+ # :nocov:
23
25
 
26
+ # :nocov:
24
27
  def respond_to_missing?(method_name, include_private = false)
25
28
  @instructions.respond_to?(method_name) || super
26
29
  end
30
+ # :nocov:
31
+
32
+ def associations_hash
33
+ @instructions.reduce({}) { |h, i| h.merge(i.associations_hash) }
34
+ end
27
35
 
28
36
  def get(key)
29
37
  @instructions.find do |instruction|
@@ -3,48 +3,25 @@
3
3
  require_relative '../adapter_activerecord'
4
4
  require_relative '../processor_base'
5
5
  require_relative '../../helpers/throws'
6
+ require_relative './active_record_operation'
6
7
 
7
8
  class ActiveSet
8
9
  class Processor::Sort < Processor::Base
9
10
  class ActiveRecordAdapter < Adapter::ActiveRecord
10
11
  def process
11
- @instructions.reduce(@set) do |set, _instruction|
12
- # set Adapter::Base#instruction, which many methods depend on
13
- self.instruction = _instruction
12
+ return false unless @set.respond_to?(:to_sql)
14
13
 
15
- return false unless @set.respond_to?(:to_sql)
16
- return false unless can_query_with_active_record?
14
+ @instructions.reduce(@set.eager_load(@instructions.associations_hash)) do |set, instruction|
15
+ ar_operation = ActiveRecordOperation.new(instruction, @set.klass)
16
+ return false unless ar_operation.attribute_is_field?
17
17
 
18
- statement = arel_eager_load_associations
19
- .merge(arel_operation)
18
+ statement = set.merge(ar_operation.operation)
20
19
 
21
20
  return false if throws?(ActiveRecord::StatementInvalid) { statement.load }
22
21
 
23
22
  statement
24
23
  end
25
24
  end
26
-
27
- private
28
-
29
- def arel_operation
30
- arel_direction = arel_direction(instruction.value)
31
-
32
- attribute_model.order(arel_column.send(arel_direction))
33
- end
34
-
35
- def arel_column
36
- case_insensitive? ? super.lower : super
37
- end
38
-
39
- def case_insensitive?
40
- instruction.operator.to_s.downcase == 'i'
41
- end
42
-
43
- def arel_direction(direction)
44
- return 'desc' if direction.to_s.downcase.start_with? 'desc'
45
-
46
- 'asc'
47
- end
48
25
  end
49
26
  end
50
27
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../adapter_activerecord'
4
+ require_relative '../processor_base'
5
+ require_relative '../../helpers/throws'
6
+
7
+ class ActiveSet
8
+ class Processor::Sort < Processor::Base
9
+ class ActiveRecordOperation
10
+ def initialize(instruction, klass)
11
+ @instruction = instruction
12
+ @klass = klass
13
+ end
14
+
15
+ def operation
16
+ attribute_model.order(column.send(validate_direction(@instruction.value)))
17
+ end
18
+
19
+ def attribute_is_field?
20
+ return false unless attribute_model
21
+
22
+ attribute_model.attribute_names
23
+ .include?(@instruction.attribute)
24
+ end
25
+
26
+ def case_insensitive?
27
+ @instruction.operator.to_s.downcase == 'i'
28
+ end
29
+
30
+ def column
31
+ column = table[@instruction.attribute]
32
+ case_insensitive? ? column.lower : column
33
+ end
34
+
35
+ def table
36
+ Arel::Table.new(attribute_model.table_name)
37
+ end
38
+
39
+ def validate_direction(direction)
40
+ return 'desc' if direction.to_s.downcase.start_with? 'desc'
41
+
42
+ 'asc'
43
+ end
44
+
45
+ def attribute_model
46
+ tmp_model = @instruction.associations_array
47
+ .reduce(@klass) do |obj, assoc|
48
+ obj.reflections[assoc.to_s]&.klass
49
+ end
50
+ # return tmp_model.klass if tmp_model.is_a?(ActiveRecord::Relation)
51
+ # tmp_model
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ActiveSet
4
- VERSION = '0.6.3'
4
+ VERSION = '0.6.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeset
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Margheim
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-29 00:00:00.000000000 Z
11
+ date: 2018-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -187,6 +187,7 @@ files:
187
187
  - lib/active_set/processor_paginate/enumerable_adapter.rb
188
188
  - lib/active_set/processor_sort.rb
189
189
  - lib/active_set/processor_sort/active_record_adapter.rb
190
+ - lib/active_set/processor_sort/active_record_operation.rb
190
191
  - lib/active_set/processor_sort/enumerable_adapter.rb
191
192
  - lib/active_set/processor_transform.rb
192
193
  - lib/active_set/processor_transform/csv_adapter.rb