require 'spec_helper' module Ransack module Helpers describe FormBuilder do router = ActionDispatch::Routing::RouteSet.new router.draw do resources :people, :comments, :notes end include router.url_helpers # FIXME: figure out a cleaner way to get this behavior before do @controller = ActionView::TestCase::TestController.new @controller.instance_variable_set(:@_routes, router) @controller.class_eval { include router.url_helpers } @controller.view_context_class.class_eval { include router.url_helpers } @s = Person.ransack @controller.view_context.search_form_for(@s) { |f| @f = f } end it 'selects previously-entered time values with datetime_select' do date_values = %w(2011 1 2 03 04 05) # @s.created_at_eq = date_values # This works in Rails 4.x but not 3.x @s.created_at_eq = [2011, 1, 2, 3, 4, 5] # so we have to do this html = @f.datetime_select( :created_at_eq, use_month_numbers: true, include_seconds: true ) date_values.each { |val| expect(html).to include date_select_html(val) } end describe '#label' do context 'with direct model attributes' do it 'localizes attribute names' do test_label(@f, :name_cont, /Full Name contains/) test_label(@f, :only_admin_start, /admin uSer Only starts with/) test_label(@f, :salary_gt, /wages greater than/) test_label(@f, :awesome_true, /ransack is really awesome is true/) end it 'falls back to `attribute_name.capitalize` when no translation' do test_label(@f, :email_cont, /Email contains/) test_label(@f, :only_sort_start, /Only sort starts with/) test_label(@f, :only_search_eq, /Only search equals/) end end context 'with `has_many` association attributes' do it 'localizes :"#{pluralized model}_#{attribute name}_#{predicate}"' do test_label(@f, :articles_body_start, /Article maiN BoDy starts with/) end it 'falls back to `model_name.capitalize + attribute_name.capitalize` when no translation' do test_label(@f, :articles_title_cont, /Article Title contains/) test_label(@f, :articles_subject_header_start, /Article Subject header starts with/) end end context 'with `belongs_to` association attributes' do before do @controller.view_context.search_form_for(Comment.ransack) { |f| @f = f } end it 'localizes :"#{singularized model}_#{attribute name}_#{predicate}"' do test_label(@f, :article_body_start, /Article maiN BoDy starts with/) end it 'falls back to `model_name.capitalize + attribute_name.capitalize` when no translation' do test_label(@f, :article_title_eq, /Article Title equals/) test_label(@f, :article_subject_header_end, /Article Subject header ends with/) end end end describe '#sort_link' do it 'sort_link for ransack attribute' do sort_link = @f.sort_link :name, controller: 'people' expect(sort_link).to match /people\?q(%5B|\[)s(%5D|\])=name\+asc/ expect(sort_link).to match /sort_link/ expect(sort_link).to match /Full Name<\/a>/ end it 'sort_link for common attribute' do sort_link = @f.sort_link :id, controller: 'people' expect(sort_link).to match /id<\/a>/ end end describe '#submit' do it 'localizes :search when no default value given' do html = @f.submit expect(html).to match /"Search"/ end end describe '#attribute_select' do it 'returns ransackable attributes' do html = @f.attribute_select expect(html.split(/\n/).size).to eq(Person.ransackable_attributes.size + 1) Person.ransackable_attributes.each do |attribute| expect(html).to match // end end end describe '#predicate_select' do it 'returns predicates with predicate_select' do html = @f.predicate_select Predicate.names.each do |key| expect(html).to match /) end end end end