Skip to content
Browse files

Route and setup js stuff

  • Loading branch information...
1 parent 0a8d5cf commit a21a9ceae0a630d94abaff1be13a8f865b086ad9 @mharris717 committed Dec 4, 2012
Showing with 99 additions and 19 deletions.
  1. +9 −1 lib/edra.rb
  2. +10 −18 lib/edra/model.rb
  3. +25 −0 lib/edra/route.rb
  4. +20 −0 lib/edra/setup.js.coffee
  5. +35 −0 spec/route_spec.rb
View
10 lib/edra.rb
@@ -11,6 +11,14 @@ def present?
end
end
-%w(ext model column association).each do |f|
+module EDRA
+ def self.getSetupJs
+ f = File.dirname(__FILE__) + "/edra/setup.js.coffee"
+ coffee = File.read(f)
+ CoffeeScript.compile coffee
+ end
+end
+
+%w(ext model column association route).each do |f|
load File.expand_path(File.dirname(__FILE__)) + "/edra/#{f}.rb"
end
View
28 lib/edra/model.rb
@@ -10,7 +10,11 @@ def class_name
name.camelize
end
def coll_name
- name.camelize(false) + "s"
+ if name == 'category'
+ "categories"
+ else
+ name.camelize(false) + "s"
+ end
end
def route_name
coll_name
@@ -21,7 +25,9 @@ def to_ember(ops={})
res << to_ember_model(ops) unless ops[:model] == false
res << to_ember_coll if ops[:coll]
res << to_ember_route if ops[:route]
- res.join("\n\n")
+ res = res.join("\n")
+ res = "window.edra.models.add('#{name}',(function() {\n#{res}\n}));" if ops[:function]
+ res
end
def from_ar(ar_cls)
@@ -48,27 +54,13 @@ def to_ember_model(ops={})
def to_ember_coll
"
-App.#{class_name}.reopenClass({url: '#{name}'});
+App.#{class_name}.reopenClass({url: '#{coll_name[0..-2]}'});
App.#{coll_name} = App.store.findAll(App.#{class_name});".strip
end
def to_ember_route
- "
-window.rootRoute.reopen({
- #{route_name}: Ember.Route.extend({
- route: '/#{route_name}',
-
- enter: function(router) {
- console.debug('enter #{route_name}');
- },
-
- connectOutlets: function(router) {
- console.debug('connectOutlets #{route_name}');
- router.get('applicationController').connectOutlet('#{route_name}',App.#{coll_name})
- }
- })
-})".strip
+ Route.new(:model => self).to_ember
end
end
View
25 lib/edra/route.rb
@@ -0,0 +1,25 @@
+module EDRA
+ class Route
+ include FromHash
+ attr_accessor :model
+ fattr(:coll_name) { model.coll_name }
+ fattr(:name) { model.route_name }
+
+ def to_ember
+ "window.edra.routes.add({#{name}: Ember.Route.extend({
+ route: '/#{name}',
+
+ enter: function(router) {
+ console.debug('enter #{name}');
+ },
+
+ connectOutlets: function(router) {
+ console.debug('connectOutlets #{name}');
+ router.get('applicationController').connectOutlet('#{name}',App.#{coll_name})
+ }
+})
+})".strip
+ end
+
+ end
+end
View
20 lib/edra/setup.js.coffee
@@ -0,0 +1,20 @@
+window.edra =
+ routes:
+ list: {}
+ add: (ops) ->
+ for k,v of ops
+ @list[k] = v
+ getAll: -> @list
+
+ models:
+ list: {}
+ add: (name,f) ->
+ @list[name] = f
+ run: ->
+ for name,f of @list
+ console.debug "Setting up #{name}"
+ f()
+
+window.testEdra = ->
+ edra.routes.add(a: "42")
+ console.debug edra.routes.list
View
35 spec/route_spec.rb
@@ -0,0 +1,35 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "Route" do
+ let(:columns) do
+ res = []
+ res << EDRA::Column.new(:name => "title", :type => "string")
+ res << EDRA::Column.new(:name => "author", :type => "string")
+ res << EDRA::Column.new(:name => "year_published", :type => "integer")
+ res
+ end
+ let(:model) do
+ EDRA::Model.new(:name => "book", :columns => columns)
+ end
+ let(:route) do
+ EDRA::Route.new(:model => model)
+ end
+
+ it 'to_ember' do
+ exp = "
+window.edra.routes.add({books: Ember.Route.extend({
+route: '/books',
+
+enter: function(router) {
+ console.debug('enter books');
+},
+
+connectOutlets: function(router) {
+ console.debug('connectOutlets books');
+ router.get('applicationController').connectOutlet('books',App.books)
+}
+})
+})"
+ route.to_ember.should be_equal_lines(exp)
+ end
+end

0 comments on commit a21a9ce

Please sign in to comment.
Something went wrong with that request. Please try again.