pbw 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +8 -8
  2. data/Rakefile +1 -10
  3. data/app/controllers/pbw/{application_controller.rb → base_controller.rb} +1 -3
  4. data/app/controllers/pbw/base_models_controller.rb +13 -19
  5. data/app/controllers/pbw/item_containers_controller.rb +36 -0
  6. data/app/controllers/pbw/passwords_controller.rb +2 -2
  7. data/app/controllers/pbw/registrations_controller.rb +5 -3
  8. data/app/controllers/pbw/sessions_controller.rb +6 -3
  9. data/app/controllers/pbw/tokens_controller.rb +4 -0
  10. data/app/models/pbw/ability.rb +3 -3
  11. data/app/models/pbw/attached_process.rb +2 -0
  12. data/app/models/pbw/capability.rb +1 -1
  13. data/app/models/pbw/constraint.rb +1 -1
  14. data/app/models/pbw/trigger.rb +1 -1
  15. data/app/models/pbw/user.rb +3 -3
  16. data/config/initializers/mongoid_accessible_attributes.rb +14 -0
  17. data/config/routes.rb +15 -6
  18. data/lib/generators/pbw/area/area_generator.rb +4 -0
  19. data/lib/generators/pbw/install/install_generator.rb +1 -1
  20. data/lib/generators/pbw/item/item_generator.rb +4 -0
  21. data/lib/generators/pbw/resource_helpers.rb +4 -0
  22. data/lib/generators/pbw/rules/command/command_generator.rb +10 -0
  23. data/lib/generators/pbw/scaffold_generator.rb +15 -12
  24. data/lib/generators/pbw/templates/index.erb +1 -0
  25. data/lib/generators/pbw/templates/model.coffee +5 -5
  26. data/lib/generators/pbw/templates/pbw.coffee +18 -9
  27. data/lib/generators/pbw/templates/router.coffee +9 -0
  28. data/lib/generators/pbw/templates/templates/edit.jst +19 -4
  29. data/lib/generators/pbw/templates/templates/home.jst +2 -1
  30. data/lib/generators/pbw/templates/templates/index.jst +21 -10
  31. data/lib/generators/pbw/templates/templates/model.jst +15 -2
  32. data/lib/generators/pbw/templates/templates/new.jst +20 -4
  33. data/lib/generators/pbw/templates/templates/show.jst +12 -3
  34. data/lib/generators/pbw/templates/user_recovery.coffee +0 -1
  35. data/lib/generators/pbw/templates/user_registration.coffee +0 -1
  36. data/lib/generators/pbw/templates/user_session.coffee +0 -1
  37. data/lib/generators/pbw/templates/views/edit_view.coffee +19 -8
  38. data/lib/generators/pbw/templates/views/index_view.coffee +10 -2
  39. data/lib/generators/pbw/templates/views/login_view.coffee +20 -8
  40. data/lib/generators/pbw/templates/views/new_view.coffee +13 -5
  41. data/lib/generators/pbw/templates/views/recovery_view.coffee +8 -3
  42. data/lib/generators/pbw/templates/views/show_view.coffee +11 -1
  43. data/lib/generators/pbw/templates/views/signup_view.coffee +19 -5
  44. data/lib/generators/pbw/token/token_generator.rb +4 -0
  45. data/lib/pbw/engine.rb +1 -0
  46. data/lib/pbw/version.rb +1 -1
  47. data/vendor/assets/javascripts/Backbone.CollectionBinder.js +281 -0
  48. data/vendor/assets/javascripts/Backbone.ModelBinder.js +576 -0
  49. metadata +6 -6
  50. data/app/controllers/pbw/item_conversions_controller.rb +0 -7
  51. data/vendor/assets/javascripts/backbone_datalink.js +0 -21
  52. data/vendor/assets/javascripts/backbone_rails_sync.js +0 -81
@@ -1,6 +1,7 @@
1
1
  <script type="text/javascript">
2
2
  $(function() {
3
3
  window.router = new <%= router_name %>;
4
+ window.<%=js_app_name%>.User = <%%=raw (current_user.to_json)%>;
4
5
  Backbone.history.start();
5
6
  });
6
7
  </script>
@@ -1,14 +1,14 @@
1
1
  class <%= js_model_namespace %> extends Backbone.Model
2
- paramRoot: '<%= singular_table_name %>'
3
- urlRoot: '/pbw/<%=model_namespace.downcase%>/<%= class_name%>'
2
+ urlRoot: '/pbw/<%=model_namespace.downcase%>/<%= class_namespace %>'
4
3
 
5
4
  defaults:
6
- name: null
7
- _type: '<%= class_name%>'
5
+ <% default_attributes.each do |attribute| -%>
6
+ <%= attribute[:name] %>: '<%=attribute[:default_value]%>'
7
+ <% end -%>
8
8
  <% attributes.each do |attribute| -%>
9
9
  <%= attribute.name %>: null
10
10
  <% end -%>
11
11
 
12
12
  class <%= collection_namespace %>Collection extends Backbone.Collection
13
13
  model: <%= js_model_namespace %>
14
- url: '/pbw/<%=model_namespace.downcase%>/<%= class_name%>'
14
+ url: '/pbw/<%=model_namespace.downcase%>/<%= class_namespace %>'
@@ -8,14 +8,23 @@
8
8
  @current_user = ->
9
9
  window.<%= js_app_name %>.User
10
10
 
11
- # Form error handling
12
- @form_errors = (message, jqXHR) ->
11
+ @is_admin = ->
12
+ current_user() && (current_user().role == 'superadmin' || current_user().role == 'admin')
13
+
14
+ # Error handling
15
+ @display_errors = (message, jqXHR, backlink) ->
13
16
  debug message
14
17
  debug jqXHR
15
- $("#error").html('<h3>' + message + '</h3><ul>')
16
- if jqXHR && jqXHR.responseText
17
- _.each($.parseJSON(jqXHR.responseText), (value,key) ->
18
- $("#error").append('<li>' + value + '</li>')
19
- )
20
- $("#error").append('</ul>')
21
- $("#error").show()
18
+ if jqXHR && jqXHR.status == 401
19
+ if backlink
20
+ window.<%=js_app_name%>.backlink = backlink
21
+ window.location.hash = '/login'
22
+
23
+ _.defer =>
24
+ $("#error").html('<h3>' + message + '</h3><ul>')
25
+ if jqXHR && jqXHR.responseText
26
+ _.each($.parseJSON(jqXHR.responseText), (value,key) ->
27
+ $("#error").append('<li>' + value + '</li>')
28
+ )
29
+ $("#error").append('</ul>')
30
+ $("#error").show()
@@ -5,6 +5,7 @@ class <%= router_name %> extends Backbone.Router
5
5
  routes:
6
6
  "" : "home"
7
7
  "login" : "login"
8
+ "logout" : "logout"
8
9
  "signup" : "signup"
9
10
  "password_recovery" : "passwordRecovery"
10
11
 
@@ -17,6 +18,14 @@ class <%= router_name %> extends Backbone.Router
17
18
  @view = new <%= "#{js_app_name}.Views.Users.LoginView" %>
18
19
  $("#app").html(@view.render().el)
19
20
 
21
+ logout: ->
22
+ $.ajax
23
+ url: '/pbw/users/sign_out.json'
24
+ type: 'DELETE'
25
+ complete: ->
26
+ window.<%= js_app_name %>.User = null
27
+ window.location.hash = "/"
28
+
20
29
  signup: ->
21
30
  @view = new <%= "#{js_app_name}.Views.Users.SignupView" %>
22
31
  $("#app").html(@view.render().el)
@@ -3,14 +3,25 @@
3
3
  <input type="hidden" name="authenticity_token" value="<%%= $("meta[name='csrf-token']").attr("content") %>">
4
4
  <fieldset>
5
5
  <legend>Edit <%= singular_table_name %></legend>
6
+ <% default_attributes.each do |attribute| -%>
7
+ <% unless attribute[:name].start_with?('_') %>
6
8
  <div class="field">
7
- <label for="name">Name:</label>
8
- <input type="text" name="name" id="name" value="<%%= name %>" >
9
+ <label for="<%= attribute[:name] %>"> <%= attribute[:name].capitalize %>:</label>
10
+ <input type="text" name="<%= attribute[:name] %>" id="<%= attribute[:name] %>" value="<%%= <%= attribute[:name] %> %>" >
9
11
  </div>
12
+ <% end %>
13
+ <% end -%>
10
14
  <% attributes.each do |attribute| -%>
11
15
  <div class="field">
12
16
  <label for="<%= attribute.name %>"> <%= attribute.human_name %>:</label>
13
- <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" >
17
+ <% case "#{attribute.type}".underscore.to_sym
18
+ when :boolean %>
19
+ <input type="checkbox" name="<%= attribute.name %>" id="<%= attribute.name %>" data-type="<%=attribute.type%>">
20
+ <% when :integer %>
21
+ <input type="number" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" data-type="<%=attribute.type%>">
22
+ <% else %>
23
+ <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" data-type="<%=attribute.type%>">
24
+ <% end %>
14
25
  </div>
15
26
 
16
27
  <% end -%>
@@ -20,4 +31,8 @@
20
31
  </fieldset>
21
32
  </form>
22
33
 
23
- <a href="#/<%=plural_model_name%>">Back</a>
34
+ <%% if(is_admin()){ %>
35
+ <a href="#/<%=plural_model_name%>">Back</a>
36
+ <%% }else { %>
37
+ <a href="#/">Back</a>
38
+ <%% } %>
@@ -1,7 +1,8 @@
1
1
  <nav>
2
2
  <ul>
3
3
  <%% if(current_user()) { %>
4
- <li><a href="/pbw/users/sign_out" data-method="delete">Sign out</a></li>
4
+ <li>Hi <%%= current_user().name %></li>
5
+ <li><a href="#/logout">Sign out</a></li>
5
6
  <%% } else { %>
6
7
  <li><a href="#/signup">Sign up</a></li>
7
8
  <li><a href="#/login">Login</a></li>
@@ -1,15 +1,26 @@
1
+ <div id="error"></div>
1
2
  <table id="<%= plural_name %>-table">
2
3
  <caption>Listing <%= plural_table_name %></caption>
3
- <tr>
4
- <th>Name</th>
5
- <% attributes.each do |attribute| -%>
6
- <th><%= attribute.human_name %></th>
7
- <% end -%>
8
- <th></th>
9
- <th></th>
10
- </tr>
4
+ <thead>
5
+ <tr>
6
+ <% default_attributes.each do |attribute| -%>
7
+ <% unless attribute[:name].start_with?('_') %>
8
+ <th><%=attribute[:name].capitalize%></th>
9
+ <% end %>
10
+ <% end -%>
11
+ <% attributes.each do |attribute| -%>
12
+ <th><%= attribute.human_name %></th>
13
+ <% end -%>
14
+ <th></th>
15
+ <th></th>
16
+ </tr>
17
+ </thead>
18
+ <tbody></tbody>
11
19
  </table>
12
20
 
13
21
  <br/>
14
-
15
- <a href="#/<%=plural_model_name%>/new">New <%= human_name %></a>
22
+ <%% if(is_admin()){ %>
23
+ <a href="#/<%=plural_model_name%>/new">New <%= human_name %></a>
24
+ <%% }else { %>
25
+ <a href="#/">Back</a>
26
+ <%% } %>
@@ -1,7 +1,20 @@
1
1
  <td><a href="#/<%=plural_model_name%>/<%%= _id %>"><%%= name %></a></td>
2
+ <% default_attributes.each do |attribute| -%>
3
+ <% unless attribute[:name].start_with?('_') || attribute[:name] == 'name' %>
4
+ <td><%%= <%= attribute[:name] %> %></td>
5
+ <% end %>
6
+ <% end -%>
2
7
  <% attributes.each do |attribute| -%>
3
8
  <td><%%= <%= attribute.name %> %></td>
4
9
  <% end -%>
5
10
 
6
- <td><a href="#/<%=plural_model_name%>/<%%= _id %>/edit">Edit</td>
7
- <td><a href="#/<%=plural_model_name%>/<%%= _id %>/destroy" class="destroy">Delete</a></td>
11
+ <td>
12
+ <%% if(is_admin()){ %>
13
+ <a href="#/<%=plural_model_name%>/<%%= _id %>/edit">Edit</a>
14
+ <%% } %>
15
+ </td>
16
+ <td>
17
+ <%% if(is_admin()){ %>
18
+ <a href="#/<%=plural_model_name%>/<%%= _id %>/destroy" class="destroy">Delete</a>
19
+ <%% } %>
20
+ </td>
@@ -3,14 +3,26 @@
3
3
  <input type="hidden" name="authenticity_token" value="<%%= $("meta[name='csrf-token']").attr("content") %>">
4
4
  <fieldset>
5
5
  <legend>New <%= singular_table_name %></legend>
6
+ <% default_attributes.each do |attribute| -%>
7
+ <% unless attribute[:name].start_with?('_') %>
6
8
  <div class="field">
7
- <label for="name">Name:</label>
8
- <input type="text" name="name" id="name" value="<%%= name %>" >
9
+ <label for="<%= attribute[:name] %>"> <%= attribute[:name].capitalize %>:</label>
10
+ <input type="text" name="<%= attribute[:name] %>" id="<%= attribute[:name] %>" value="<%%= <%= attribute[:name] %> %>" >
9
11
  </div>
12
+ <% end %>
13
+
14
+ <% end -%>
10
15
  <% attributes.each do |attribute| -%>
11
16
  <div class="field">
12
17
  <label for="<%= attribute.name %>"> <%= attribute.human_name %>:</label>
13
- <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" >
18
+ <% case "#{attribute.type}".underscore.to_sym
19
+ when :boolean %>
20
+ <input type="checkbox" name="<%= attribute.name %>" id="<%= attribute.name %>" data-type="<%=attribute.type%>">
21
+ <% when :integer %>
22
+ <input type="number" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" data-type="<%=attribute.type%>">
23
+ <% else %>
24
+ <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" data-type="<%=attribute.type%>">
25
+ <% end %>
14
26
  </div>
15
27
 
16
28
  <% end -%>
@@ -20,4 +32,8 @@
20
32
  </fieldset>
21
33
  </form>
22
34
 
23
- <a href="#/<%=plural_model_name%>">Back</a>
35
+ <%% if(is_admin()){ %>
36
+ <a href="#/<%=plural_model_name%>">Back</a>
37
+ <%% }else { %>
38
+ <a href="#/">Back</a>
39
+ <%% } %>
@@ -1,9 +1,14 @@
1
+ <div id="error"></div>
1
2
  <table id="<%= singular_name %>-table">
2
3
  <tbody>
4
+ <% default_attributes.each do |attribute| -%>
5
+ <% unless attribute[:name].start_with?('_') %>
3
6
  <tr>
4
- <th>Name:</th>
5
- <td><%%= name %></td>
7
+ <th><%= attribute[:name] %>:</th>
8
+ <td><%%= <%= attribute[:name] %> %></td>
6
9
  </tr>
10
+ <% end %>
11
+ <% end -%>
7
12
  <% attributes.each do |attribute| -%>
8
13
  <tr>
9
14
  <th><%= attribute.human_name %>:</th>
@@ -13,4 +18,8 @@
13
18
 
14
19
  </tbody>
15
20
  </table>
16
- <a href="#/<%=plural_model_name%>">Back</a>
21
+ <%% if(is_admin()){ %>
22
+ <a href="#/<%=plural_model_name%>">Back</a>
23
+ <%% }else { %>
24
+ <a href="#/">Back</a>
25
+ <%% } %>
@@ -1,5 +1,4 @@
1
1
  class <%= js_user_model_namespace %>Recovery extends Backbone.Model
2
- paramRoot: 'user'
3
2
  url: '/pbw/users/password.json'
4
3
 
5
4
  defaults:
@@ -1,5 +1,4 @@
1
1
  class <%= js_user_model_namespace %>Registration extends Backbone.Model
2
- paramRoot: 'user'
3
2
  url: '/pbw/users.json'
4
3
 
5
4
  defaults:
@@ -1,5 +1,4 @@
1
1
  class <%= js_user_model_namespace %>Session extends Backbone.Model
2
- paramRoot: 'user'
3
2
  url: '/pbw/users/sign_in.json'
4
3
 
5
4
  defaults:
@@ -9,15 +9,24 @@ class <%= view_namespace %>.EditView extends Backbone.View
9
9
  constructor: (options) ->
10
10
  super(options)
11
11
  @model = options.model
12
-
12
+
13
13
  @model.bind("change:errors", () =>
14
14
  this.render()
15
15
  )
16
16
 
17
17
  @model.bind("error", (model, xhr, options) =>
18
- form_errors 'There was a problem saving <%= singular_name %>', xhr
18
+ display_errors 'There was a problem saving <%= singular_name %>', xhr, "/<%=plural_model_name%>/#{@model.id}/edit"
19
19
  )
20
20
 
21
+ initialize: ->
22
+ @_modelBinder = new Backbone.ModelBinder
23
+ @bindings = <% default_attributes.each do |attribute| -%><% unless attribute[:name].start_with?('_') %>
24
+ <%= attribute[:name] %> : '[name=<%= attribute[:name] %>]'
25
+ <% end %><% end %><% attributes.each do |attribute| -%>
26
+ <%= attribute.name %> : '[name=<%= attribute.name %>]'
27
+ <% end %>
28
+
29
+
21
30
  update: (e) ->
22
31
  e.preventDefault()
23
32
  e.stopPropagation()
@@ -25,14 +34,16 @@ class <%= view_namespace %>.EditView extends Backbone.View
25
34
  @model.save(null,
26
35
  success: (<%= singular_name %>, response, options) =>
27
36
  @model = <%= singular_name %>
28
- window.location.hash = "/<%=model_namespace.downcase%>/#{@model.id}"
37
+ window.location.hash = "/<%=plural_model_name%>/#{@model.id}"
29
38
  error: (model, xhr, options) =>
30
- form_errors 'There was a problem saving <%= singular_name %>', xhr
39
+ display_errors 'There was a problem saving <%= singular_name %>', xhr
31
40
  )
32
41
 
33
42
  render: ->
34
- @$el.html(@template(@model.toJSON() ))
35
-
36
- this.$("form").backboneLink(@model)
37
-
43
+ @model.fetch
44
+ success: (model) =>
45
+ @$el.html(@template(model.toJSON() ))
46
+ @_modelBinder.bind(@model, @el, @bindings)
47
+ error: (model, response) ->
48
+ debug response
38
49
  return this
@@ -5,16 +5,24 @@ class <%= view_namespace %>.IndexView extends Backbone.View
5
5
 
6
6
  initialize: () ->
7
7
  @options.<%= plural_model_name %>.bind('reset', @addAll)
8
+ @options.<%= plural_model_name %>.bind("error", (model, xhr, options) =>
9
+ display_errors '', xhr, "/<%=plural_model_name%>"
10
+ @$("#<%=plural_model_name%>-table").hide()
11
+ )
8
12
 
9
13
  addAll: () =>
10
- @options.<%= plural_model_name %>.each(@addOne)
14
+ @options.<%= plural_model_name %>.fetch
15
+ success: (collection) =>
16
+ collection.each(@addOne)
17
+ error: (collection, response) ->
18
+ debug response
11
19
 
12
20
  addOne: (<%= singular_model_name %>) =>
13
21
  view = new <%= view_namespace %>.<%= singular_name.camelize %>View({model : <%= singular_model_name %>})
14
22
  @$("tbody").append(view.render().el)
15
23
 
16
24
  render: =>
17
- @$el.html(@template(<%= plural_model_name %>: @options.<%= plural_model_name %>.toJSON() ))
25
+ @$el.html(@template())
18
26
  @addAll()
19
27
 
20
28
  return this
@@ -9,31 +9,43 @@ class <%= user_view_namespace %>.LoginView extends Backbone.View
9
9
  constructor: (options) ->
10
10
  super(options)
11
11
  @model = new <%= js_user_model_namespace %>Session
12
-
12
+
13
13
  @model.bind("change:errors", () =>
14
14
  this.render()
15
15
  )
16
16
 
17
17
  @model.bind("error", (model, xhr, options) =>
18
- form_errors 'There was a problem logging in', xhr
18
+ display_errors 'There was a problem logging in', xhr
19
+ )
20
+
21
+ @model.bind("sync", (model, xhr, options) =>
22
+ window.<%=js_app_name%>.User = xhr
19
23
  )
20
24
 
25
+ initialize: ->
26
+ @_modelBinder = new Backbone.ModelBinder
27
+ @bindings =
28
+ email: '[name=email]'
29
+ password: '[name=password]'
30
+ remember_me: '[name=remember_me]'
31
+
21
32
  save: (e) ->
22
33
  e.preventDefault()
23
34
  e.stopPropagation()
24
35
 
25
36
  @model.unset("errors")
26
37
 
27
- @model.save(@model.attributes,
28
- success: (user, response, options) =>
29
- @model = user
30
- window.<%= js_app_name %>.User = @model
31
- window.location.hash = "/"
38
+ @model.save(null,
39
+ success: ->
40
+ if window.<%=js_app_name%>.backlink
41
+ window.location.hash = window.<%=js_app_name%>.backlink
42
+ else
43
+ window.location.hash = "/"
32
44
  )
33
45
 
34
46
  render: ->
35
47
  @$el.html(@template(@model.toJSON() ))
36
48
 
37
- this.$("form").backboneLink(@model)
49
+ @_modelBinder.bind(@model, @el, @bindings)
38
50
 
39
51
  return this
@@ -8,31 +8,39 @@ class <%= view_namespace %>.NewView extends Backbone.View
8
8
 
9
9
  constructor: (options) ->
10
10
  super(options)
11
- @model = new @collection.model()
11
+ @model = options.model
12
12
 
13
13
  @model.bind("change:errors", () =>
14
14
  this.render()
15
15
  )
16
16
 
17
17
  @model.bind("error", (model, xhr, options) =>
18
- form_errors 'There was a problem saving <%= singular_name %>', xhr
18
+ display_errors 'There was a problem saving <%= singular_name %>', xhr, "/<%=plural_model_name%>/new"
19
19
  )
20
20
 
21
+ initialize: ->
22
+ @_modelBinder = new Backbone.ModelBinder
23
+ @bindings = <% default_attributes.each do |attribute| -%><% unless attribute[:name].start_with?('_') %>
24
+ <%= attribute[:name] %> : '[name=<%= attribute[:name] %>]'
25
+ <% end %><% end %><% attributes.each do |attribute| -%>
26
+ <%= attribute.name %> : '[name=<%= attribute.name %>]'
27
+ <% end %>
28
+
21
29
  save: (e) ->
22
30
  e.preventDefault()
23
31
  e.stopPropagation()
24
32
 
25
33
  @model.unset("errors")
26
34
 
27
- @collection.create(@model.toJSON(),
35
+ @model.save(null,
28
36
  success: (<%= singular_name %>, response, options) =>
29
37
  @model = <%= singular_name %>
30
- window.location.hash = "/<%=model_namespace.downcase%>/#{@model.id}"
38
+ window.location.hash = "/<%=plural_model_name%>/#{@model.id}"
31
39
  )
32
40
 
33
41
  render: ->
34
42
  @$el.html(@template(@model.toJSON() ))
35
43
 
36
- this.$("form").backboneLink(@model)
44
+ @_modelBinder.bind(@model, @el, @bindings)
37
45
 
38
46
  return this