Skip to content

Commit

Permalink
Merge cddeb94 into 0be9e22
Browse files Browse the repository at this point in the history
  • Loading branch information
barsukov committed Aug 26, 2013
2 parents 0be9e22 + cddeb94 commit 647b1d2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 55 deletions.
16 changes: 14 additions & 2 deletions app/assets/javascripts/rails_admin/ra.filter-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'<option value="false"' + (field_value == "false" ? 'selected="selected"' : '') + '>False</option>' +
'<option disabled="disabled">---------</option>' +
'<option ' + (field_value == "_present" ? 'selected="selected"' : '') + ' value="_present">Is present</option>' +
'<option ' + (field_value == "_blank" ? 'selected="selected"' : '') + ' value="_blank" >Is blank</option>' +
'<option ' + (field_value == "_blank" ? 'selected="selected"' : '') + ' value="_blank" >Has blank</option>' +
'</select>';
break;
case 'date':
Expand All @@ -49,7 +49,7 @@
var control = '<select style="display:' + (multiple_values ? 'none' : 'inline-block') + '" ' + (multiple_values ? '' : 'name="' + value_name + '"') + ' data-name="' + value_name + '" class="select-single input-small">' +
'<option value="_discard">...</option>' +
'<option ' + (field_value == "_present" ? 'selected="selected"' : '') + ' value="_present">Is present</option>' +
'<option ' + (field_value == "_blank" ? 'selected="selected"' : '') + ' value="_blank">Is blank</option>' +
'<option ' + (field_value == "_blank" ? 'selected="selected"' : '') + ' value="_blank">Has blank</option>' +
'<option disabled="disabled">---------</option>' +
field_options +
'</select>' +
Expand All @@ -72,6 +72,18 @@
'</select>'
var additional_control = '<input class="additional-fieldset input-small" style="display:' + (field_operator == "_blank" || field_operator == "_present" ? 'none' : 'inline-block') + ';" type="text" name="' + value_name + '" value="' + field_value + '" /> ';
break;
case 'has_many_association':
var control = '<select class="switch-additionnal-fieldsets input-small" value="' + field_operator + '" name="' + operator_name + '">' +
'<option data-additional-fieldset="additional-fieldset"' + (field_operator == "like" ? 'selected="selected"' : '') + ' value="like">Contains</option>' +
'<option data-additional-fieldset="additional-fieldset"' + (field_operator == "is" ? 'selected="selected"' : '') + ' value="is">Is exactly</option>' +
'<option data-additional-fieldset="additional-fieldset"' + (field_operator == "starts_with" ? 'selected="selected"' : '') + ' value="starts_with">Starts with</option>' +
'<option data-additional-fieldset="additional-fieldset"' + (field_operator == "ends_with" ? 'selected="selected"' : '') + ' value="ends_with">Ends with</option>' +
'<option disabled="disabled">---------</option>' +
'<option ' + (field_operator == "_present" ? 'selected="selected"' : '') + ' value="_present">Is present</option>' +
'<option ' + (field_operator == "_blank" ? 'selected="selected"' : '') + ' value="_blank">Is blank</option>' +
'</select>'
var additional_control = '<input class="additional-fieldset input-small" style="display:' + (field_operator == "_blank" || field_operator == "_present" ? 'none' : 'inline-block') + ';" type="text" name="' + value_name + '" value="' + field_value + '" /> ';
break;
case 'integer':
case 'decimal':
case 'float':
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def check_for_cancel
end

def get_collection(model_config, scope, pagination)
associations = model_config.list.fields.select {|f| f.type == :belongs_to_association && !f.polymorphic? }.map {|f| f.association[:name] }
associations = model_config.list.fields.select {|f| f.type == :belongs_to_association || f.type == :has_many_association && !f.polymorphic? }.map {|f| f.association[:name] }
options = {}
options = options.merge(:page => (params[Kaminari.config.param_name] || 1).to_i, :per => (params[:per] || model_config.list.items_per_page)) if pagination
options = options.merge(:include => associations) unless associations.blank?
Expand Down
12 changes: 12 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
end

factory :team do
division
sequence(:division_id)
sequence(:name) { |n| "Team #{n}" }
sequence(:manager) { |n| "Manager #{n}" }
Expand All @@ -34,6 +35,17 @@
factory :division do
sequence(:custom_league_id)
sequence(:name) { |n| "Division #{n}" }

trait :premier do
name "Premier division"
end

trait :second do
name "Second division"
end

factory :premier_division, traits: [:premier]
factory :second_division, traits: [:second]
end

factory :fan do
Expand Down
104 changes: 52 additions & 52 deletions spec/integration/basic/list/rails_admin_basic_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,36 @@
end
end

#for test has_many association and filters
describe "GET /admin/division" do
let!(:premier_division) { FactoryGirl.create(:premier_division)}
let!(:second_division) { FactoryGirl.create(:second_division)}
let!(:premier_teams) {FactoryGirl.create_list(:team, 5,division: premier_division)}
let!(:second_teams) { FactoryGirl.create_list(:team, 5,division: second_division)}

context 'on the list page of division' do
setup do
RailsAdmin.config Division do
list do
field :name
field :teams , :has_many_association do
searchable [:name]
visible true
end
end
end
end
it "allows to filter on has_many relationships" do
visit index_path(:model_name => "division" , :f => {"0000" => {:o => "is", :v => premier_teams.first.name }})
should have_content(premier_teams.first.name)
should have_no_content(second_teams.first.name)
end
end
end

describe "GET /admin/player" do
before do
@teams = 2.times.map do
FactoryGirl.create(:team)
end
@teams = FactoryGirl.create_list(:team,2)
@players = [
FactoryGirl.create(:player, :retired => true, :injured => true, :team => @teams[0]),
FactoryGirl.create(:player, :retired => true, :injured => false, :team => @teams[0]),
Expand All @@ -67,7 +92,7 @@
]
end

it "allows to query on any attribute" do
context "base list action of players" do
RailsAdmin.config Player do
list do
field :name
Expand All @@ -77,62 +102,37 @@
end
end

visit index_path(:model_name => "player", :query => @players[0].name)
should have_content(@players[0].name)
(1..3).each do |i|
should have_no_content(@players[i].name)
end
end

it "allows to filter on one attribute" do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
it "allows to query on any attribute" do
visit index_path(:model_name => "player", :query => @players[0].name)
should have_content(@players[0].name)
(1..3).each do |i|
should have_no_content(@players[i].name)
end
end

visit index_path(:model_name => "player", :f => {:injured => {"1" => {:v => "true"}}})
should have_content(@players[0].name)
should have_no_content(@players[1].name)
should have_content(@players[2].name)
should have_no_content(@players[3].name)
end

it "allows to combine filters on two different attributes" do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
end
end

visit index_path(:model_name => "player", :f => {:retired => {"1" => {:v => "true"}}, :injured => {"1" => {:v => "true"}}})
should have_content(@players[0].name)
(1..3).each do |i|
should have_no_content(@players[i].name)
it "allows to filter on one attribute" do
visit index_path(:model_name => "player", :f => {:injured => {"1" => {:v => "true"}}})
should have_content(@players[0].name)
should have_no_content(@players[1].name)
should have_content(@players[2].name)
should have_no_content(@players[3].name)
end
end

it "allows to filter on belongs_to relationships" do
RailsAdmin.config Player do
list do
field :name
field :team
field :injured
field :retired
it "allows to combine filters on two different attributes" do
visit index_path(:model_name => "player", :f => {:retired => {"1" => {:v => "true"}}, :injured => {"1" => {:v => "true"}}})
should have_content(@players[0].name)
(1..3).each do |i|
should have_no_content(@players[i].name)
end
end

visit index_path(:model_name => "player", :f => {:team => {"1" => { :v => @teams[0].name }}})
should have_content(@players[0].name)
should have_content(@players[1].name)
should have_no_content(@players[2].name)
should have_no_content(@players[3].name)
it "allows to filter on belongs_to relationships" do
visit index_path(:model_name => "player", :f => {:team => {"1" => { :v => @teams[0].name }}})
should have_content(@players[0].name)
should have_content(@players[1].name)
should have_no_content(@players[2].name)
should have_no_content(@players[3].name)
end
end

it "allows to disable search on attributes" do
Expand Down

0 comments on commit 647b1d2

Please sign in to comment.