Sticky table header with jQuery
When you want the table headers to always stay around (e.g. because that table is huuuge), use the code below.
Style
table.fixed_table_header{
position: fixed;
top: 0;
width: auto;
display: none;
border: none;
margin: 0;
}
Javascript
;(function($) {
$.fn.fixHeader = function() {
return this.each(function() {
var $table = $(this),
$t_fixed;
function init() {
$t_fixed = $table.clone();
$t_fixed.find('tbody').remove().end().addClass('fi...
Hash any Ruby object into an RGB color
If you want to label things with a color but don't actually care which cholor, you can use the attached Colorizer
class.
To get a completely random color (some of which will clash with your design):
Colorizer.colorize(some_object) # => "#bb4faa"
To get similiar colors (e. g. bright, pale colors of different hues):
# random hue, saturation of 0.5, lightness of 0.6
Colorizer.colorize_similarly(some_object, 0.5, 0.6) # => "#bbaaaa"
Also see the color gem.
Restangular: How to remove an element from a collection without breaking restangular
So you have a restangular collection and you want to remove an element from it, after you've successfully deleted it from the server.
The README suggests to say something like $scope.users = _.without($scope.users, user)
. While that works at first glance (the element is no longer in your collection), it will break horribly when you want to use restangular's attributes on that collection.
This...
better rails app restart with the passenger restart-app tool
With this command you can initiate an application restart without touching restart.txt. Unlike touching restart.txt, this tool initiates the restart immediately instead of on the next request. http://blog.phusion.nl/2014/01/02/phusion-passenger-4-0-33-released/
If you want to use this with capistrano 2.x just replace the touch command:
- run "touch #{current_path}/tmp/restart.txt"
+ run "passenger-config restart-app --ignore-app-not-running #{deploy_to}...
Rails 2: Refuse response formats application-wide
If you regularly get ActionView::MissingTemplate
exceptions, maybe some bot visits your site requesting silly formats like:
http://www.rails-app.com/makandra.html-username-2000 # => Rails tries to retrieve 'makandra' with format 'html-username-2000'
Just restrict accepted format parameters for the whole application like this:
class ApplicationController < ActionController::Base
before_filter :refuse_silly_formats
private
def refuse_silly_formats
acceptable_formats = %w[html xml pdf]
if par...
Customize and download · Bootstrap
List of variables used in Bootstrap. Overwrite to customize your bootstrap application.
When you are using bootstrap-sass
, simply replace @
by $
.
AngularJS directive to format a text with paragraphs and new lines
If you are using Angular and want something like Rails' simple_format
which HTML-formats a plain-text input into paragraphs and line breaks, this directive is for you.
Any HTML fragments inside that text will still be escaped properly.
Use it like this, where your text
attribute specifies something available in your current scope:
<simple-format text="email.message"></simple-format>
This is the directive, in CoffeeScript syntax:
@app.directive 'simpleFor...
assignable_values 0.9.0 supports virtual attributes
Now you can say:
class Song < ActiveRecord::Base
attr_accessor :virtual_attribute
assignable_values_for :virtual_attribute do
%w[air fluff]
end
end
Linux: How to print PDF files from the command line
Sometimes you may want to print files from the command line, especially when you have lots of them.
You can use lp
for that.
To print a single example.pdf
file on your default printer, simply say:
lp example.pdf
lp
accepts multiple filenames, so to print all PDF files in the current directory:
lp *.pdf
You can specify a printer via the -d
switch:
lp -d YOUR_PRINTER_NAME *.pdf
Your printer's name is then one you defined on your system. You can check with your CUPS configuration by visiting <http://localhost:631/...
Ruby: How to camelize a string with a lower-case first letter
If you want to do JavaScript-style camelization, ActiveSupport's String#camelize
method can actually help you out. Simply pass a :lower
argument to it.
>> 'foo_bar_baz'.camelize
=> "FooBarBaz"
>> 'foo_bar_baz'.camelize(:lower)
=> "fooBarBaz"
Listening to bubbling events in Prototype is easy
If you come across an (older) application that is using Prototype instead of jQuery, you may often see events bound to single elements only, like this:
$('foo').observe('change', updateThings);
$('bar').observe('change', updateThings);
$('baz').observe('change', updateThings);
If you are calling only one method in each case, this is unnecessarily ugly. Also, when your page contents have been replaced via AJAX (like sections of a form after choosing something), those event hooks will no longer wo...
Test whether Perfect Forward Secrecy (PFS) is enabled on a server (using OpenSSL)
Use the following command to test if a server (in this example: makandra.com on port 443) uses Perfect Forward Secrecy (PFS):
openssl s_client -connect makandra.com:443 -cipher ECDHE-RSA-RC4-SHA
You should see something like the following:
~ > openssl s_client -connect projecthero.com:443 -cipher ECDHE-RSA-RC4-SHA
CONNECTED(00000003)
depth=1 O = AlphaSSL, CN = AlphaSSL CA - G2
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
0 s:/C=DE/OU=Domain Control Va...
The Heartbleed Bug
The Heartbleed Bug is a serious vulnerability in the popular OpenSSL cryptographic software library. This weakness allows stealing the information protected, under normal conditions, by the SSL/TLS encryption used to secure the Internet.
No more file type confusion in TextMate2
When using TextMate2 with the cucumber bundle, it does not recognize step definitions (e.g. custom_steps.rb
) as such but believes they are plain Ruby files. But there is help!
Solution
Add these lines to the bottom of your .tm_properties
file (in ~/
for global settings, in any directory for per-project settings):
[ "*_steps.rb" ]
fileType = "source.ruby.rspec.cucumber.steps"
Apparently, this works for any files. Define a regex and specify custom settings. The attached article lists all available configuration options (whic...
What's new in edge Rails: Active Record enums
Declare an enum attribute where the values map to integers in the database, but can be queried by name.
The MariaDB Foundation Announces General Availability of MariaDB 10
MariaDB 10 includes numerous innovations developed with and for web-scale players like Google, Fusion-IO and Taobao such as Replication, NoSQL Capabilities, Sharding.
Why Ruby Class Methods Resist Refactoring
In a nutshell:
- Splitting a long method into sub methods is easier in instances since it is in classes. Since you must not save state in a class, you need to pass around context as a long chain of parameters again and again.
- If your public API has a single entry point, you can still have a class-level method that takes care of constructing the instance etc. So it's all win.
Obfuscating data in Rails applications for screenshots and demonstrations
I recently had a need to demonstrate a data-heavy application to potential customers. Demonstrating the application with bogus numbers is one thing, but everything looks much more realistic when I’m using real data. I can’t reveal any real information, though, so I needed a quick way to obfuscate real names. Faker to the rescue!
IE10 / IE11 Metro mode: How to switch tabs or show the address bar
Internet Explorer on Windows 8 and 8.1 is available in a "Desktop version" and the metro version which is designed for touch devices. \
When using IE Metro with a mouse (e.g. via BrowserStack), controls such as the tab bar or address bar will disappear after a little while.
To show them again, right-click anywhere in the page (except on links, etc).
O_o
Thread Safety With Ruby — Luca Guidi
Ruby’s model for concurrency is based on threads. It was typical approach for object oriented languages, designed in the 90s. A thread is sequence of instructions that can be scheduled and executed in the context of a process. Several threads can be running at the same time.
Ruby’s VM process allocates a memory heap, which is shared and writable by threads. If incorrectly coordinated, those threads can lead to unexpected behaviors.