Skip to content

Commit

Permalink
Address hound comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatasrancan committed Oct 16, 2017
1 parent c0895f1 commit 63b56cd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
14 changes: 7 additions & 7 deletions lib/administrate/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ def query_table_name(attr)
if association_search?(attr)
ActiveRecord::Base.connection.quote_table_name(attr.to_s.pluralize)
else
ActiveRecord::Base.connection.quote_table_name(@scoped_resource.table_name)
ActiveRecord::Base.connection
.quote_table_name(@scoped_resource.table_name)
end
end

def column_to_query(attr)
if association_search?(attr)
ActiveRecord::Base.connection.quote_column_name(attribute_types[attr].searchable_field)
ActiveRecord::Base.connection
.quote_column_name(attribute_types[attr].searchable_field)
else
ActiveRecord::Base.connection.quote_column_name(attr)
end
Expand All @@ -64,17 +66,15 @@ def tables_to_join
end
end

attr_reader :resolver, :term

private

def association_search?(attribute)
return unless attribute_types[attribute].respond_to?(:deferred_class)
[
Administrate::Field::BelongsTo,
Administrate::Field::HasMany,
Administrate::Field::HasOne
Administrate::Field::HasOne,
].include?(attribute_types[attribute].deferred_class)
end

attr_reader :resolver, :term
end
end
2 changes: 1 addition & 1 deletion spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CustomerDashboard < Administrate::BaseDashboard
primary_key: :code,
foreign_key: :country_code,
searchable: true,
searchable_field: 'name'
searchable_field: "name",
),
}

Expand Down
5 changes: 4 additions & 1 deletion spec/lib/administrate/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
end

def relation_with_column(column)
double(columns_hash: { column.to_s => :column_info }, table_name: 'table_name')
double(
columns_hash: { column.to_s => :column_info },
table_name: "table_name"
)
end
end
20 changes: 14 additions & 6 deletions spec/lib/administrate/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ class MockDashboard
name: Administrate::Field::String,
email: Administrate::Field::Email,
phone: Administrate::Field::Number,
}
}.freeze
end

class MockDashboardWithAssociation
ATTRIBUTE_TYPES = {
role: Administrate::Field::BelongsTo.with_options(searchable: true, searchable_field: 'name'),
address: Administrate::Field::HasOne.with_options(searchable: true, searchable_field: 'street')
role: Administrate::Field::BelongsTo.with_options(
searchable: true,
searchable_field: "name"
),
address: Administrate::Field::HasOne.with_options(
searchable: true,
searchable_field: "street"
),
}
end

Expand Down Expand Up @@ -101,7 +107,7 @@ class User < ActiveRecord::Base; end
Administrate::Search.new(
scoped_object,
MockDashboardWithAssociation,
"Тест Test"
"Тест Test",
)
end

Expand All @@ -117,13 +123,15 @@ class User < ActiveRecord::Base; end
it "joins with the correct association table to query" do
allow(scoped_object).to receive(:where)

expect(scoped_object).to receive(:joins).with([:role, :address]).and_return(scoped_object)
expect(scoped_object).to receive(:joins).with(%i(role address))
.and_return(scoped_object)

search.run
end

it "builds the 'where' clause using the joined tables" do
allow(scoped_object).to receive(:joins).with([:role, :address]).and_return(scoped_object)
allow(scoped_object).to receive(:joins).with(%i(role address))
.and_return(scoped_object)

expect(scoped_object).to receive(:where).with(*expected_query)

Expand Down

0 comments on commit 63b56cd

Please sign in to comment.