Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails gem rails3-jquery-autocomplete how to scope by user

I'm using the Rails gem rails3-jquery-autocomplete to add categories to posts.

I would like to restrict the search to include only categories that belong to the current user or post's author in the results.

The documentation says that I can specify a scope:

:scopes

Added option to use scopes. Pass scopes in an array. e.g :scopes => [:scope1, :scope2]

But I'm not sure how I would pass the user id here?

It seems like a comon scenario, am I missing something obvious?

I found an answer that suggests modifying the get_item method, but that seems to break the auto-complete

Scoping the results for rails3 jquery autocomplete plugin

like image 276
deb Avatar asked Aug 30 '11 14:08

deb


6 Answers

I know the gem and the question are old but I found myself using this gem and needing this answer recently... None of the old answers will work anymore because in the source code, the method get_autocomplete_items is generated dynamically and has the ORM prepended on the method name. This is what got it working for me. I assume most folks are using ActiveRecord too but check the autocomplete.rb method 'get_prefix' to figure out what you should prepend to the method name to get it working.

Hope this saves someone a bunch of time. Be the change you want to see and all that ;)

def active_record_get_autocomplete_items(parameters)
  super(parameters).where(id: current_user.id)
end
like image 188
bjgaynor Avatar answered Oct 15 '22 19:10

bjgaynor


To answer the question posed by @ctilley79, multiple autocompletes is not a problem because, in addition to the possibility of passing more values in the params hash, you also have access to the autocomplete parameters. On my form (as an example), I have both a City and a Zip autocomplete. I need to restrict the City to those in a certain state. So my controller action looks like this:

def get_autocomplete_items(parameters)
  if (parameters[:model] == City)
    super(parameters).where("state_id" => params[:state_id])
  else
    super(parameters)
  end
end

You also have access to the method in case you need it. Do logger.debug on the parameters to see all that is available.

like image 39
Mark Fraser Avatar answered Oct 15 '22 19:10

Mark Fraser


In posts_controller:

 def get_autocomplete_items(parameters)
    items = super(parameters)
    items = items.where(:user_id => current_user.id)
  end

I'm first calling the original get_autocomplete_items method, and then filtering out the results by current_user.id.

This question helped: Rails 3: alias_method_chain still used?

like image 17
deb Avatar answered Oct 19 '22 12:10

deb


I had a similar problem I solved thanks to the answers above.

My autocomplete also worked against a User model, but I needed to restrict the results to the user's institution (Institution has many :users). My controller creates an @institution instance variable that is accessed in the view.

Although the get_autocomplete_items method cannot directly access the instance variable, I found that the data CAN be passed to autocomplete as a parameter (note: I use the simple_forms gem, so the input call looks a little different than the standard rails syntax).

In my view:

<%= f.input :email,  :url => autocomplete_user_email_institutions_path(:institution_id=>@institution.id.to_s), :as => :autocomplete %>

In my controller:

  autocomplete :user, :email, :extra_data => [:first_name, :last_name]

  def get_autocomplete_items(parameters)
    super(parameters).where(:institution_id => params[:institution_id])
  end

My autocomplete list is now scoped to just the users who work for a particular institution.

like image 8
Kevin Avatar answered Oct 19 '22 12:10

Kevin


deb's answer works for me. The code can be cleaned up a bit:

def get_autocomplete_items(parameters)
  super(parameters).where(:user_id => current_user.id)
end
like image 6
Giuseppe Avatar answered Oct 19 '22 12:10

Giuseppe



There is small update to code for those who have having trouble with super method.because of dynamic dispatch it above code need to replaced as below:

def get_autocomplete_items(parameters)
 items = super(parameters)
 items = items.where(searchable: true)
end

to this:

def get_autocomplete_items(parameters)
 items = active_record_get_autocomplete_items(parameters)
 items = items.where(searchable: true)
end

Reference: https://github.com/crowdint/rails3-jquery-autocomplete/issues/278

like image 3
Vaibhav Avatar answered Oct 19 '22 12:10

Vaibhav