mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Factored active_record and active_support enhancements to separate modules.
This commit is contained in:
parent
bf2eaf30e3
commit
97d8bb71d7
5 changed files with 43 additions and 17 deletions
|
@ -7,4 +7,6 @@ require File.dirname(__FILE__) + "/ap/core_ext/string"
|
|||
require File.dirname(__FILE__) + "/ap/core_ext/kernel"
|
||||
require File.dirname(__FILE__) + "/ap/awesome_print"
|
||||
|
||||
require File.dirname(__FILE__) + "/ap/mixin/rails" if defined?(::Rails)
|
||||
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
|
||||
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)
|
||||
|
||||
|
|
18
lib/ap/mixin/rails.rb → lib/ap/mixin/active_record.rb
Executable file → Normal file
18
lib/ap/mixin/rails.rb → lib/ap/mixin/active_record.rb
Executable file → Normal file
|
@ -3,21 +3,19 @@
|
|||
# Awesome Print is freely distributable under the terms of MIT license.
|
||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||
#------------------------------------------------------------------------------
|
||||
module AwesomePrintRails
|
||||
module AwesomePrintActiveRecord
|
||||
|
||||
def self.included(base)
|
||||
base.alias_method_chain :printable, :rails
|
||||
base.alias_method_chain :printable, :active_record
|
||||
end
|
||||
|
||||
# Add ActiveRecord class names to the dispatcher pipeline.
|
||||
#------------------------------------------------------------------------------
|
||||
def printable_with_rails(object)
|
||||
printable = printable_without_rails(object)
|
||||
def printable_with_active_record(object)
|
||||
printable = printable_without_active_record(object)
|
||||
if printable == :self
|
||||
if object.is_a?(ActiveRecord::Base)
|
||||
printable = :active_record_instance
|
||||
elsif object.is_a?(ActiveSupport::TimeWithZone)
|
||||
printable = :active_support_time
|
||||
end
|
||||
elsif printable == :class && object.class.is_a?(ActiveRecord::Base.class)
|
||||
printable = :active_record_class
|
||||
|
@ -49,12 +47,6 @@ module AwesomePrintRails
|
|||
end
|
||||
end
|
||||
|
||||
# Format ActiveSupport::TimeWithZone as standard Time.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_active_support_time(object)
|
||||
awesome_self(object, :as => :time)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
AwesomePrint.send(:include, AwesomePrintRails)
|
||||
AwesomePrint.send(:include, AwesomePrintActiveRecord)
|
32
lib/ap/mixin/active_support.rb
Normal file
32
lib/ap/mixin/active_support.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Copyright (c) 2010 Michael Dvorkin
|
||||
#
|
||||
# Awesome Print is freely distributable under the terms of MIT license.
|
||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||
#------------------------------------------------------------------------------
|
||||
module AwesomePrintActiveSupport
|
||||
|
||||
def self.included(base)
|
||||
base.alias_method_chain :printable, :active_support
|
||||
end
|
||||
|
||||
# Add ActiveSupport class names to the dispatcher pipeline.
|
||||
#------------------------------------------------------------------------------
|
||||
def printable_with_active_support(object)
|
||||
printable = printable_without_active_support(object)
|
||||
if printable == :self
|
||||
if object.is_a?(ActiveSupport::TimeWithZone)
|
||||
printable = :active_support_time
|
||||
end
|
||||
end
|
||||
printable
|
||||
end
|
||||
|
||||
# Format ActiveSupport::TimeWithZone as standard Time.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_active_support_time(object)
|
||||
awesome_self(object, :as => :time)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
AwesomePrint.send(:include, AwesomePrintActiveSupport)
|
|
@ -1,6 +1,6 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
if defined?(::Rails)
|
||||
if defined?(::ActiveRecord)
|
||||
|
||||
# Create tableless ActiveRecord model.
|
||||
#------------------------------------------------------------------------------
|
||||
|
@ -20,7 +20,7 @@ if defined?(::Rails)
|
|||
column :created_at, :datetime
|
||||
end
|
||||
|
||||
describe "AwesomePrint/Rails" do
|
||||
describe "AwesomePrint/ActiveRecord" do
|
||||
before(:each) do
|
||||
stub_dotfile!
|
||||
end
|
|
@ -267,7 +267,7 @@ EOS
|
|||
#------------------------------------------------------------------------------
|
||||
describe "BigDecimal and Rational" do
|
||||
it "should present BigDecimal object as Float scalar" do
|
||||
big = BigDecimal("2010.04")
|
||||
big = BigDecimal("2010.4")
|
||||
big.ai(:plain => true).should == "2010.4"
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue