Skip to content

Commit

Permalink
Add tests for warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Jul 16, 2014
1 parent 0ac3a93 commit fd185a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/utusemi/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def utusemi!(obj = nil, options = {})
utusemi_values[:flag] = obj ? true : false
utusemi_values[:type] = obj.to_sym if obj.class.in? [Symbol, String]
utusemi_values[:options] = options
warning_checker unless Rails.env.production?
self
end

Expand All @@ -50,6 +51,13 @@ def unmapped_utusemi_column_name(column_name, index = nil)
def eigenclass
class << self; self; end
end

def warning_checker
utusemi_column_names.each do |new_column_name, origin_column_name|
return if new_column_name != origin_column_name
Rails.logger.warn "[Utusemi:WARNING] \"#{new_column_name}\" is duplicated in map(:#{utusemi_values[:type]})."
end
end
end

# 用途
Expand Down Expand Up @@ -78,12 +86,13 @@ def utusemi!(obj = nil, options = {})
end

def utusemi_columns_mapper
utusemi_column_names.keys.each do |column_name|
utusemi_column_names.each_pair do |new_column_name, origin_column_name|
next if new_column_name == origin_column_name
# alias_attributeと同じことを、対象カラム名を動的に変更して行う
define_getter_method(column_name)
define_setter_method(column_name)
define_predicate_method(column_name)
define_was_method(column_name)
define_getter_method(new_column_name)
define_setter_method(new_column_name)
define_predicate_method(new_column_name)
define_was_method(new_column_name)
end
end

Expand Down Expand Up @@ -148,10 +157,6 @@ module ClassMethods
module QueryMethods
include Utusemi::Core::Base

def utusemi!(obj = nil, options = {})
super.tap { warning_checker unless Rails.env.production? }
end

def build_where(opts = :chain, *rest)
return super unless utusemi_values[:flag]
if utusemi_values[:options][:times]
Expand Down Expand Up @@ -192,13 +197,6 @@ def mapped_column_names_for_string(string, index = nil)
end
string
end

def warning_checker
utusemi_column_names.each do |old_column_name, new_column_name|
return if old_column_name != new_column_name
Rails.logger.warn "[Utusemi:WARNING] #{old_column_name} is duplicated in Utusemi::Engine.config.#{utusemi_values[:type]}_columns."
end
end
end

# Rails 3.x で scope に対してのカラムマッピングが正常に動作するようにするためのもの
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/utusemi/core_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
describe Utusemi::Core do
describe Utusemi::Core::Base do
describe '#warning_checker' do
let(:warning_message) { '[Utusemi:WARNING] "title" is duplicated in map(:product).' }

before do
Utusemi.configure do
map(:product) { title :title }
end
end

context 'alias is duplicated in ::utusemi' do
let!(:product) { FactoryGirl.create(:product, :with_stock) }
it 'output warning' do
expect(Rails.logger).to receive(:warn).with(warning_message)
Product.utusemi(:product)
end
end

context 'alias is duplicated in #utusemi' do
let(:product) { FactoryGirl.build(:product, :with_stock) }
it 'output warning' do
expect(Rails.logger).to receive(:warn).with(warning_message)
product.utusemi(:product)
end
end
end
end

# TODO: Implement the new syntax
#
# map(:sample_one) { ... }
Expand Down

0 comments on commit fd185a6

Please sign in to comment.