Module: ActiveRecord::QueryMethods
- Defined in:
- lib/postgres_ext/active_record/relation/query_methods.rb
Defined Under Namespace
Classes: WhereChain
Instance Method Summary collapse
- #build_arel_with_extensions ⇒ Object
- #build_rank(arel, rank_window_options) ⇒ Object
- #build_with(arel, withs) ⇒ Object
- #ranked(options = :order) ⇒ Object
- #ranked!(value) ⇒ Object
- #with(*args) ⇒ Object
- #with!(*args) ⇒ Object
Instance Method Details
#build_arel_with_extensions ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 115 def build_arel_with_extensions arel = build_arel_without_extensions build_with(arel, with_values) build_rank(arel, rank_value) if rank_value arel end |
#build_rank(arel, rank_window_options) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 145 def build_rank(arel, ) unless arel.projections.count == 1 && Arel::Nodes::Count === arel.projections.first rank_window = case when :order arel.orders when Symbol table[].asc when Hash .map { |field, dir| table[field].send(dir) } else Arel::Nodes::SqlLiteral.new "(#{rank_window_options})" end unless rank_window.blank? rank_node = Arel::Nodes::SqlLiteral.new 'rank()' window = Arel::Nodes::Window.new if String === rank_window window = window.frame rank_window else window = window.order(rank_window) end over_node = Arel::Nodes::Over.new rank_node, window arel.project(over_node) end end end |
#build_with(arel, withs) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 125 def build_with(arel, withs) with_statements = withs.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::SqlLiteral.new "(#{expression})" when ActiveRecord::Relation select = Arel::SqlLiteral.new "(#{expression.to_sql})" end as = Arel::Nodes::As.new Arel::SqlLiteral.new(name.to_s), select end end end arel.with with_statements unless with_statements.empty? end |
#ranked(options = :order) ⇒ Object
106 107 108 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 106 def ranked( = :order) spawn.ranked! end |
#ranked!(value) ⇒ Object
110 111 112 113 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 110 def ranked!(value) self.rank_value = value self end |
#with(*args) ⇒ Object
96 97 98 99 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 96 def with(*args) check_if_method_has_arguments!('with', args) spawn.with!(*args.compact.flatten) end |
#with!(*args) ⇒ Object
101 102 103 104 |
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 101 def with!(*args) self.with_values += args self end |