Skip to content

Instantly share code, notes, and snippets.

View jameshibbard's full-sized avatar

James Hibbard jameshibbard

View GitHub Profile
@jameshibbard
jameshibbard / seeds.rb
Created January 18, 2015 14:26
Devise / cancancan tutorial: seeding the app with data
r1 = Role.create({name: "Regular", description: "Can read items"})
r2 = Role.create({name: "Seller", description: "Can read and create items. Can update and destroy own items"})
r3 = Role.create({name: "Admin", description: "Can perform any CRUD operation on any resource"})
u1 = User.create({name: "Sally", email: "[email protected]", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r1.id})
u2 = User.create({name: "Sue", email: "[email protected]", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r2.id})
u3 = User.create({name: "Kev", email: "[email protected]", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r2.id})
u4 = User.create({name: "Jack", email: "[email protected]", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r3.id})
i1 = Item.create({name: "Rayban Sunglasses", description: "Stylish shades", price: 99.99, user_id: u2.id})
@jameshibbard
jameshibbard / application.html.erb
Created January 18, 2015 14:23
Devise / cancancan tutorial: changes to the application template
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Edit profile", edit_user_registration_path %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
<% end %>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, id: "flash_#{name}" %>
@jameshibbard
jameshibbard / generate_project_with_scaffolding_and_assosciations.rb
Created January 18, 2015 14:08
Devise / cancancan tutorial: generate the project including scaffolding and assosciations
rails new store
cd store
rails g scaffold user name:string role:belongs_to
rails g scaffold role name:string description:string
rails g scaffold item name:string description:text 'price:decimal{5,2}', user:belongs_to
rake db:migrate
@jameshibbard
jameshibbard / ajax_example.html
Last active August 29, 2015 14:03
Submit whatever value is entered into text field via AJAX
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AJAX demo</title>
</head>
<body>
<input type="text" id="myTextField" />
<button id="ajax">Submit AJAX request</button>
@jameshibbard
jameshibbard / simple_search_and_replace_utf8.rb
Last active August 29, 2015 14:02
Parse text files recursively, searching for occurrence of string
#encoding: UTF-8
start_path = "/path/to/parent/directory/"
def change_text_file(path)
file_contents = ""
File.open(path,'r') do |file|
while line = file.gets
if line.force_encoding("ISO-8859-1").encode("utf-8", replace: nil).match "whatever"
line = line.sub("whatever", "new whatever")
end
@jameshibbard
jameshibbard / simple_search_and_replace.rb
Last active August 29, 2015 14:02
Parse text files recursively, searching for occurrence of string
start_path = "/path/to/parent/directory/"
def change_text_file(path)
file_contents = ""
File.open(path,'r') do |file|
while line = file.gets
if line.match "whatever"
line = line.sub("whatever", "new whatever")
end
file_contents += line
@jameshibbard
jameshibbard / ui-blocking-overlay.html
Created April 1, 2014 09:33
Display UI-blocking overlay on page load (uses local storage to display only once)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
#overlay{
position: fixed;
top: 0%;
left: 0%;
width: 100%;
@jameshibbard
jameshibbard / capture_close.rb
Last active August 29, 2015 13:57
FXRuby - capture close on the main window
require 'fox16'
include Fox
class MyApp < FXMainWindow
def initialize(app)
@app = app
super(app, "Test", :height => 150, :width => 350, :opts=> DECOR_ALL)
self.connect(SEL_CLOSE, method(:on_close))
end
@jameshibbard
jameshibbard / persistent_checkboxes.html
Created January 27, 2014 11:42
Persistent checkboxes using cookies
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Persist checkboxes</title>
<style>
button{
margin-top:8px;
}
</style>
@jameshibbard
jameshibbard / get_employee_data_2.html
Created December 3, 2013 10:21
Get employee data via AJAX (2)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>AJAX filter demo</title>
<style>
body {
padding: 10px;
}