Skip to content
Browse files

moved generating validation metadata into a rake task

  • Loading branch information...
1 parent 31bc345 commit d0aac884e3574eae7a04789c17956a5b7d57aa84 Chris Nelson committed Oct 11, 2012
View
2 example/app/assets/javascripts/backbone/example.js.coffee
@@ -14,4 +14,4 @@ window.Example =
$ ->
new Example.Routers.PeopleRouter()
Backbone.history.start()
- Backtastic.Rails.applyValidations(Example.Models)
+ Backtastic.applyValidations(Example.Models, Backtastic.Rails.validations)
View
8 example/spec/javascripts/models/person_spec.coffee
@@ -8,21 +8,21 @@ describe "Person", ->
@validationErrors = errors
describe "failing validation", ->
beforeEach ->
- @person.set first_name: "", last_name: "NotSmith"
+ @person.set first_name: "", last_name: "NotJones"
it "should have triggered the error event for first name", ->
expect(@validationErrors.first_name.length).toEqual 1
it "should have triggered the error event for last name", ->
expect(@validationErrors.last_name.length).toEqual 1
describe "with only one attribute", ->
beforeEach ->
- @person.set last_name: "Smith"
+ @person.set last_name: "Jones"
it "should only validate attributes that being set", ->
expect(@person.isValid()).toBeTruthy()
it "should have set the last name", ->
- expect(@person.get("last_name")).toEqual "Smith"
+ expect(@person.get("last_name")).toEqual "Jones"
describe "with valid attributes", ->
beforeEach ->
- @person.set first_name: "Fred", last_name: "Smith"
+ @person.set first_name: "Fred", last_name: "Jones"
it "should be valid", ->
expect(@person.isValid()).toTruthy
it "should have no errors", ->
View
2 example/spec/javascripts/views/edit_person_view_spec.coffee
@@ -19,7 +19,7 @@ describe "EditPersonView", ->
beforeEach ->
jasmine.Ajax.useMock()
@editPersonView.$("input[name='first_name']").val("Bob")
- @editPersonView.$("input[name='last_name']").val("Smith")
+ @editPersonView.$("input[name='last_name']").val("Jones")
@editPersonView.save(new jQuery.Event)
@request = mostRecentAjaxRequest()
it "disables the save button", ->
View
14 example/spec/javascripts/views/text_field_view_spec.coffee
@@ -15,7 +15,7 @@ describe "text field view", ->
it "puts a label on it", ->
expect(@textFieldView.$("label[for=name]")).toExist()
expect(@textFieldView.$("label[for=name]")).toHaveText /Name/
-
+
describe "updating model", ->
beforeEach ->
@textFieldView.$("input").val("Not bob")
@@ -30,11 +30,19 @@ describe "text field view", ->
expect(@textFieldView.$el).toHaveClass "error"
it "should add the error message", ->
expect(@textFieldView.$("span.help-inline")).toHaveText "totally borked"
+ describe "and then setting model attribute to something valid", ->
+ beforeEach ->
+ @model.set name: "Wut"
+ it "should remove error class", ->
+ expect(@textFieldView.$el).not.toHaveClass "error"
+ it "should remove the error message", ->
+ expect(@textFieldView.$("span.help-inline")).not.toExist()
+
describe "again", ->
beforeEach ->
@textFieldView.displayErrors
name: ["totally borked"]
it "should only display it once", ->
expect(@textFieldView.$("span.help-inline").size()).toEqual 1
-
-
+
+
View
3 lib/assets/javascripts/util/validation.coffee
@@ -49,3 +49,6 @@ Backtastic.Validation =
@errors if _.keys(@errors).length > 0
Backtastic.include Backbone.Model, Backtastic.Validation
+
+Backtastic.applyValidations = (models, validations) ->
+ model.validate(validations[name]) for name, model of models
View
18 lib/assets/javascripts/views/form_field_view.coffee
@@ -1,34 +1,38 @@
class Backtastic.Views.FormFieldView extends Backtastic.View
-
+
constructor: (options)->
super
@field = options.field
@label = options.label
@parentView = options.parentView
@parentView.on "rendered", => @afterParentRender()
@model.on "error", (model, errors) => @displayErrors(errors)
-
+ @model.on "change:#{@field}", @clearErrors, @
+
events:
"blur input": "updateModel"
-
+ "keyup input": "updateModel"
+
updateModel: ->
@model.set @field, @$("input").val()
-
+
afterParentRender: ->
@setElement(@parentView.$("[data-view-id=#{@cid}]"))
@render()
-
+
render: ->
super
@$el.addClass "control-group"
-
+
displayErrors: (errors) ->
errors = errors.errors if errors.errors
if errors?[@field]
messages = errors[@field]
@$el.addClass "error"
@$("span.help-inline").remove()
@$el.append "<span class='help-inline'>#{message}</span>" for message in messages
-
+
clearErrors: ->
@$el.removeClass "error"
+ @$("span.help-inline").remove()
+
View
16 lib/tasks/validations.rake
@@ -0,0 +1,16 @@
+namespace :backtastic do
+ namespace :validations do
+ desc "Build put validation metadata in app/assets/javascripts"
+ task :build => :environment do
+ Dir[Rails.root.join "app", "models", '**', '*.rb'].each do |file|
+ begin
+ require file
+ rescue
+ end
+ end
+
+ erb = ERB.new File.read(File.join(File.dirname(__FILE__), "..", "templates", "rails_validations.coffee.erb"))
+ File.open(Rails.root.join("app", "assets", "javascripts", "rails_validations.coffee"), "w") { |f| f.puts(erb.result) }
+ end
+ end
+end
View
3 .../javascripts/rails_validations.coffee.erb → lib/templates/rails_validations.coffee.erb
@@ -4,6 +4,3 @@ Backtastic.Rails =
<% ActiveRecord::Base.descendants.each do |model| %>
Backtastic.Rails.validations.<%= model %> = <%= Backtastic.validations_for(model).to_json %>
<% end %>
-
-Backtastic.Rails.applyValidations = (models) ->
- model.validate(Backtastic.Rails.validations[name]) for name, model of models

0 comments on commit d0aac88

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