Skip to content

Commit

Permalink
Fix manually adding and id to association
Browse files Browse the repository at this point in the history
  • Loading branch information
wspurgin authored and zorab47 committed Dec 4, 2017
1 parent b94b9e7 commit b4931d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/active_admin/inputs/filters/select_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def searchable_method_name
"#{reflection.through_reflection.name}_#{reflection.foreign_key}"
else
name = method.to_s
name.concat '_id' if reflection
name.concat "_#{reflection.association_primary_key}" if reflection_searchable?
name
end
end
Expand Down Expand Up @@ -48,6 +48,10 @@ def pluck_column
klass.reorder("#{method} asc").distinct.pluck method
end

def reflection_searchable?
reflection && !reflection.polymorphic?
end

end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/filters/filter_form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,28 @@ def filter(name, options = {})
end
end

context "when given the name of relationship with a primary key other than id" do
let(:resource_klass) {
Class.new(Post) do
belongs_to :kategory, class_name: "Category", primary_key: :name, foreign_key: :title

def self.name
"SuperPost"
end
end
}

let(:scope) do
resource_klass.search
end

let(:body) { Capybara.string(filter :kategory) }

it "should use the association primary key" do
expect(body).to have_selector("select[name='q[kategory_name_eq]']")
end
end

context "as check boxes" do
let(:body) { Capybara.string(filter :author, as: :check_boxes) }

Expand Down

0 comments on commit b4931d2

Please sign in to comment.