Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value_humanizer to i18n-ize header values #70

Merged
merged 1 commit into from
Aug 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/comma/extractors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_support/core_ext/class/attribute'

module Comma

class Extractor
Expand Down Expand Up @@ -27,18 +29,26 @@ def convert_to_data_value(result)

class HeaderExtractor < Extractor

class_attribute :value_humanizer

DEFAULT_VALUE_HUMANIZER = lambda do |value, model_class|
value.is_a?(String) ? value : value.to_s.humanize
end
self.value_humanizer = DEFAULT_VALUE_HUMANIZER

def method_missing(sym, *args, &block)
@results << sym.to_s.humanize if args.blank?
model_class = @instance.class
@results << self.value_humanizer.call(sym, model_class) if args.blank?
args.each do |arg|
case arg
when Hash
arg.each do |k, v|
@results << ((v.is_a? String) ? v : v.to_s.humanize)
@results << self.value_humanizer.call(v, get_association_class(model_class, sym))
end
when Symbol
@results << arg.to_s.humanize
@results << self.value_humanizer.call(arg, get_association_class(model_class, sym))
when String
@results << arg
@results << self.value_humanizer.call(arg, model_class)
else
raise "Unknown header symbol #{arg.inspect}"
end
Expand All @@ -48,6 +58,13 @@ def method_missing(sym, *args, &block)
def __static_column__(header = '', &block)
@results << header
end

private

def get_association_class(model_class, association)
model_class.respond_to?(:reflect_on_association) ?
model_class.reflect_on_association(arg).klass : nil
end
end

class DataExtractor < Extractor
Expand Down
31 changes: 31 additions & 0 deletions spec/comma/rails/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
require 'spec_helper'

describe Comma, 'generating CSV from an ActiveRecord object' do
Expand Down Expand Up @@ -35,4 +36,34 @@ class Person < ActiveRecord::Base
Person.teenagers.to_comma
end
end

describe 'with custom value_humanizer' do
before do
Comma::HeaderExtractor.value_humanizer =
lambda do |value, model_class|
if model_class.respond_to?(:human_attribute_name)
model_class.human_attribute_name(value)
else
value.is_a?(String) ? value : value.to_s.humanize
end
end

I18n.config.backend.store_translations(:ja, {:activerecord => {:attributes => {:person => {:age => '年齢', :name => '名前'}}}})
@original_locale = I18n.locale
I18n.locale = :ja

Person.create(:age => 19, :name => 'John')
end

after do
I18n.locale = @original_locale

Comma::HeaderExtractor.value_humanizer =
Comma::HeaderExtractor::DEFAULT_VALUE_HUMANIZER
end

it 'should i18n-ize header values' do
Person.teenagers.to_comma.should match(/^名前,年齢/)
end
end
end