active_record_extended 1.2.0 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +85 -7
  3. data/lib/active_record_extended/active_record.rb +2 -11
  4. data/lib/active_record_extended/active_record/relation_patch.rb +21 -4
  5. data/lib/active_record_extended/arel.rb +1 -0
  6. data/lib/active_record_extended/arel/nodes.rb +24 -21
  7. data/lib/active_record_extended/arel/predications.rb +3 -2
  8. data/lib/active_record_extended/arel/sql_literal.rb +16 -0
  9. data/lib/active_record_extended/arel/visitors/postgresql_decorator.rb +1 -1
  10. data/lib/active_record_extended/query_methods/any_of.rb +5 -4
  11. data/lib/active_record_extended/query_methods/either.rb +2 -1
  12. data/lib/active_record_extended/query_methods/inet.rb +6 -2
  13. data/lib/active_record_extended/query_methods/json.rb +14 -17
  14. data/lib/active_record_extended/query_methods/select.rb +13 -12
  15. data/lib/active_record_extended/query_methods/unionize.rb +13 -7
  16. data/lib/active_record_extended/query_methods/where_chain.rb +17 -8
  17. data/lib/active_record_extended/query_methods/window.rb +93 -0
  18. data/lib/active_record_extended/query_methods/with_cte.rb +104 -37
  19. data/lib/active_record_extended/utilities/order_by.rb +11 -30
  20. data/lib/active_record_extended/utilities/support.rb +21 -18
  21. data/lib/active_record_extended/version.rb +1 -1
  22. data/spec/query_methods/any_of_spec.rb +2 -2
  23. data/spec/query_methods/either_spec.rb +11 -0
  24. data/spec/query_methods/json_spec.rb +5 -5
  25. data/spec/query_methods/select_spec.rb +13 -13
  26. data/spec/query_methods/unionize_spec.rb +5 -5
  27. data/spec/query_methods/window_spec.rb +51 -0
  28. data/spec/query_methods/with_cte_spec.rb +12 -2
  29. data/spec/spec_helper.rb +1 -1
  30. data/spec/sql_inspections/any_of_sql_spec.rb +2 -2
  31. data/spec/sql_inspections/contains_sql_queries_spec.rb +8 -8
  32. data/spec/sql_inspections/either_sql_spec.rb +19 -3
  33. data/spec/sql_inspections/json_sql_spec.rb +7 -1
  34. data/spec/sql_inspections/unionize_sql_spec.rb +2 -2
  35. data/spec/sql_inspections/window_sql_spec.rb +98 -0
  36. data/spec/sql_inspections/with_cte_sql_spec.rb +30 -1
  37. data/spec/support/models.rb +18 -0
  38. metadata +23 -20
  39. data/lib/active_record_extended/patch/5_0/predicate_builder_decorator.rb +0 -87
  40. data/lib/active_record_extended/patch/5_0/regex_match.rb +0 -10
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Stripped from Rails 5.1.x
4
- # This patch is used so that when querying with hash elements that do not belong to an association,
5
- # but instead a data attribute. It returns the corrected attribute binds back to the query builder.
6
- #
7
- # Without joins
8
- # Before:
9
- # User.where.contains(data: { nickname: "george" })
10
- # #=> "SELECT \"people\".* FROM \"people\" WHERE (\"data\".\"nickname\" @> 'george')"
11
- #
12
- # After:
13
- # User.where.contains(data: { nickname: "george" })
14
- # #=> "SELECT \"people\".* FROM \"people\" WHERE (\"people\".\"data\" @> '\"nickname\"=>\"george\"')"
15
- #
16
- # With Joins
17
- # Before:
18
- # Tag.joins(:user).where.contains(people: { data: { nickname: "george" } })
19
- # #=> NoMethodError: undefined method `type' for nil:NilClass
20
- #
21
- # After:
22
- # Tag.joins(:user).where.contains(people: { data: { nickname: "george" } })
23
- # #=> "SELECT \"tags\".* FROM \"tags\" INNER JOIN \"people\" ON \"people\".\"id\" = \"tags\".\"person_id\"
24
- # WHERE (\"people\".\"data\" @> '\"nickname\"=>\"george\"')"
25
- #
26
- module ActiveRecord
27
- class TableMetadata
28
- def has_column?(column_name) # rubocop:disable Naming/PredicateName
29
- klass&.columns_hash&.key?(column_name.to_s)
30
- end
31
- end
32
-
33
- class PredicateBuilder
34
- def create_binds_for_hash(attributes) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize
35
- result = attributes.dup
36
- binds = []
37
-
38
- attributes.each do |column_name, value| # rubocop:disable Metrics/BlockLength
39
- if value.is_a?(Hash) && !table.has_column?(column_name)
40
- attrs, bvs = associated_predicate_builder(column_name).create_binds_for_hash(value)
41
- result[column_name] = attrs
42
- binds += bvs
43
- next
44
- elsif value.is_a?(Relation)
45
- binds += value.bound_attributes
46
- elsif value.is_a?(Range) && !table.type(column_name).respond_to?(:subtype)
47
- first = value.begin
48
- last = value.end
49
- unless first.respond_to?(:infinite?) && first.infinite?
50
- binds << build_bind_param(column_name, first)
51
- first = Arel::Nodes::BindParam.new
52
- end
53
- unless last.respond_to?(:infinite?) && last.infinite?
54
- binds << build_bind_param(column_name, last)
55
- last = Arel::Nodes::BindParam.new
56
- end
57
-
58
- result[column_name] = RangeHandler::RangeWithBinds.new(first, last, value.exclude_end?)
59
- elsif can_be_bound?(column_name, value)
60
- result[column_name] = Arel::Nodes::BindParam.new
61
- binds << build_bind_param(column_name, value)
62
- end
63
-
64
- # Find the foreign key when using queries such as:
65
- # Post.where(author: author)
66
- #
67
- # For polymorphic relationships, find the foreign key and type:
68
- # PriceEstimate.where(estimate_of: treasure)
69
- if table.associated_with?(column_name)
70
- result[column_name] = AssociationQueryHandler.value_for(table, column_name, value)
71
- end
72
- end
73
-
74
- [result, binds]
75
- end
76
-
77
- def can_be_bound?(column_name, value)
78
- return if table.associated_with?(column_name)
79
- case value
80
- when Array, Range
81
- table.type(column_name).respond_to?(:subtype)
82
- else
83
- !value.nil? && handler_for(value).is_a?(BasicObjectHandler)
84
- end
85
- end
86
- end
87
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Regexp
4
- # Stripped from ActiveSupport v5.1
5
- unless //.respond_to?(:match?)
6
- def match?(string, pos = 0)
7
- !(!match(string, pos))
8
- end
9
- end
10
- end