Module: ActiveRecordExtended::QueryMethods::WithCTE
- Defined in:
- lib/active_record_extended/query_methods/with_cte.rb
Defined Under Namespace
Classes: WithChain
Instance Method Summary collapse
- #build_arel(*aliases) ⇒ Object
- #build_with(arel) ⇒ Object
- #build_with_hashed_value(with_value) ⇒ Object
- #recursive_value ⇒ Object (also: #recursive_value?)
- #recursive_value=(value) ⇒ Object
- #with(opts = :chain, *rest) ⇒ Object
- #with!(opts = :chain, *rest) ⇒ Object
- #with_values ⇒ Object
- #with_values=(values) ⇒ Object
- #with_values? ⇒ Boolean
Instance Method Details
#build_arel(*aliases) ⇒ Object
62 63 64 65 66 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 62 def build_arel(*aliases) super.tap do |arel| build_with(arel) if with_values? end end |
#build_with(arel) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 82 def build_with(arel) with_statements = with_values.flat_map do |with_value| case with_value when String, Arel::Nodes::As with_value when Hash build_with_hashed_value(with_value) end end.compact return if with_statements.empty? recursive_value? ? arel.with(:recursive, with_statements) : arel.with(with_statements) end |
#build_with_hashed_value(with_value) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 68 def build_with_hashed_value(with_value) with_value.map do |name, expression| select = case expression when String Arel::Nodes::SqlLiteral.new("(#{expression})") when ActiveRecord::Relation, Arel::SelectManager Arel::Nodes::SqlLiteral.new("(#{expression.to_sql})") end next if select.nil? Arel::Nodes::As.new(Arel::Nodes::SqlLiteral.new(PG::Connection.quote_ident(name.to_s)), select) end end |
#recursive_value ⇒ Object Also known as: recursive_value?
46 47 48 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 46 def recursive_value @values[:recursive] end |
#recursive_value=(value) ⇒ Object
41 42 43 44 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 41 def recursive_value=(value) raise ImmutableRelation if @loaded @values[:recursive] = value end |
#with(opts = :chain, *rest) ⇒ Object
51 52 53 54 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 51 def with(opts = :chain, *rest) return WithChain.new(spawn) if opts == :chain opts.blank? ? self : spawn.with!(opts, *rest) end |
#with!(opts = :chain, *rest) ⇒ Object
56 57 58 59 60 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 56 def with!(opts = :chain, *rest) return WithChain.new(self) if opts == :chain self.with_values += [opts] + rest self end |
#with_values ⇒ Object
29 30 31 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 29 def with_values @values[:with] || [] end |
#with_values=(values) ⇒ Object
37 38 39 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 37 def with_values=(values) @values[:with] = values end |
#with_values? ⇒ Boolean
33 34 35 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 33 def with_values? !(@values[:with].nil? || @values[:with].empty?) end |