Skip to content

Instantly share code, notes, and snippets.

@braintreeps
Created November 8, 2011 23:15
Show Gist options
  • Save braintreeps/1349643 to your computer and use it in GitHub Desktop.
Save braintreeps/1349643 to your computer and use it in GitHub Desktop.
diff --git a/app/assets/javascripts/application.js.coffee.erb b/app/assets/javascripts/application.js.coffee.erb
index 1754c77..29574ac 100644
--- a/app/assets/javascripts/application.js.coffee.erb
+++ b/app/assets/javascripts/application.js.coffee.erb
@@ -6,22 +6,26 @@
#
#= require jquery
#= require jquery_ujs
+#= require_self
#= require_tree .
<% self.class.send :include, Rails.application.routes.url_helpers %>
-class Login
- @controller = {}
- @addController: (controller) ->
- @controllers[controller.name] = controller
+class @Application
+ @controllers = {}
- @findAction: (controller_name, action_name) ->
- @controller[controller][action_name]
+ @registerController: (controller) ->
+ @controllers[controller.controllerName] = controller
+
+ @callAction: (controllerName, actionName) ->
+ @controllers[controllerName]?[actionName]()
@init: (controllerName, actionName) ->
- Login.findAction controllerName, actionName
+ @callAction(controllerName, actionName)
-jQuery.ready ->
+$ ->
+ body = document.body
controllerName = body.getAttribute("data-controller")
actionName = body.getAttribute("data-action")
- Login.init controllerName, actionName
+
+ Application.init controllerName, actionName
diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb
deleted file mode 100644
index 71696b1..0000000
--- a/app/assets/javascripts/application.js.erb
+++ /dev/null
@@ -1,11 +0,0 @@
-// This is a manifest file that'll be compiled into including all the files listed below.
-// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
-// be included in the compiled file accessible from http://example.com/assets/application.js
-// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
-// the compiled file.
-//
-//= require jquery
-//= require jquery_ujs
-//= require_tree .
-
-<% self.class.send :include, Rails.application.routes.url_helpers %>
diff --git a/app/assets/javascripts/controllers/users.js.coffee b/app/assets/javascripts/controllers/users.js.coffee
index 4429362..8edfe89 100644
--- a/app/assets/javascripts/controllers/users.js.coffee
+++ b/app/assets/javascripts/controllers/users.js.coffee
@@ -1,7 +1,7 @@
class UsersController
- @name: "users"
+ @controllerName = "users"
- @signup: ->
+ @new: ->
console.log("user#signup")
-Login.addController(UsersController)
+Application.registerController(UsersController)
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index c99b55f..85e93fa 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -15,7 +15,7 @@
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
-<body class="no-nav">
+<body class="no-nav" data-controller="<%= controller.controller_name %>" data-action="<%= controller.action_name %>">
<div id="wrap">
<header id="app-north">
diff --git a/spec/javascripts/application_spec.js.coffee b/spec/javascripts/application_spec.js.coffee
new file mode 100644
index 0000000..acefe2c
--- /dev/null
+++ b/spec/javascripts/application_spec.js.coffee
@@ -0,0 +1,26 @@
+#= require 'application'
+
+describe "Application", ->
+ it 'registers a controller', ->
+ class FakesController
+ @controllerName = 'fakes'
+ Application.registerController FakesController
+ expect(Application.controllers['fakes']).toBe(FakesController)
+
+ it 'calls action', ->
+ class FakesController
+ @controllerName = 'fakes'
+ @callMe: -> "my fancy return value"
+
+ Application.registerController FakesController
+ expect(Application.callAction("fakes", "callMe")).toEqual("my fancy return value")
+
+ it 'inits a controller', ->
+ class FakesController
+ @controllerName = 'fakes'
+ @callMeWasCalled = false
+ @callMe: -> @callMeWasCalled = true
+
+ Application.registerController FakesController
+ Application.init("fakes", "callMe")
+ expect(FakesController.callMeWasCalled).toBeTruthy()
diff --git a/spec/javascripts/signup_spec.js.coffee b/spec/javascripts/signup_spec.js.coffee
index 2c27798..1080d97 100644
--- a/spec/javascripts/signup_spec.js.coffee
+++ b/spec/javascripts/signup_spec.js.coffee
@@ -10,9 +10,3 @@ describe "Signup", ->
console.log $('#password').parent().html()
expect($('span.status')).toHaveText('X')
-
- # it 'invalid if password is not strong enough', ->
- # loadFixtures "signup"
- # $('#password').val = 'abcdefg'
- # $('#password').blur()
- # expect($('.valid')).toNotHaveText('X')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment