Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Admin side product search not functioning #1570

Closed
wants to merge 1 commit into from

4 participants

@dandyhat

1) Make a new order from the orders tab in the admin
2) Attempt to search for a SKU
3) Witness it not function

@radar
Collaborator

I can verify this issue and I know the fix for it, however there's another problem after that. I get undefined method serializable_hash for ActiveRecord::Relation when it attempts to include the variants association on the JSON that it is returning. That's a little frustrating.

I don't know how to fix that particular error right now, but will look into it again later on.

@radar
Collaborator

I've turned this issue into a pull request with the fix. If you're game, you can try out the commit in a local copy of Spree and see if you can fix the issue.

@mscottford

I started trying to fix this issue as well before I figured out how to search to see if it was already reported. My research has led me to the conclusion that the serializable_hash error can be removed by changing

        collection.to_json(:include => {:variants => {:include => {:option_values => {:include => :option_type}, 
                                                      :images => {:only => [:id], :methods => :mini_url}}}, 
                                                      :images => {:only => [:id], :methods => :mini_url}, :master => {}})

to

        collection.to_json(:include => {:variants => {:include => {:option_values => {:include => :option_type}, 
                                                      :images => {:only => [:id], :methods => :mini_url}}}, 
                                                      :master => {}})

I was able to write a controller test that causes this error. If you are interested, I can send along a pull request.

I'll try out your patch locally and let you know if it resolves the problem for me.

@mscottford

With your patch, I'm not getting the error, but I'm also not getting any results.

@mscottford

This appears to have been introduced by 4b48e3f. Product#variant_images used to return an array of Image, but now it returns an ActiveRecord::Relation.

Adding .all to the end of the AREL expression fixes the undefined method serializable_hash for ActiveRecord::Relation issue, but it seems to result in a bunch of other test failures.

Any advice?

@mscottford

This is starting to look like a Rails issue. Instances of ActiveRecord::Relation are not passing the is_a?(Enumberable) test on https://github.com/rails/rails/blob/3-2-stable/activemodel/lib/active_model/serialization.rb#L88. But ActiveRecord::Relation will correctly return a valid value for records.map { |a| a.serializable_hash(opts) }.

@ademidov

@mscottford yes, it's possibly a Rails issue.
This is because there are no more association images for Products, while :include option takes only enumerables (association target array is enumerable) and not scopes, which are AR::Relation.

In this case I see two ways.
1. :methods => :images instead of :include => :images
2. Why images is a method and not an association?

    has_many :images, :through => :variants_including_master,
                      :order => "#{Asset.quoted_table_name}.position",
                      :readonly => true

It's much cleaner than find_by_sql, i feel.

@mscottford

Thanks @alekseydem. Adding :methods => images instead of :include => :images does prevent the crash, and results do come back. However, each image in the json response is not in the format expected by the Javascript. It expects the image to look like

      "image":{
        "id":1004,
        "mini_url":"/path/to/image"
      }

but it is coming across as

      "image":{
        "alt":null,
        "attachment_content_type":"image/png",
        "attachment_file_name":"apache_baseball.png",
        "attachment_file_size":141361,
        "attachment_height":484,
        "attachment_updated_at":"2012-05-16T20:27:31Z",
        "attachment_width":504,
        "id":1004,
        "position":1,
        "viewable_id":157039898,
        "viewable_type":"Spree::Variant"
      }

It does not look like Object#to_json supports providing options to the :methods that get called.

@mscottford

I've figured out how to solve this. I'll send a pull request in a few minutes.

@mscottford

Pull request sent. radar#3

@radar radar closed this in ac2c1ac
@joahking joahking referenced this pull request from a commit in joahking/spree
@mscottford mscottford Adds a `serializable_hash` method to the result of `Product#variant_i…
…mages`.

Fixes #1570
20b3043
@tvdeyen tvdeyen referenced this pull request from a commit in magiclabs/spree
@mscottford mscottford Adds a `serializable_hash` method to the result of `Product#variant_i…
…mages`.

Fixes #1570
645bbc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 22, 2012
  1. @radar
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  core/app/assets/javascripts/admin/admin.js.erb
View
2  core/app/assets/javascripts/admin/admin.js.erb
@@ -121,7 +121,7 @@ $.fn.product_autocomplete = function(){
return this.each(function() {
$(this).autocomplete({
source: function(request, response) {
- $.get(ajax_root_prefix() + "admin/products.json?q=" + $('#add_product_name').val() + "&authenticity_token=" + encodeURIComponent($('meta[name=csrf-token]').attr("content")), function(data) {
+ $.get(ajax_root_prefix() + "admin/products.json?q[name_cont]=" + $('#add_product_name').val() + "&authenticity_token=" + encodeURIComponent($('meta[name=csrf-token]').attr("content")), function(data) {
result = prep_product_autocomplete_data(data)
response(result);
});
Something went wrong with that request. Please try again.