Skip to content

Instantly share code, notes, and snippets.

@DVG
DVG / env.rb
Created May 21, 2009 17:07
env.rb file for using Cucumber, Webrat, Mechanize and RSpec to test non-ruby applicaitons.
require 'webrat'
require 'webrat/core/matchers'
include Webrat::Methods
include Webrat::Matchers
Webrat.configure do |config|
config.mode = :mechanize
end
class MechanizeWorld < Webrat::MechanizeSession
@DVG
DVG / Obj-C Detailed Error Save.m
Created June 25, 2010 14:56
Bruce's Detailed Error Code
NSError* error;
if(![managedObjectContext save:&error]) {
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else {
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
for (int y=0; y<[selectedMonsters count];y++) {
if [[selectedMonsters objectAtIndex:y] isKindOfTemplate:[monsters objectAtIndexPath:indexPath]] {
cell.detailLabel.text = [monster quantity];
}
else {
cell.detailLabel.text = "";
}
}
}
@DVG
DVG / gist:2938205
Created June 15, 2012 19:03
Jquery wrapText Fucntion in Coffeescript
wrapText = (elementID, openTag, closeTag) ->
textArea = $("##{elementID}")
len = textArea.val().length
start = textArea[0].selectionStart()
end = textArea[0].selectionEnd()
selectedText = textArea.val().substring(start, end)
replacement = "#{openTag}#{selectedText}#{closeTag}"
textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len))
#example click function
class Post
has_one :avatar
end
class Avatar
belongs_to :post
has_one :image
delegate :image, :to => :post
end

valid BBCOde Document

Hello World # => rule text
[b]I'd like to teach the world to dance[/b] # => rule expression

Also Valid

[b]Hello World[/b] # rule expression
@DVG
DVG / combinations.rb
Created March 1, 2013 13:47
Prints out a boolean truth table for a given number of variables
class Combinations
attr_accessor :number_of_variables
def initialize(number_of_variables=2)
@number_of_variables = number_of_variables
end
def print_combinations
combinations.each do |row|
row.each_with_index do |v, i|
@DVG
DVG / my_active_record_class.rb
Last active December 17, 2015 09:19
Conditional alias attribute for changing a column name if the migrations haven't been run yet. Useful if your code deploy can possibly happen before you rmigration
unless self.column_names.include? "new_name"
alias_attribute :new_name, :old_name
end
StripfighterEmber.ApplicationController = Ember.Controller.extend
signOut: ->
StripfighterEmber.Auth.signOut()
StripfighterEmber.Auth.destroySession()

It's important to make sure your server-side app is secure when using a client-side application like Ember, since any user can alter your javascript at runtime to make it do whatever you want. Therefore you want to make sure you still use server-side validations to make sure that your persistant records are valid.

However, Ember doesn't include any built-in way of displaying these errors on a form like you might be used to coming from a Rails world. This is how I do it.

Let's take this login form as an example (using twitter bootstrap css):

<form class="form-horizontal" {{action sendRegistration on="submit"}}>
  <div class="control-group">
    <label class="control-label" for="email">Email</label>