Skip to content

Commit

Permalink
Removed trailing whitespace and normalized to use space chars, never …
Browse files Browse the repository at this point in the history
…tabs
  • Loading branch information
amuino committed Mar 7, 2014
1 parent 31305d4 commit 8800b70
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
56 changes: 28 additions & 28 deletions lib/assets/javascripts/rest_in_place/rest_in_place.js.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ class RestInPlaceEditor
@initOptions()
@bindForm()
@createClickHandler()

@$element.click(@clickHandler)

@updateDisplayValue(@$element.text())

# ## Public interface functions ############################################
# Toggle the element associated with this editor from normal to a form field

# Toggle the element associated with this editor from normal to a form field
activate : ->
@oldValue = @elementHTML()
@$element.trigger('activate.rest-in-place')
@$element.addClass('rip-active')
@$element.unbind('click', @clickHandler)
@activateForm()
@$element.trigger('ready.rest-in-place')

# Restore the element to its default state
abort : ->
@$element.trigger('abort.rest-in-place')
@updateDisplayValue(@oldValue)
@$element.removeClass('rip-active')
.click(@clickHandler)

# Take the changes a user has made and send them to the server.
# If the server accepted the request but does not send back a
# parseable answer, a second request is initiated to retrieve the updated
Expand All @@ -40,22 +40,22 @@ class RestInPlaceEditor
updateRequest = @ajax
type : "post"
data : @requestData()

updateRequest.done (data) =>
if data
@loadSuccessCallback(data)
else
@loadViaGET()

updateRequest.fail (jqXHR, textStatus) =>
if (jqXHR.status == 200 && textStatus == "parsererror")
@loadViaGET()
else
@$element.trigger('failure.rest-in-place', jqXHR.responseJSON)
@abort()

@$element.html("saving...")

loadViaGET : ->
showRequest = @ajax()
showRequest.done (data) => @loadSuccessCallback(data)
Expand All @@ -65,11 +65,11 @@ class RestInPlaceEditor
#
# These are not implemented in RestInPlaceEditor. Instead, different form
# types implement this method (and some others). See [Forms](#forms)
# Turns the elements HTML into a form.

# Turns the elements HTML into a form.
activateForm : ->
alert("The form was not properly initialized. activateForm is unbound")

# When the element is active (it is a form), thtis method returns the value
# that should be sent to the server.
getValue : ->
Expand All @@ -86,17 +86,17 @@ class RestInPlaceEditor
#
# These are the options:
#
# **data-url / @url**
# **data-url / @url**
# Where to send/receive data from
#
# **data-formtype / @formType**
# **data-formtype / @formType**
# Which form extension to use (see [Forms](#forms))
#
# **data-object / @objectName**
# **data-object / @objectName**
# Rails singular lowercase name of the class of the objects. This is used
# to generate requests/parse responses that ActionController can understand.
#
# **data-attributes / @attributeName**
# **data-attributes / @attributeName**
# Name of the attribute on the object. Combined with the object name to build
# query string parameters in the form `object[attribute]`, just as Rails
# expects it.
Expand All @@ -121,20 +121,20 @@ class RestInPlaceEditor
bindForm : ->
@activateForm = RestInPlaceEditor.forms[@formType].activateForm
@getValue = RestInPlaceEditor.forms[@formType].getValue

# Generate the data that is sent in the POST request
requestData : ->
data = @getEncodedTokenAuthenticationParams()
data["_method"] = 'put'
data["#{@objectName}[#{@attributeName}]"] = @getValue()
data

# Extract CSRF token from metatags
getEncodedTokenAuthenticationParams : ->
data = {}
param = $('meta[name=csrf-param]').attr('content')
token = $('meta[name=csrf-token]').attr('content')
data[param] = token if param && token
data[param] = token if param && token
data

# A wrapper for jQuery.ajax
Expand All @@ -148,7 +148,7 @@ class RestInPlaceEditor
if @include_root_in_json
data[@objectName][@attributeName]
else
data[@attributeName]
data[@attributeName]

updateDisplayValue : (value) ->
if $.trim(value).length < 1
Expand All @@ -165,15 +165,15 @@ class RestInPlaceEditor
value

# ## Handlers ##############################################################

# Handles the successful response from the server
loadSuccessCallback : (data) ->
@updateDisplayValue(@extractAttributeFromData(data))
@$element.click(@clickHandler)
@$element.removeClass('rip-active')
@$element.trigger('success.rest-in-place', data)


# Creates a clickhandler for the current instance of RestInPlaceEditor, that
# has its this pointer permanently bound to the editor instance.
createClickHandler : ->
Expand All @@ -185,10 +185,10 @@ class RestInPlaceEditor
# provide two methods, `activateForm` and `getValue`, which are transplanted
# into the RestInPlaceEditor instance.
#
# `activateForm` : Turns the elements HTML into a form.
# `activateForm` : Turns the elements HTML into a form.
# `getValue` : When the element is active (it is a form), this method returns
# the value that should be sent to the server.
RestInPlaceEditor.forms =
RestInPlaceEditor.forms =
"input" :
activateForm : ->
value = $.trim(@elementHTML())
Expand All @@ -203,10 +203,10 @@ RestInPlaceEditor.forms =
@$element.find("input").keyup (e) =>
@abort() if e.keyCode == ESC_KEY
@$element.find("input").blur => @abort()

getValue : ->
@$element.find("input").val()

"textarea" :
activateForm : ->
value = $.trim(@elementHTML())
Expand Down
38 changes: 19 additions & 19 deletions testapp/app/assets/javascripts/spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ describe "Setup", ->
it "should find the data-object" , -> expect(rip.objectName).toEqual('person')
it "should find the data-attribute" , -> expect(rip.attributeName).toEqual('name')
it "should find the data-placeholder", -> expect(rip.placeholder).toEqual('Enter name')

it "should prefer inner settings over outer", ->
rip = makeRip """<div data-object="outer"><p data-url="inner"><span>Blubb</span></p></div>"""
expect(rip.url).toEqual('inner')

describe "guessing objectName from Rails", ->
describe 'without parent-provided info', ->
beforeEach -> rip = makeRip """<p id="person_123"><span>Blubb</span></p>"""
it "should derive the objectName from a railslike id", -> expect(rip.objectName).toEqual('person')
describe 'with parent-provided info', ->
beforeEach -> rip = makeRip """<div data-object="customer"><p id="person_123"><span>Blubb</span></p></div>"""
it "should not overwrite the explicit value with the guess", -> expect(rip.objectName).not.toEqual('person')

describe "own data attributes", ->
it "url should default to the current path", ->
rip = makeRip '<p><span>Blubb</span></p>'
Expand Down Expand Up @@ -66,15 +66,15 @@ describe "Server communication", ->
it "should directly access the attribute if include_root_in_json is not set", ->
rip.include_root_in_json = false
expect(rip.extractAttributeFromData(age:12)).toEqual(12)

describe "when sending the update", ->
csrf_metatags = null

beforeEach ->
spyOn(rip, 'getValue').andReturn(111)
csrf_metatags = $('meta[name=csrf-param], meta[name=csrf-token]')
csrf_metatags.remove()
csrf_metatags.remove()

afterEach ->
csrf_metatags.appendTo($('head'))

Expand All @@ -90,7 +90,7 @@ describe "Server communication", ->

it "should not include rails csrf stuff if its not in the HTML", ->
expect(rip.requestData()['authenticity_token']).toBeUndefined()

describe "after updating", ->
jqXHR = null
beforeEach ->
Expand All @@ -99,7 +99,7 @@ describe "Server communication", ->
options.url = @url
options.dataType = "json"
jqXHR

describe "when receiving an empty body", ->
beforeEach ->
spyOn(rip, 'loadViaGET')
Expand All @@ -108,7 +108,7 @@ describe "Server communication", ->
it "should load via get", ->
jqXHR.resolve()
expect(rip.loadViaGET).toHaveBeenCalled()

describe "when receiving a body with data", ->
response = age : 12
beforeEach ->
Expand All @@ -118,7 +118,7 @@ describe "Server communication", ->
it "should load the success callback", ->
jqXHR.resolve(response)
expect(rip.loadSuccessCallback).toHaveBeenCalledWith(response)

describe "when receiving unparseable data", ->
beforeEach ->
spyOn(rip, 'loadViaGET')
Expand All @@ -128,7 +128,7 @@ describe "Server communication", ->
jqXHR.status = 200
jqXHR.reject(jqXHR, 'parsererror')
expect(rip.loadViaGET).toHaveBeenCalled()

describe "when receiving any other error", ->
beforeEach ->
spyOn(rip, 'abort')
Expand All @@ -147,24 +147,24 @@ describe "Server communication", ->
it "should escape the HTML", ->
jqXHR.resolve(response)
expect(rip.$element.html()).toEqual("&lt;strong&gt;&lt;/strong&gt;")


describe "User Interaction", ->
beforeEach ->
rip = makeRip '<p><span data-object="person" data-attribute="age">Blubb</span></p>'

describe "when clicked", ->
it "should be turned rip-active", ->
rip.$element.click()
expect(rip.$element.hasClass('rip-active')).toBe(true)

it "should call activate", ->
spyOn(rip, 'activate')
rip.$element.click()
expect(rip.activate).toHaveBeenCalled()

xit "should remove the click handler"

describe "when aborting", ->
beforeEach ->
rip.activate()
Expand All @@ -178,7 +178,7 @@ describe "jQuery Interface", ->
it "should automatically convert elements with class rest-in-place", ->
rip = $('#autoload-sample').data('restInPlaceEditor')
expect(typeof rip.activate).toEqual("function")

it "should convert jQuery objects with the restInPlace() function ", ->
rip = $('<p><span data-object="person" data-attribute="age">Blubb</span></p>')
.find('span')
Expand Down

0 comments on commit 8800b70

Please sign in to comment.