Module: ActiveRecord::QueryMethods

Defined in:
lib/postgres_with/active_record/relation/query_methods.rb

Defined Under Namespace

Classes: WithChain

Instance Method Summary collapse

Instance Method Details

#build_arel_with_extensions(aliases = nil) ⇒ Object Also known as: build_arel



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 78

def build_arel_with_extensions(aliases = nil)
  arel = build_arel_without_extensions(aliases)

  with_statements = with_values.flat_map do |with_value|
    case with_value
    when String
      with_value
    when Hash
      with_value.map do |name, expression|
        case expression
        when String
          select = Arel::Nodes::SqlLiteral.new "(#{expression})"
        when ActiveRecord::Relation, Arel::SelectManager
          select = Arel::Nodes::SqlLiteral.new "(#{expression.to_sql})"
        end
        if name.to_s.start_with?('_materialized_')
          name = name.gsub('_materialized_', '')
          Arel::Nodes::AsMaterialized.new Arel::Nodes::SqlLiteral.new("\"#{name}\""), select
        else
          Arel::Nodes::As.new Arel::Nodes::SqlLiteral.new("\"#{name}\""), select
        end
      end
    when Arel::Nodes::As
      with_value
    end
  end
  unless with_statements.empty?
    if recursive_value
      arel.with :recursive, with_statements
    else
      arel.with with_statements
    end
  end

  arel
end

#with(opts = :chain, *rest) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 59

def with(opts = :chain, *rest)
  if opts == :chain
    WithChain.new(spawn)
  elsif opts.blank?
    self
  else
    spawn.with!(opts, *rest)
  end
end

#with!(opts = :chain, *rest) ⇒ Object

:nodoc:



69
70
71
72
73
74
75
76
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 69

def with!(opts = :chain, *rest) # :nodoc:
  if opts == :chain
    WithChain.new(self)
  else
    self.with_values += [opts] + rest
    self
  end
end