Skip to content

Instantly share code, notes, and snippets.

View Odaeus's full-sized avatar

Andrew France Odaeus

View GitHub Profile
class User
has_many :products
def root?
id == 1
end
def products_with_root
if root?
Product # Better way?
conds = {:name => params[:name], :cas_number => params[:cas_number]}.reject {|k,v| v.nil? }
conds = conds.inject([[]]) {|h,(k,v)| h.first << "#{k} LIKE ?"; h << "%#{v}%"; h }
conds[0] = conds[0].join(' AND ')
ingredients = Ingredient.find(:all, :conditions => conds)
# Rails Monkey Patch for fields_for with nested attributes.
# Allows an array to be supplied to a nested fields_for call instead
# of just a single object.
module ActionView
module Helpers
class FormBuilder
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
obj_arg = args.first
@Odaeus
Odaeus / gist:1057518
Created June 30, 2011 23:13
Fix for acts_as_reportable associations with the same name
diff --git a/lib/ruport/acts_as_reportable.rb b/lib/ruport/acts_as_reportable.rb
index cdcafe9..30db614 100644
--- a/lib/ruport/acts_as_reportable.rb
+++ b/lib/ruport/acts_as_reportable.rb
@@ -344,13 +344,8 @@ module Ruport
new_columns = []
associations.each do |association|
- if include_has_options
- assoc_options = includes[association].merge({
@Odaeus
Odaeus / gist:3294088
Created August 8, 2012 10:30
ActiveRecord column defaults
# Given an articles table with a title column
class Article < ActiveRecord::Base
column_defaults["title"] = "Blank"
end
assert_equals Article.new.title, "Blank"
@Odaeus
Odaeus / gist:3620131
Created September 4, 2012 10:57
Active Places response
Dear Andrew,
Thank you for your enquiry.
Sport England still maintain the Active Places database , however the
website has not had any money spent on it for a number of years and is
being superseded by Spogo https://spogo.co.uk/ . We are working with
the FIA on the development of this with an initial one year pilot.
@Odaeus
Odaeus / robot.js
Created December 11, 2012 21:39
Alpha Charlie
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
for (var i = 0; i < 3; i++) {
@Odaeus
Odaeus / a_rspec_output.txt
Created March 12, 2013 15:40
Discourse craziness
44) User previous_visit_at first time another call right after still has no value
Failure/Error: let(:user) { Fabricate(:user) }
NoMethodError:
undefined method `first' for nil:NilClass
# ./lib/pretty_text.rb:122:in `block in markdown'
# <internal:prelude>:10:in `synchronize'
# ./lib/pretty_text.rb:119:in `markdown'
# ./lib/pretty_text.rb:175:in `cook'
# ./app/models/user.rb:424:in `cook'
# /home/andrew/.rbenv/versions/1.9.3-p362/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:429:in `_run__4302418198468556500__save__3765986560144774874__callbacks'
@Odaeus
Odaeus / application_controller.rb
Last active June 15, 2021 09:34
Alternative to Rails' sharing of instance variables between controller and views.
class ApplicationController < ActionController::Base
# Creates an accessor which is exposed to the view
def self.view_accessor(*names)
attr_accessor *names
helper_method *names
end
end
@Odaeus
Odaeus / packages.md
Last active December 15, 2015 10:19
Packages for running Ruby on Rails on Ubuntu

Minimal for gem compilation

  • build-essential

Common for gems

  • libxml2
  • libxml2-dev (for nokogiri)

Ruby compilation

  • libreadline6