0

I have User model and users have they own customers. In rails4-autocomplete I want the user to have autocomplete(:name, :address, :zip, :phone ....) only from they own customers. So I need a condition somewhere in Customer controller where I have :

autocomplete :customer, :customer_name, :extra_data => [:phone, :address, :zip_code, :email]


I've tried in customer model :

default_scope where(:user_id => current_user.id)

but rails4-autocomplete doesn't work anymore.

I have customer.rb

    belongs_to :job, optional: true
    belongs_to :user, optional: true

And user.rb

   has_many :customers

And _customers_fields.html.erb:

<%= f.autocomplete_field :customer_name, autocomplete_customer_customer_name_customers_path, 
      :update_elements => { :id => '#id_element',
                            :phone => '#phone-number',
                            :address => '#address_element',
                            :zip_code => '#zip_code_element',
                            :email => '#email_element'
                          }, class: "form-control", :required => true, placeholder: "Company Name", autocomplete: 'off' %>

ovi_tsb
  • 17
  • 1
  • 8

2 Answers2

0

I've checked the gem and it does not look easy or at least is a bit tricky. There's an undocumented where option that you can use when you declare the autocomplete feature on the controller, but I guess it's evaluated at that moment and you define it, I don't think it can be defined dinamically.

I can think of three options:

1) there's a "scopes" option, so you could define an scope on the model that does where(user_id: <the current user's id>) but you'll need a way to get the current user's id from a model (personally I don't like that, it breakes the idea of MVC) like this Access current_user in model

Pros: it does not patch the gem

Cons: it breaks the MVC patter (but you can do that if you want)

2) add a little 'hacky' override of the get_autocomplete_items function called here https://github.com/peterwillcn/rails4-autocomplete/blob/440a2f0ba6456f87119675b2b703f56dc54dffec/lib/rails4-autocomplete/autocomplete.rb#L54 to add a dimanically generated 'where' option (though it will only work for activerecord, the mongo code on that gem does not use that 'where' option)

# after the `autocomplete :customer, :customer_name.... line
alias_method :original_get_autocomplete_items, :get_autocomplete_items # save the original method so you can call it inside your override method

def get_autocomplete_items(*parameters)
  parameters['options'] ||= {} # in case you have no options defined
  parameters['options']['where'] = "user_id: #{current_user.id}" # add the 'where' option to use that undocumented feature
  original_get_autocomplete_items(parameters) # call the original method
end

Pros: no need to break MVC pattern

Cons: the 'where' option is undocumented and only used on the ActiveRecord method

3) Just override the get_autocomplete_items method defined on your controller by the gem, copy the code from the ORM you are using https://github.com/peterwillcn/rails4-autocomplete/tree/master/lib/rails4-autocomplete/orm and just add the where condition you need

Pros: no need break MVC patter, no need to use an undocumented feature

Cons: you are overriding the whole method, if the gem changes on an update you'll need to check if the custom method works fine with those chenges

arieljuod
  • 15,460
  • 2
  • 25
  • 36
  • Thanks Ariel for your complex answer. I've tried all solution but still not working. Right now I'm still struggling on solution No. 2 but I'm getting a error on server log. TypeError (no implicit conversion of String into Integer): app/controllers/customers_controller.rb:14:in `[]' app/controllers/customers_controller.rb:14:in `get_autocomplete_items' . And I don't understand why. – ovi_tsb Feb 04 '19 at 19:01
  • sorry, try replacing `*parameters` with `parameters` wihout the * – arieljuod Feb 04 '19 at 19:13
  • an also use `*parameters` instead of `parameters` on the call to the original method – arieljuod Feb 04 '19 at 19:13
  • we getting close !!! now it saying " ArgumentError (wrong number of arguments (given 5, expected 1)): " which is normal because I have 5 parameters ( :name, :address, :zip, :email, :phone) but why is expecting 1 argument ? – ovi_tsb Feb 05 '19 at 03:23
0

After couple days of searching I found the solution:

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

maybe somebody else will use this solution in future.

ovi_tsb
  • 17
  • 1
  • 8