Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jameshibbard's full-sized avatar

James Hibbard jameshibbard

View GitHub Profile
@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 / 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 / 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 / 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 / users_controller.rb
Last active August 29, 2015 14:13
Devise / cancancan tutorial: UsersController#update
def update
if user_params[:password].blank?
user_params.delete(:password)
user_params.delete(:password_confirmation)
end
successfully_updated = if needs_password?(@user, user_params)
@user.update(user_params)
else
@user.update_without_password(user_params)
@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: "sally@example.com", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r1.id})
u2 = User.create({name: "Sue", email: "sue@example.com", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r2.id})
u3 = User.create({name: "Kev", email: "kev@example.com", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", role_id: r2.id})
u4 = User.create({name: "Jack", email: "jack@example.com", 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 / registrations_controller.rb
Created January 23, 2015 20:56
Overriding Devise's RegistrationsController allows us to add our own logic for handling updates
class RegistrationsController < Devise::RegistrationsController
def update
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
@user = User.find(current_user.id)
if needs_password?
successfully_updated = @user.update_with_password(account_update_params)
else
account_update_params.delete('password')
account_update_params.delete('password_confirmation')
@jameshibbard
jameshibbard / edit.html.erb
Created January 23, 2015 20:29
Adding nested attributes to Devise "Edit profile" view
<h2>Login Details</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
@jameshibbard
jameshibbard / site.css
Created March 13, 2015 15:10
Basic styles for a Camel.js blog
body{background-color:#FFF;color:#303030;line-height:1.6;font-size:16pt;font-weight:300}
a{text-decoration:none;color:#0083ff}
time{color:#ababab}
.super{vertical-align:super;font-size:50%}
.content{text-align:left;max-width:775px;margin:0 auto 2em}
.content ul{padding-left:2.5em}
.content ol{padding-left:2.5em}
.container{margin-bottom:3em;margin-left:1em;margin-right:1em}
.content .header{text-align:center;border-bottom-style:solid;border-bottom-color:#e4e4e4;border-bottom-width:.3em}
.content .header .siteTitle{font-size:3.5em;font-weight:700;margin-top:-.25em}