forked from fixin.me/fixin.me
Add Tag
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
module CoreExt::ActiveRecord::QueryMethods
|
||||
class SelectChain
|
||||
def initialize(scope)
|
||||
@scope = scope
|
||||
end
|
||||
|
||||
def lag(expr, offset: 1, default: nil)
|
||||
case expr
|
||||
when Hash
|
||||
when Symbol
|
||||
expr = {expr => "lag_#{expr}"}
|
||||
else
|
||||
raise ArgumentError, "Unsupported expression argument type: #{expr.class.name}."
|
||||
end
|
||||
|
||||
expr.each do |column, column_alias|
|
||||
@scope._select!(
|
||||
Arel::Nodes::NamedFunction.new(
|
||||
'LAG',
|
||||
[@scope.table[column], offset, Arel::Nodes.build_quoted(default)]
|
||||
).over().as(column_alias)
|
||||
)
|
||||
end
|
||||
|
||||
@scope
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(base)
|
||||
base.prepend SelectWithSelectChain
|
||||
end
|
||||
end
|
||||
|
||||
module SelectWithSelectChain
|
||||
def select(*fields)
|
||||
return super if !fields.empty? || block_given?
|
||||
ActiveRecord::Relation::SelectChain.new(spawn)
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,6 @@
|
||||
module CoreExt::ActiveRecord::Relation
|
||||
INVALID_METHODS_FOR_UPDATE_AND_DELETE_ALL = [:distinct]
|
||||
ActiveRecord::Relation.class_eval do
|
||||
remove_const :INVALID_METHODS_FOR_UPDATE_AND_DELETE_ALL
|
||||
INVALID_METHODS_FOR_UPDATE_AND_DELETE_ALL = [:distinct]
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user