From c41c56722fe5647aafbda08667ad6e404cd84a67 Mon Sep 17 00:00:00 2001 From: John Dell Date: Mon, 13 Oct 2014 16:15:35 -0700 Subject: [PATCH] Add failing test for #437 Give person a ```belongs_to``` association to group for testing Fix for #437 --- lib/ransack/translate.rb | 4 ++-- spec/ransack/helpers/form_builder_spec.rb | 27 ++++++++++++++++++++--- spec/support/schema.rb | 11 +++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/ransack/translate.rb b/lib/ransack/translate.rb index b231d51..922e7d9 100644 --- a/lib/ransack/translate.rb +++ b/lib/ransack/translate.rb @@ -100,9 +100,9 @@ module Ransack def self.build_interpolations(associated_class) { :attr_fallback_name => attr_fallback_name(associated_class), - :association_name => association_name + :association_name => association_name } - .reject! { |_, value| value.nil? } + .reject { |_, value| value.nil? } end def self.attr_fallback_name(associated_class) diff --git a/spec/ransack/helpers/form_builder_spec.rb b/spec/ransack/helpers/form_builder_spec.rb index 7d42695..92149e5 100644 --- a/spec/ransack/helpers/form_builder_spec.rb +++ b/spec/ransack/helpers/form_builder_spec.rb @@ -43,9 +43,30 @@ module Ransack describe '#label' do - it 'localizes attribute names' do - html = @f.label :name_cont - expect(html).to match /Full Name contains/ + context 'with direct model attributes' do + it 'localizes attribute names' do + html = @f.label :name_cont + expect(html).to match /Full Name contains/ + end + + it 'falls back to column name when no translation' do + html = @f.label :email_cont + expect(html).to match /Email contains/ + end + end + + context 'with has_many association attributes' do + it 'falls back to associated model + column name when no translation' do + html = @f.label :article_title_cont + expect(html).to match /Article title contains/ + end + end + + context 'with belongs_to association attributes' do + it 'falls back to associated model + column name when no translation' do + html = @f.label :group_name_cont + expect(html).to match /Group name contains/ + end end end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 9aca350..bba727d 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -22,6 +22,10 @@ else ) end +class Group < ActiveRecord::Base + has_many :people +end + class Person < ActiveRecord::Base if ActiveRecord::VERSION::MAJOR == 3 default_scope order('id DESC') @@ -29,6 +33,7 @@ class Person < ActiveRecord::Base default_scope { order(id: :desc) } end belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id + belongs_to :group has_many :children, :class_name => 'Person', :foreign_key => :parent_id has_many :articles has_many :comments @@ -126,8 +131,14 @@ module Schema ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do + create_table :groups, :force => true do |t| + t.string :name + t.timestamps null: false + end + create_table :people, :force => true do |t| t.integer :parent_id + t.integer :group_id t.string :name t.string :email t.string :only_search