Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of pluck instead of select #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

lastobelus
Copy link

this in conjunction with using a scope that joins
an association allows fetching attributes of associated
records.

class Organization
  belongs_to :contact

  scope :with_contact, -> { joins(:contact) }
end

autocomplete :organization, :name, extra_data: ['contacts.email'],
select_method: :pluck, scopes: [:with_contact]

f.autocomplete_field :name,
autocomplete_organization_name_product_path, update_elements: {id:
'#products_organization_attributes_id', 'contacts.email':
'#products_contact_attributes_email'}

I have not yet written any tests, let me know if you are interested in including this and I will try to provide some coverage.

this in conjunction with using a scope that joins
an association allows fetching attributes of associated
records.

class Organization
  belongs_to :contact

  scope :with_contact, -> { joins(:contact) }
end

autocomplete :organization, :name, extra_data: ['contacts.email'],
select_method: :pluck, scopes: [:with_contact]

f.autocomplete_field :name,
autocomplete_organization_name_product_path, update_elements: {id:
'#products_organization_attributes_id', 'contacts.email':
'#products_contact_attributes_email'}
@@ -77,6 +79,7 @@ class AutocompleteTest < Test::Unit::TestCase
should 'add that extra data to result' do
item = mock(Object)
mock(item).send(:name).times(2) { 'Object Name' }
stub(item).kind_of?(Hash){ false }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer Object#is_a? over Object#kind_of?.
Space missing to the left of {.

@bigtunacan
Copy link
Collaborator

@lastobelus Yeah; this looks useful. If you want to add a couple of test cases and some documentation to the readme I'll merge it in.

Thanks

because rails internally does a uniq before querying
item["label"] = item["value"] = item[method.to_s]
end
else
items = items.collect do |item|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants