lib/arel/extensions/insert_statement.rb in arel_toolkit-0.3.0 vs lib/arel/extensions/insert_statement.rb in arel_toolkit-0.4.0

- old
+ new

@@ -1,23 +1,26 @@ # rubocop:disable Naming/MethodName # rubocop:disable Naming/UncommunicativeMethodParamName module Arel module Nodes - # https://www.postgresql.org/docs/9.5/sql-insert.html - Arel::Nodes::InsertStatement.class_eval do - attr_accessor :with - attr_accessor :conflict - attr_accessor :override - attr_accessor :returning + class InsertStatement + # https://www.postgresql.org/docs/9.5/sql-insert.html + module InsertStatementExtensions + attr_accessor :with + attr_accessor :conflict + attr_accessor :override + attr_accessor :returning - alias_method :old_initialize, :initialize - def initialize - old_initialize + def initialize + super - @returning = [] + @returning = [] + end end + + prepend(InsertStatementExtensions) end end module Visitors class ToSql @@ -55,20 +58,36 @@ maybe_visit o.select, collector else collector end + visit(o.conflict, collector) if o.conflict + unless o.returning.empty? collector << ' RETURNING ' collector = inject_join o.returning, collector, ', ' end - visit(o.conflict, collector) if o.conflict collector end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/PerceivedComplexity + end + + class Dot + module InsertStatementExtension + def visit_Arel_Nodes_InsertStatement(o) + super + + visit_edge o, 'with' + visit_edge o, 'conflict' + visit_edge o, 'override' + visit_edge o, 'returning' + end + end + + prepend(InsertStatementExtension) end end end # rubocop:enable Naming/MethodName