Skip to content

Instantly share code, notes, and snippets.

View mhenrixon's full-sized avatar
🐢
I may be slow to respond.

Mikael Henriksson mhenrixon

🐢
I may be slow to respond.
View GitHub Profile
@mhenrixon
mhenrixon / console.js
Created October 2, 2012 14:37 — forked from dagda1/console.js
console.js
if (typeof console === "undefined" || typeof console.log === "undefined") {
console = {
log: function() {
}
};
}
@mhenrixon
mhenrixon / user_spec.rb
Created November 13, 2012 09:56
Comparison of RSpec with Test Unit
require 'spec_helper'
describe User do
describe "#password_reset" do
let(:site) { create :site }
let(:user) { create :user }
it "creates a password_reset_token" do
expect{
user.send_password_reset(site)
@mhenrixon
mhenrixon / template_spec.rb
Created November 13, 2012 10:19
More comparisons
require 'spec_helper'
describe Template do
let(:site) { create :site }
let(:template) { create :template, name: "ABCD", tpl_type: 'tpl', site: site }
subject { template }
# this also tests downcasing names
its(:tpl_name) { should eql('abcd.tpl') }
its(:dirname) { should eql("#{Rails.root}/public/sites/#{template.site_id}/templates") }
@mhenrixon
mhenrixon / query_methods.rb
Created November 19, 2012 10:04
Where to add query methods?
# 1
List.fetch_by_slug(@site, 'mys-slug')
# 2 - adding query methods on the collection
@site.lists.fetch_by_slug('my-slug')
# How achieved
class Site < ActiveRecord::Base
has_many :lists do
def fetch_by_slug(slug)
@mhenrixon
mhenrixon / create_task_trigger.sql
Created November 22, 2012 19:04
How to achieve rails counter_cache directly in the database.
CREATE TRIGGER update_task_counters
AFTER INSERT OR UPDATE OR DELETE ON tasks
FOR EACH ROW EXECUTE PROCEDURE update_task_counters();
class AddCountTriggers < ActiveRecord::Migration
def up
execute %q{
CREATE OR REPLACE FUNCTION update_task_counters (taskid integer)
RETURNS void AS $$
BEGIN
update tasks set
users_count = coalesce((select count(9) from taskships where task_id = taskid), 0)
where id = taskid;
END;
@mhenrixon
mhenrixon / search.sql
Created November 23, 2012 06:05
Various search queries for MS SQL server
alter proc search (@query nvarchar(100))
/*
** Purpose: Search for a specific value in all columns in all tables
** Notes:
set statistics time on
exec q_sys_search 'q_'
set statistics time off
** Revision History:
** yyyy-mm-dd Author Comments
** ---------- -------------- --------------------
# Scenario för att välja produkt
# Resurser som behövs är:
# @card_products
# Scenario för produkten 'Period buss'
# Resurser som behövs är:
# @product
@mhenrixon
mhenrixon / migrate_s3.rake
Created December 1, 2012 14:01
rake task to migrate paperclip attachments to Amazon S3
namespace :attachments do
task :migrate_to_s3 => :environment do
require 'aws/s3'
# Load credentials
s3_options = YAML.load_file(File.join(Rails.root, 'config/s3.yml')).symbolize_keys
bucket = s3_options[:bucket_name]
# Establish S3 connection
@mhenrixon
mhenrixon / edit_post_spec.rb
Created December 3, 2012 11:19 — forked from weilu/edit_post_spec.rb
Turn off transactional fixtures only for Capybara javascript specs
# requests/edit_post_spec.rb
require 'spec_helper'
feature 'editing a post', js: true do
let(:post) { create :post }
let(:user) { post.creator }
before { sign_in user }