Module: Army::Negative::Quoting

Defined in:
lib/army-negative/quoting.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Called when this module is included in ActiveRecord::ConnectionAdapters::Quoting. Uses #alias_method_chain to replace the original #quote method with our #quote_with_negative_one variation.



11
12
13
14
15
# File 'lib/army-negative/quoting.rb', line 11

def self.included(klass)
  klass.instance_eval do
    alias_method_chain :quote, :negative_one
  end
end

Instance Method Details

#quote_with_negative_one(value, column = nil) ⇒ Object

Wraps the original #quote method, ensuring that “true” values get stored as -1 in the database where they’d normally have been stored as 1.



21
22
23
24
# File 'lib/army-negative/quoting.rb', line 21

def quote_with_negative_one(value, column = nil)
  result = quote_without_negative_one(value, column)
  (value.is_a?(TrueClass) and result == '1') ? '-1' : result
end