Skip to content

Commit

Permalink
Refactor: extract i18n method into TableField
Browse files Browse the repository at this point in the history
  • Loading branch information
jgdavey committed Nov 26, 2010
1 parent c110f01 commit 2acfa55
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
6 changes: 1 addition & 5 deletions lib/tabletastic/table_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ def head
content_tag(:thead) do
content_tag(:tr) do
@table_fields.inject("") do |result,field|
field_heading = field.heading.to_s.downcase
localised_heading = I18n.translate(field_heading,
:scope => [:tabletastic, :models, klass.model_name.collection.singularize],
:default => field.heading) unless(field.heading.empty?)
result + content_tag(:th, localised_heading, field.heading_html)
result + content_tag(:th, field.heading, field.heading_html)
end.html_safe
end
end
Expand Down
22 changes: 19 additions & 3 deletions lib/tabletastic/table_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module Tabletastic
class TableField
@@association_methods = %w[to_label display_name full_name name title username login value to_str to_s]

attr_accessor :heading, :method_or_proc, :cell_html, :heading_html
attr_reader :heading, :method, :method_or_proc, :cell_html, :heading_html, :klass

def initialize(*args, &proc)
options = args.extract_options!
method = args.first.to_sym
@method = args.first.to_sym
@method_or_proc = block_given? ? proc : method
@cell_html, @heading_html = options[:cell_html], options[:heading_html]
@klass = options.delete(:klass)
@heading = options.delete(:heading) || @klass.try(:human_attribute_name, method.to_s) || method.to_s.humanize
@heading = options.delete(:heading) || default_heading
end

def cell_data(record)
Expand All @@ -29,6 +29,22 @@ def cell_data(record)

private

def default_heading
I18n.translate(method, :scope => i18n_scope, :default => klass_default_heading)
end

def klass_default_heading
if klass.respond_to?(:human_attribute_name)
klass.human_attribute_name(method.to_s)
else
method.to_s.humanize
end
end

def i18n_scope
[:tabletastic, :models, klass.try(:model_name).try(:singular)].compact
end

def detect_string_method(association)
@@association_methods.detect { |method| association.respond_to?(method) }
end
Expand Down
47 changes: 36 additions & 11 deletions spec/tabletastic/table_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@
tf.heading.should == "Method"
end

it "should know its heading when provided" do
tf = TableField.new(:method, :heading => 'Foo', :klass => ::Post)
tf.heading.should == "Foo"
end

it "should know what to do with a record" do
tf = TableField.new(:downcase)
tf.cell_data("HELLO").should == "hello"
end

it "should use a human_attribute_name whenever possible" do
::Post.stub!(:human_attribute_name).with('method').and_return("Blah blue")
tf = TableField.new(:method, :klass => ::Post)
tf.heading.should == "Blah blue"
end

it "should know what to do with a record (proc)" do
tf = TableField.new(:fake) do |record|
record.upcase
Expand All @@ -35,4 +24,40 @@
tf = TableField.new(:booya)
tf.cell_data(post).should_not be_html_safe
end

describe "#heading" do
subject { TableField.new(:method, :heading => heading, :klass => Post) }
context "when heading option is provided" do
let(:heading) { "Foo" }
its(:heading) { should == "Foo" }
end

context "when heading option is omitted" do
let(:heading) { nil }
let(:derived_heading) { "Blah blue" }
let(:i18n_heading) { "I18n Foo" }

context "with I18n attributes defined" do
let(:i18n_translations) do
{ :tabletastic => { :models => { :post => { :method => i18n_heading } } } }
end
before { I18n.backend.store_translations :en, i18n_translations }
after { I18n.backend.reload! }

its(:heading) { should == i18n_heading }
end

context "with no I18n attributes defined" do
context "with human_attribute_name-capable class" do
before { Post.stub!(:human_attribute_name).with('method').and_return(derived_heading) }
its(:heading) { should == derived_heading }
end

context "without human_attribute_name-capable class" do
its(:heading) { should == "Method" }
end
end

end
end
end

0 comments on commit 2acfa55

Please sign in to comment.