Skip to content

Instantly share code, notes, and snippets.

View nigelr's full-sized avatar

Nigel Rausch nigelr

  • brisbane, australia
View GitHub Profile
@nigelr
nigelr / presenter_design_pattern.rb
Last active December 1, 2017 05:38 — forked from addstar34/presenter_design_pattern.rb
Presenter Design Pattern (without double iterating when coming from #index)
# app/controllers/pets_controller.rb
class PetsController < ApplicationController
before_action :set_pet, only: [:show, :edit, :update, :destroy]
def index
# old way
# @pet_presenters = Pet.all.map { |pet| PetPresenter.new(pet, view_context) }
# new way
@pet_presenters = PetsPresenter.prepare(Pet.all, view_context)
@nigelr
nigelr / meme_spec.rb
Created February 21, 2015 07:56
create menu
require 'rspec'
class Menu
def item val, &block
p val
yield if block_given?
end
def test_it
class Company < ActiveRecord::Base
has_many :contacts
class Contact < ActiveRecord::Base
belongs_to :company
new.html.haml
%h1 New vendor
= render 'form'
= link_to 'Cancel', vendors_path, id: :cancel_new_vendor_link
---------------------
_form.html.haml
<b>Priority:</b>
<%= @ticket.priority.name %>
@nigelr
nigelr / show.html.erb
Created May 17, 2013 01:30
priority show
<b>Priority:</b>
<%= @ticket.priority.try(:name) %>
create_table :priorities do |t|
t.string :name
t.boolean :default
t.boolean :archived
t.integer :position
t.timestamps
end
@nigelr
nigelr / select.html
Created May 16, 2013 02:03
sample HTML dropdown
<select name="ticket[priority_id]">
<option value="low">Low</option>
<option value="medium">Medium</option>
<option value="high">High</option>
</select>
@nigelr
nigelr / selection.rb
Created May 10, 2013 01:55
special_report
def self.report
from_date = '20130401'
entities = Entity.unscoped.where('updated_at > ?', from_date).where('type in (?)', ['Contact', 'Deceased', 'Child']).where(culture_id: [Selection.culture_australian_aboriginal.id, Selection.culture_australian.id]).order(:site_id, :type)
p entities.length
media_items = MediaItem.unscoped.where('updated_at > ?', from_date)
p media_items.length
entities = Entity.unscoped.where('updated_at > ?', from_date).where('type in (?)', ['Contact', 'Deceased', 'Child']).where(referral_source_id: [Selection.referral_source_internet.id, Selection.referral_source_information_session.id]).order(:site_id, :type)
p entities.length
entities = Entity.unscoped.where('updated_at > ?', from_date).where('type in (?)', ['Contact', 'Deceased', 'Child']).where(relation_id: [Selection.relation_friend.id, Selection.relation_support_1.id]).order(:site_id, :type)
@nigelr
nigelr / application_controller_spec.rb
Created January 15, 2013 06:23
application_controller_spec
require "spec_helper"
describe ApplicationController do
controller do
def index
render nothing: true
end
end
let(:user) { create(:user_agent) }