2011-03-31 00:31:39 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
module Helpers
|
|
|
|
describe FormBuilder do
|
|
|
|
|
|
|
|
router = ActionDispatch::Routing::RouteSet.new
|
|
|
|
router.draw do
|
|
|
|
resources :people
|
2013-11-29 14:44:51 +00:00
|
|
|
resources :notes
|
2013-03-01 20:29:40 +00:00
|
|
|
get ':controller(/:action(/:id(.:format)))'
|
2011-03-31 00:31:39 +00:00
|
|
|
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 do
|
|
|
|
include router.url_helpers
|
|
|
|
end
|
|
|
|
|
2011-04-18 15:28:19 +00:00
|
|
|
@controller.view_context_class.class_eval do
|
|
|
|
include router.url_helpers
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
@s = Person.search
|
|
|
|
@controller.view_context.search_form_for @s do |f|
|
|
|
|
@f = f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-08 12:25:06 +00:00
|
|
|
# TODO: fix this test after the following Rails 4.2 PR and commit which
|
|
|
|
# broke it: https://github.com/rails/rails/pull/17203 and
|
|
|
|
# https://github.com/schneems/rails/commit/03d30ce7
|
|
|
|
if ::ActiveRecord::VERSION::STRING < '4.2'.freeze
|
|
|
|
it 'selects previously-entered time values with datetime_select' do
|
|
|
|
@s.created_at_eq = [2011, 1, 2, 3, 4, 5]
|
|
|
|
html = @f.datetime_select(
|
|
|
|
:created_at_eq,
|
|
|
|
:use_month_numbers => true,
|
|
|
|
:include_seconds => true
|
|
|
|
)
|
|
|
|
%w(2011 1 2 03 04 05).each do |val|
|
|
|
|
expect(html).to match /<option selected="selected" value="#{val}">#{val}<\/option>/
|
|
|
|
end
|
2011-06-04 22:01:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-05 18:29:54 +00:00
|
|
|
describe '#label' do
|
|
|
|
|
|
|
|
it 'localizes attribute names' do
|
|
|
|
html = @f.label :name_cont
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /Full Name contains/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
2011-06-04 22:01:12 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-08-13 20:36:40 +00:00
|
|
|
describe '#sort_link' do
|
2013-05-23 12:56:48 +00:00
|
|
|
it 'sort_link for ransack attribute' do
|
2014-05-01 13:55:39 +00:00
|
|
|
sort_link = @f.sort_link :name, :controller => 'people'
|
|
|
|
if ActiveRecord::VERSION::STRING =~ /^3\.[1-2]\./
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(sort_link).to match /people\?q%5Bs%5D=name\+asc/
|
2013-08-06 16:56:16 +00:00
|
|
|
else
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(sort_link).to match /people\?q(%5B|\[)s(%5D|\])=name\+asc/
|
2013-08-06 16:56:16 +00:00
|
|
|
end
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(sort_link).to match /sort_link/
|
|
|
|
expect(sort_link).to match /Full Name<\/a>/
|
2013-05-23 12:56:48 +00:00
|
|
|
end
|
2011-08-13 20:36:40 +00:00
|
|
|
|
2013-05-23 12:56:48 +00:00
|
|
|
it 'sort_link for common attribute' do
|
2014-05-01 13:55:39 +00:00
|
|
|
sort_link = @f.sort_link :id, :controller => 'people'
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(sort_link).to match /id<\/a>/
|
2013-05-23 12:56:48 +00:00
|
|
|
end
|
2011-08-13 20:36:40 +00:00
|
|
|
end
|
|
|
|
|
2011-06-05 18:29:54 +00:00
|
|
|
describe '#submit' do
|
|
|
|
|
|
|
|
it 'localizes :search when no default value given' do
|
|
|
|
html = @f.submit
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /"Search"/
|
2011-06-04 22:01:12 +00:00
|
|
|
end
|
2011-06-05 18:29:54 +00:00
|
|
|
|
2011-06-04 22:01:12 +00:00
|
|
|
end
|
|
|
|
|
2011-06-05 18:29:54 +00:00
|
|
|
describe '#attribute_select' do
|
|
|
|
|
|
|
|
it 'returns ransackable attributes' do
|
|
|
|
html = @f.attribute_select
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html.split(/\n/).size).
|
|
|
|
to eq(Person.ransackable_attributes.size + 1)
|
2011-06-05 18:29:54 +00:00
|
|
|
Person.ransackable_attributes.each do |attribute|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /<option value="#{attribute}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
2011-06-04 22:01:12 +00:00
|
|
|
end
|
|
|
|
|
2011-06-05 18:29:54 +00:00
|
|
|
it 'returns ransackable attributes for associations with :associations' do
|
2013-08-04 13:13:41 +00:00
|
|
|
attributes = Person.ransackable_attributes + Article.
|
|
|
|
ransackable_attributes.map { |a| "articles_#{a}" }
|
2014-05-01 13:55:39 +00:00
|
|
|
html = @f.attribute_select(:associations => ['articles'])
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html.split(/\n/).size).to eq(attributes.size)
|
2011-06-05 18:29:54 +00:00
|
|
|
attributes.each do |attribute|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /<option value="#{attribute}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
2011-06-05 18:29:54 +00:00
|
|
|
|
|
|
|
it 'returns option groups for base and associations with :associations' do
|
2014-05-01 13:55:39 +00:00
|
|
|
html = @f.attribute_select(:associations => ['articles'])
|
2011-06-05 18:29:54 +00:00
|
|
|
[Person, Article].each do |model|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /<optgroup label="#{model}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
2011-06-05 18:29:54 +00:00
|
|
|
describe '#predicate_select' do
|
|
|
|
|
|
|
|
it 'returns predicates with predicate_select' do
|
|
|
|
html = @f.predicate_select
|
2011-06-07 01:32:03 +00:00
|
|
|
Predicate.names.each do |key|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /<option value="#{key}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters predicates with single-value :only' do
|
2014-05-01 13:55:39 +00:00
|
|
|
html = @f.predicate_select :only => 'eq'
|
2013-08-04 13:13:41 +00:00
|
|
|
Predicate.names.reject { |k| k =~ /^eq/ }.each do |key|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).not_to match /<option value="#{key}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters predicates with multi-value :only' do
|
2013-08-04 13:13:41 +00:00
|
|
|
html = @f.predicate_select only: [:eq, :lt]
|
|
|
|
Predicate.names.reject { |k| k =~ /^(eq|lt)/ }.each do |key|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).not_to match /<option value="#{key}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-04 13:13:41 +00:00
|
|
|
it 'excludes compounds when compounds: false' do
|
2014-05-01 13:55:39 +00:00
|
|
|
html = @f.predicate_select :compounds => false
|
2013-08-04 13:13:41 +00:00
|
|
|
Predicate.names.select { |k| k =~ /_(any|all)$/ }.each do |key|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).not_to match /<option value="#{key}">/
|
2011-06-05 18:29:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-29 14:44:51 +00:00
|
|
|
|
|
|
|
context 'fields used in polymorphic relations as search attributes in form' do
|
|
|
|
before do
|
|
|
|
@controller.view_context.search_form_for Note.search do |f|
|
|
|
|
@f = f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
it 'accepts poly_id field' do
|
|
|
|
html = @f.text_field(:notable_id_eq)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /id=\"q_notable_id_eq\"/
|
2013-11-29 14:44:51 +00:00
|
|
|
end
|
|
|
|
it 'accepts poly_type field' do
|
|
|
|
html = @f.text_field(:notable_type_eq)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(html).to match /id=\"q_notable_type_eq\"/
|
2013-11-29 14:44:51 +00:00
|
|
|
end
|
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-01 20:29:40 +00:00
|
|
|
end
|