Skip to content

Instantly share code, notes, and snippets.

View DivineDominion's full-sized avatar

Christian Tietze DivineDominion

View GitHub Profile
@DivineDominion
DivineDominion / README.md
Last active December 15, 2015 13:49
Where I show the most inefficient way to duplicate HTML rendering for JavaScript XHR form validation callbacks.

Because Rails' Unobstrusive JavaScript (UJS) driver rails.js and jQuery won't execute any JavaScript in XMLHttpRequest's response bodies when the status code is 400 or 424 or something similar, I had to instruct the client (jQuery) myself to render error messages on form validation.

This is my first attempt to do so: instead of a JS request, respond with JSON and do the interface changes client-side. Problem is, the JavaScript code is sent to the client only once, hence ERB view templates aren't available during execution. I couldn't just use Rails helpers this way to refactor rendering flash messages. The HTML was spread across three places then: create.html.erb to handle synchroneous requests and render both flash messages and form validation error, entries.js.coffee to render errors and lastly create.js.erb to render success messages and insert the new entry.

It worked, and it was a fun ride since I didn't

Where, instead of my inefficient approach, I execute XHR JavaScript responses. Behaves just like JangoSteve said it should for 400 or 424 HTTP response status codes.

Binds the event on $(document) so that dynamically created forms will be affected, too.

@DivineDominion
DivineDominion / com.brettterpstra.gitlogger.plist
Last active December 26, 2021 17:59 — forked from ttscoff/gitlogger.rb
Logs selected git repository commits to a text file or Day One for the past day once in the morning.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.brettterpstra.gitlogger</string>
<key>EnvironmentVariables</key>
<dict>
<key>LANG</key>
<string>en_US.UTF-8</string>
@DivineDominion
DivineDominion / in_memory_strategy.rb
Created September 22, 2013 10:24
Repository in Ruby according to Eric Evans (2006): Domain-Driven Design. Tackling complexity in the heart of software, Upper Saddle River, NJ: Addison-Wesley.
class InMemoryStrategy
def matching(criteria)
results = []
domain_objects.each do |object|
results << object if criteria.satisfied_by?(object)
end
end
end
@DivineDominion
DivineDominion / markdownTemplate.txt
Last active June 4, 2023 06:56
BibDesk MultiMarkdown export template. Put into `~/Library/Application Support/BibDesk/Templates` and activate in settings to use.--- From http://zettelkasten.de/bibliography-zettelkasten/
<$publications>
[#<$citeKey/>]: <$authors.name?>
<$authors.name.stringByRemovingTeX.@componentsJoinedByCommaAndAnd/><?$authors.name?><$editors.name.stringByRemovingTeX.@componentsJoinedByCommaAndAnd/> (Hrsg.)
</$authors.name?>
<$fields.Year?> (<$fields.Year/>)</$fields.Year?>: _<$fields.Title.stringByRemovingTeX/><$fields.Subtitle?>. <$fields.Subtitle.stringByRemovingTeX/></$fields.Subtitle?>_<$pubType=article?>
, <$fields.Journal/><$fields.Number?> <$fields.Number/></$fields.Number?>, <$fields.Year/>, Vol. <$fields.Volume/>, S. <$fields.Pages/><?$pubType=incollection?>
, in: <$fields.Booktitle/><$fields.Address?>, <$fields.Address/>: <$fields.Publisher?><$fields.Publisher/></$fields.Publisher?><?$fields.Address?>
<$fields.Publisher?>, <$fields.Publisher/></$fields.Publisher?>
</$fields.Address?>
<?$pubType?>
@DivineDominion
DivineDominion / installation_instructions.md
Last active August 12, 2021 17:27
nvALT Preview Template featuring relative paths

Run this from your terminal to execute the setup script. It creates the directory and downloads the template right into this new folder.

curl -s https://gist.githubusercontent.com/DivineDominion/ab1abe8d2b93d4b73d69/raw/6a7066f1fbcf45e3c47a8d85acee475dd33bd32c/install.sh | bash 

Aferwards, you can change the base directory in ~/Library/Application\ Support/nvALT/template.html as expected.

@DivineDominion
DivineDominion / nvALT Zettel Headel.scpt
Created September 11, 2014 08:52
AppleScript to rename notes in nvALT with the Zettel ID and a title of your choosing. Also inserts the information as a header into the note.
-- Extend "current date"
script FormattedDate
property parent : current date
on twoDigitDisplay(aNumber)
set multiplier to 10 ^ 2 #howManyDigits
return "" & (text 2 thru 3 of ((multiplier + aNumber) as string))
end twoDigitDisplay
end script
feature 'Contact form POST without Ajax', :js => false do
context 'without name' do
it 'shows the index' do
visit_landing_page
within('#request-form') do
fill_in 'request-email', :with => 'test@test.de'
fill_in 'request-anliegen', :with => 'Test'
fill_in 'request-seiten', :with => '123'
end
click_button 'Anfragen'
@DivineDominion
DivineDominion / TestCase.swift
Last active August 15, 2018 09:14 — forked from akolov/TestCase.swift
Bridging XCTAssertThrows to Swift to catch exceptions. (Doesn't work with assert(), though)
class ExceptionTestCase: XCTestCase {
func raisesException() {
var exception = NSException(name: NSInternalInconsistencyException, reason: "Testing exceptions", userInfo: nil)
XCTAssertThrows({ exception.raise() })
XCTAssertThrowsSpecific({ exception.raise() }, NSInternalInconsistencyException, "Should raise NSInternalInconsistencyException")
}
}
@DivineDominion
DivineDominion / ValueTransformation.swift
Created March 1, 2015 19:27
Paste this into a playground and see what happens with the two client code examples at the bottom. Should work out of the box.
import Cocoa
/// A wrapper around any type to make Result compile
class Box<T> {
let unbox: T
init(_ value: T) {
self.unbox = value
}
}