Skip to content

Instantly share code, notes, and snippets.

@jlebrech
jlebrech / aliases_with_params.rb
Created February 12, 2013 09:00
Would be nice if this worked?
alias :save :user_confirm
alias :delete :user_confirm
def user_confirm(current_user)
unless self.user == current_user
errors.add(:user, 'Must be you')
else
super
end
end
@jlebrech
jlebrech / _widget.erb
Last active December 12, 2015 05:29
domain problem based javascript, css and html
<% widget_coffee :my_widget do %>
... some coffeescript excluding closure wrapping
... this content gets compiled into application.js once.
<% end %>
<% widget_sass :my_widget do %>
... namespaced styling for my_widget
... this gets compiled into application.css once.
<% end %>
@jlebrech
jlebrech / new.rb
Created February 6, 2013 10:20
Instead of a blank migration, a commented out but moderately complex example (using the name from the cli)
# class AddCategories < ActiveRecord::Migration
# def up
# create_table :categories do |t|
# t.string :name
# t.integer :score
# ... # other examples
# end
# end
# def down
@jlebrech
jlebrech / replace-tags.rb
Created January 30, 2013 10:15
replacing tags doesn't persist in the database.
# loop through article containing the old_tag
Article.tagged_with(old_tag).each do |article|
# give articles with the old tag the new tag
article.tag_list.add(new_tag)
# remove the old tag
article.tag_list.remove(old_tag)
article.save # this isn't
end
@jlebrech
jlebrech / dyndefn.rb
Last active December 10, 2015 04:08
Dynamic Method Module
class MyClass
def initialiser
defn /do_([a-z]*)_to_([a-z]*)/, :do_x_to_y
end
def defn(regex, &proc)
@@missing_things = [] if @@missing_things.nil?
@@missing_things << [ regex, proc ]
end
@jlebrech
jlebrech / radial.lua
Created December 20, 2012 10:28
Radial gravity in love2d
ship = bodies[1]
shipVec = vector(ship:getX(),ship:getY())
planet = bodies[2]
planetVec = vector(planet:getX(),planet:getY())
distance = planetVec – shipVec
force = 250 / distance:len2()
normforce = force*distance
bodies[1]:applyImpulse(normforce.x, normforce.y,ship:getX(),ship:getY())
@jlebrech
jlebrech / ng-options.html
Created October 19, 2015 14:08
angular is dumb
<select
ng-model="selected.service_plan"
ng-options="item.id as ('£' + item.label) for item in billing_info.service_plans">
</select>
what's wrong with looping through stuff rather than come up with some expression language? speed?
@jlebrech
jlebrech / idea.txt
Created November 23, 2012 15:09
partials: erb, js and css
Say we had the following structure
App
-> Views
-> ABC
abc.erb
-> partials
_a.erb
_b.erb
_c.erb
@jlebrech
jlebrech / howtocombine.rb
Created April 5, 2012 12:40
Arel subqueries
t=Ticket.arel_table # the arel table to extract subqueries from
if params[:title].length > 0 then
sqla = t[:title].matches("%#{params[:title]}%")
else
sqla = nil
end
if params[:from].length > 0 then
sqlb = t[:from].matches("%#{params[:from]}%")
tags=Tag.arel_table
@tags = Tag.where(tags[:label].matches("%#{params[:term]}%"))