Skip to content

Instantly share code, notes, and snippets.

@merqlove
Created June 6, 2014 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save merqlove/fe366728b89fac4f1722 to your computer and use it in GitHub Desktop.
Save merqlove/fe366728b89fac4f1722 to your computer and use it in GitHub Desktop.
Rails engine JS assets route path fix from Spree
#= require jsuri
class window.Spree
@ready: (callback) ->
jQuery(document).ready(callback)
@mountedAt: ->
"<%= Rails.application.routes.url_helpers.spree_path %>"
@pathFor: (path) ->
"#{window.location.origin}#{@mountedAt()}#{path}"
# Helper function to take a URL and add query parameters to it
# Uses the JSUri library from here: https://code.google.com/p/jsuri/
# Thanks to Jake Moffat for the suggestion: https://twitter.com/jakeonrails/statuses/321776992221544449
@url: (uri, query) ->
if uri.path == undefined
uri = new Uri(uri)
if query
$.each query, (key, value) ->
uri.addQueryParam(key, value)
return uri
# Helper method in case people want to call uri rather than url
@uri: (uri, query) ->
url(uri, query)
# This function automatically appends the API token
# for the user to the end of any URL.
# Immediately after, this string is then passed to jQuery.ajax.
#
# ajax works in two ways in jQuery:
#
# $.ajax("url", {settings: 'go here'})
# or:
# $.ajax({url: "url", settings: 'go here'})
#
# This function will support both of these calls.
@ajax: (url_or_settings, settings) ->
if (typeof(url_or_settings) == "string")
$.ajax(Spree.url(url_or_settings).toString(), settings)
else
url = url_or_settings['url']
delete url_or_settings['url']
$.ajax(Spree.url(url).toString(), url_or_settings)
@routes:
states_search: @pathFor('api/states')
apply_coupon_code: (order_id) ->
Spree.pathFor("api/orders/#{order_id}/apply_coupon_code")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment