Skip to content

Instantly share code, notes, and snippets.

View mitchlloyd's full-sized avatar

Mitch Lloyd mitchlloyd

View GitHub Profile
def answer_iucu_challenge(question)
case question
when /what year/: return "1988"
when /what street/: return "Main Street"
when /father\'s middle name/: return "Jonathan"
when /what city/: return "Denver"
else "blah"
end
end
@mitchlloyd
mitchlloyd / gist:1504102
Created December 21, 2011 01:18
Some ideas for fetching/finding with spine and callbacks.
class Model extends Spine.Model
# This one always pulls the data from the server.
@fetchAndThen: (id, callback) ->
@fetch(id: id)
@one 'refresh', (docs) -> callback(docs[0])
# This one pulls the data from the server if it isn't cached locally.
@remoteFind: (id, callback) ->
if @exists? id
callback @find(id)
@mitchlloyd
mitchlloyd / gist:5782436
Created June 14, 2013 14:45
(Blog snippet) Example of ember auto save that won't work
App.Document = DS.Model.extend
# some attributes here...
saveWhenDirty: (->
@get('store').commit() if @get('isDirty')
).property('isDirty')
@mitchlloyd
mitchlloyd / gist:5782662
Last active December 18, 2015 12:28
Simple debounce function bases on underscore's.
App.debounce = (func, wait) ->
timeout = null
->
context = this
args = arguments
immediate = true if args[0] and args[0].now
later = ->
@mitchlloyd
mitchlloyd / gist:5782698
Last active December 18, 2015 12:28
(blog snippet)
App.DocumentController = Ember.ObjectController.extend
autoSave: (->
@debouncedSave()
).property('content.body')
debouncedSave: App.debounce (-> @save()), 1000
save: ->
@get('store').commit()
@mitchlloyd
mitchlloyd / gist:5784462
Last active December 18, 2015 12:39
(Blog snippet) A mixin for autosaving.
#= require ./debounce
BUFFER_DELAY = 1000
App.AutoSaving = Ember.Mixin.create
# Setup buffers to write to instead of directly editing
# the model attributes.
_buffers: Ember.Map.create()
# Buffered fields are saved after the set delay for the
@mitchlloyd
mitchlloyd / gist:5784507
Last active December 18, 2015 12:39
(blog snippet) Example of using the AutoSaving mixin.
App.DocumentController = Ember.ObjectController.extend App.AutoSaving,
bufferedFields: ['title', 'body']
instaSaveFields: ['postedAt', 'category']
s = Student.find_by(email: 'mitch.lloyd@gmail.com')
# Before
def show
if current_user.is_admin?
if params[:assignment].nil?
@assignments = Assignment.all
else
@assignments = Assignment.where(:student_id => params[:assignment][:id] )
end
else
# Before
def index
if current_user.is_student?
@assignments = current_user.assignments.order(created_at: :desc)
render "student_index"
elsif current_user.is_admin?
@current_student = params[:student]
@students = User.get_all_students
@average_grade = 0