Skip to content

Instantly share code, notes, and snippets.

@salex
salex / jekyll_uv_highlight.rb
Created July 18, 2010 12:53
Jekyll Plugin for Ultraviolet
module Jekyll
class UltravioletTag < Liquid::Block
require "Uv"
# This defines an uvhighlight tag for code highlighting using Ultraviolet (must [sudo] gem install ultraviolet and dependancies)
# Syntax for tag is {% uvhighlight code [theme] ["linenumb|linenos"]%} which is similar to the pygments call
# code is required, theme and linenumb optional and in any order
def initialize(tag_name, arg, tokens)
super
copy_files? # copy css files if not in css directory

Client-Side Unobtrusive Javascript

As I keep trying to dig into Rails 3, I keep running into things that make me learn more than I want to -see "My brain is full!"

Javascript is another one of those languages that I have used a lot, but only understand the basics. Rails 3 implements Unobtrusive Javascript (UJS) using either Prototype or JQuery. I have done quit a bit with Prototype and when I ported a Rails 2 application to Rails 3, a validation.js library I was using stopped working. I think because of a Prototype version upgrade that broke the library. This was one of those generic validation libraries that checked: empty, email, number, etc. The only thing I used was empty - and then I had to modify it to take care of radio and checkbox inputs in a sane manner.

Anyhow I decided to roll my own. My first attempt was to steal parts of rails.js and write kludges to suit my needs. It worked, but I was not sure I liked it (or understood)

@salex
salex / jsonParseSimple.a4d
Created January 2, 2011 18:10
A JSON Parse Routine for Active4D with some limitations
<%
/**********************************************************************************
jsonParseSimple
In => $json -> Text in JavaScript Object Notation(JSON)
Out => The JSON text parsed to an Active4D collection with some limitations.
Active4D cannot do everything that you can in JSON (JavaScript, Ruby, PHP etc).
You cannot have an array of mixed types as you can in other languages.
# This is yet another attempt at doing the elusive combo box.
# Since I moved to jquery,I wanted to see if there was something out there
# that was better then what I found a few years ago. There was
# a jquery plugin, but I am not sure how it worked - didn't see where you could enter text!
# You should be able to either select from a pulldown list, or add an item that is not in the list.
# This in a nutshell overlays a 13em input text field over a 15em select tag. (css)
# There is a helper method build_combo_box where you send the form_builder object, the method,
# the choices and the current value. It builds the combo box.
@salex
salex / json.a4d
Created June 5, 2011 16:05
jsonParseSimple for 4D v11
<%
method "jsonParseSimple"($json)
// First convert any dates in IETF format to 4d date
$re1 := "~\"?(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)( \d\d*, \d{4})\"~"
$rp1 := "form.newDate(\"\1\2\")"
regex replace($re1; $json; $rp1; $json)
$json := trim($json)
if ($json[[1]] = "[") // if json is just an array, wrap in in an object
@salex
salex / gist:1346121
Created November 7, 2011 20:47
A YAML like serializer for Active4D
/*
serialize library
Serialize is beta library that seriailizes deeply nested collection into a blob.
My need is that these collections never moved from a prototype implmentation in collections
versues a table approach. The system is still in use, with hopes of moving off of 4D, but not yet!
The current solution is to convert the collection to an objectTools object. While it works, it is a little.
slow, probably because as my parser. The xml is also bumping up against the 4.5 32k text limit.
@salex
salex / score_gist.rb
Last active December 17, 2015 17:08
Refactored Score, as class and tests
## app/models/assessable/scoring.rb
module Assessable
module Scoring
class Score
class Score
attr :post, :total_raw, :total_weighted, :scores
def initialize(assessment,post)
#@assessment = assessment
@salex
salex / recurring_select_helper
Created August 12, 2013 10:51
Changes made to recurring select helper. Move modules from utilities folder into main module and comment out require_relative
require "ice_cube"
#require_relative "utilities/form_options_ext"
#require_relative "utilities/tag_ext"
module RecurringSelectHelper
module FormHelper
def select_recurring(object, method, default_schedules = nil, options = {}, html_options = {})
if Rails::VERSION::STRING.to_f >= 4.0
# === Rails 4
RecurringSelectTag.new(object, method, self, default_schedules, options, html_options).render
@salex
salex / _fire.html.slim
Last active March 7, 2020 16:52
Rails Stimulus Rails.ujs approaches
.w3-row
p Test two, move element using Rails.ujs
/ This is a hidden form, payload may be usefull in other applications
= form_with( url:'/test/fire',method: :patch, html: {data: { type: "html", action: "ajax:success->testTwo#onPostSuccess",target:'testTwo.form' } },class:'w3-hide') do |form|
= form.text_area :payload, data: { target: "testTwo.payload" }
.w3-col.s6
p Left list (Cleared)
ul
- session[:test_list].each do |k,v|
A spreadsheet like controller for a table with two controllers with shared targets
Since I didn't get any opinions on last post https://discourse.stimulusjs.org/t/one-controller-and-multiple-repeating-target-or-multiple-controller-for-each-area/1087 (Is anyone using this site? Is Stimulus.js still alive?) I decided to try another approach.
Since most of my work is using `accounting like` applications, I have a lot of tables (ledgers etc). One of the applications I'm rewriting deals with keeping scores for a golf groups. You can look at a golf score card in visualize a spreadsheet. You put your scores down for each hole, sum the front and back and come up with a total score. We play teams so there are three or four players on the card, and there our four or so teams. Each team turns in their card with scores (front, back and total) and someone wins.
The scores are then entered into a Rails application and new quotas or a handicap is computed. That's the simplified version of ptgolf.us. The rewrite is de-jq