Modify Assets using Shopify gem on rails 3.1.0

304 views
Skip to first unread message

nAv

unread,
Oct 5, 2011, 3:07:16 PM10/5/11
to shopify-api
Hey everyone,
I'm relatively new to ruby so bare with me. I found my way around the
Shopify gem to retrieve assets; but when I try to create/update/delete
an asset, nothing seems to happen.

For example, I created a "test.txt" under the assets/ folder using the
theme editor.

Then I do something like:

test_asset = ShopifyAPI::Asset.find('assets/test.txt', :params =>
{:theme_id=>theme_id})
# I can check the test_asset.value, and see the asset is successfully
found
test_asset.destroy

I then go on to theme editor, refresh the page, and the test.txt file
is still there

I dumped the test_asset.destroy response, and it looks good:

--- !ruby/object:Net::HTTPOK
http_version: '1.1'
code: '200'
message: OK
header:
...


Same story with updates:
test_asset = ShopifyAPI::Asset.find('assets/test.txt', :params =>
{:theme_id=>theme_id})
test_asset.value = "this is a test UPDATED"
test_asset.save

(.save returns "true")


Any ideas as to why this isn't working?

nAv

unread,
Oct 5, 2011, 7:33:10 PM10/5/11
to shopify-api
ALRIGHT!
After sitting here forever, wrestling with the api gem, httparty, net/
https; I seem to have hacked it together!
Clearly some strange bugs w.r.t ActiveResource going on here.

This fails:
test_asset = ShopifyAPI::Asset.find('assets/test.txt', :params =>
{:theme_id=>theme_id})
test_asset.value = "this is an UPDATE"
test_asset.save

This works:
test_prod = ShopifyAPI::Asset.all(:params =>
{:theme_id=>theme_id}).find{|asset| asset.attributes[:key] == "assets/
test.txt"}
test_asset.value = "this is an UPDATE"
test_asset.save

(same with test_asset.destroy)

To create, the .create(...) fails
This works:
test_prod =
ShopifyAPI::Asset.new(:theme_id=>theme_id, :key=>"assets/test2.txt")
test_prod.save

hahhhhhhh

Travis Haynes

unread,
Oct 5, 2011, 7:58:44 PM10/5/11
to shopi...@googlegroups.com
Something to keep in mind with the shopify_api gem is that it uses ActiveResource, and not ActiveRecord, which is what you're used to with most Rails apps. I was getting confused in a lot of places until I realized that, then I read up on ActiveResource here: http://api.rubyonrails.org/classes/ActiveResource/Base.html

If you read the ActiveResource::Base API for the find method, you'll see that the proper way to do this using find would be:

ShopifyAPI::Asset.find(:first, :params => { :theme_id => theme_id })

That was the missing piece of the puzzle for you here. If you look at the code for ActiveResource::Base.all, it is simply this:

def all(*args)
  find(:all, *args)
end

Hope that helps you out in the future! It sure helped me out once I realized that with ShopifyAPI I was dealing with ActiveResource, and not ActiveRecord.

nAv

unread,
Oct 6, 2011, 12:31:31 AM10/6/11
to shopify-api
Hey Travis, thanks a ton for the response. I'm reading up on
ActiveResource now. (like I said, still new to rails)

It still doesn't seem to explain why this doesn't NOT work:
method1 = ShopifyAPI::Asset.find("assets/test.txt",:params =>
{:theme_id => theme_id})
method1.value = "METHOD 1!"
method1.save

while this DOES:
method2 = ShopifyAPI::Asset.all(:params =>
{:theme_id=>theme_id}).find{|asset| asset.attributes[:key] == "assets/
test.txt"}
method2.value = "METHOD 2!"
method2.save


AHA! (writing this as I code)
It looks like my little hack just worked around a bug. I dumped the
two objects that are returned:

METHOD 1:
--- !ruby/object:ShopifyAPI::Asset
attributes:
created_at: 2011-10-05 18:26:54.000000000Z
updated_at: 2011-10-06 04:18:53.000000000Z
public_url: http://static.shopify.com/s/files/1/0102/3992/t/2/assets/test.txt?398
value: this is a test
key: assets/test.txt
prefix_options: {}
persisted: true

METHOD 2:
--- !ruby/object:ShopifyAPI::Asset
attributes:
created_at: 2011-10-05 18:26:54.000000000Z
updated_at: 2011-10-06 04:18:53.000000000Z
public_url: http://static.shopify.com/s/files/1/0102/3992/t/2/assets/test.txt?398
key: assets/test.txt
prefix_options:
:theme_id: '1931092'
persisted: true

looks like the "prefix_options" are missing on method1! this works!

method1 = ShopifyAPI::Asset.find("assets/test.txt",:params =>
{:theme_id => theme_id})
method1.value = "METHOD 1!"
method1.prefix_options[:theme_id] = theme_id
method1.save
Reply all
Reply to author
Forward
0 new messages