One-liner syntax in RSpec's should-based and expect-based syntaxes
RSpec supports a one-liner syntax for setting an expectation on the subject
:
describe Array do
describe "when first created" do
it { should be_empty }
end
end
The example description "it should be empty" will be defined automatically.
With RSpec 3's expect
-based syntax you use it_is_expected
instead:
describe Array do
describe "when first created" do
it { is_expected.to be_empty }
end
end
Testing state_machine callbacks without touching the database
You should test the callback methods and its correct invocation in two separate tests. Understand the ActiveRecord note before you move on with this note.
Say this is your Spaceship
class with a transition launch
and a release_docking_clamps
callback:
class Spaceship
state_machine :state, :initial => :docked do
event :launch do
transition :docked => :en_route
end
before_transition :on => :launch, :do => :release_doc...
Custom transclusion with Angular
Angular's directives have a transclude
option, that allows for rendering an element's original content within the directive's template:
# HTML
<wrapping-directive>
Content ...
</wrapping-directive>
# Directive template
<div class="wrapper">
<ng-transclude></ng-transclude>
</div>
# Result
<div class="wrapper">
Content ...
</div>
However, if you need more control over the transcludable content you need heavier armor. For this case, Angular provides you with a transclusion function as the 5th argument of...
record a logstalgia video
Cause logstaglia is so cool you may want to record a video. We're lucky: Logstalgia has a parameter for an ppm-stream output: --output-ppm-stream FILE
. We can pipe this to ffmpeg or avconv to record a h264 encoded video.
record command when using ffmpeg (for e.g. with ubuntu 12.04)
cat some_acces.log | logstalgia --sync -1920x1080 --output-ppm-stream - | ffmpeg -y -r 60 -f image2pipe -vcodec ppm -i - -vcodec libx264 -preset ultrafast -pix_fmt yuv420p -crf 1 -threads 0 -bf 0 logstalgia.mp4
record command when using avconv(...
Git: auto-stashing to avoid conflicts on pull
First, decide if you want to pull with rebase. There are some implications of changing this, so think before you do it.
If you do, you can tell git to stash automatically before pulling:
git config --global rebase.autoStash true
Now on pull
, git will a) stash, b) pull and c) reapply your working tree. Note that applying your changes may lead to unresolvable conflicts!
Further see a common git config with this configuration enabled.
Bundler: Install gems behind a proxy
To install gems Bundler needs to be able to talk to https://api.rubygems.org
.
If you are behind a proxy you can use the https_proxy
environment variable:
https_proxy=http://myproxy:123 bundle install
Note that if there is no https_proxy
env variable, Bundler will also look for a http_proxy
env variable.
With Capistrano
Ideally the server you're deploying on exports an https_proxy
variable for all shells.
If you don't have control over the server setup, you can also add this to your Capistrano config:
...
Useful tricks for logging debugging information to the browser console
During debugging you might pepper your code with lines like these:
console.log('foo = ' + foo + ', bar = ' + bar)
I recommend to use variable interpolation instead:
console.log('foo = %o, bar = %o', foo, bar)
This has some advantages:
- It's easier to write
- Variables are colored in the console output
- You don't need to stringify arguments so the
+
operator doesn't explode - If the variable is a structured object like a D...
How to generate a Rails-compatible query string
From Rails 3.0.9, there is a method Hash#to_query that will turn a Hash into a query string:
>> {:a => "a", :b => ["c", "d", "e"]}.to_query
=> "a=a&b%5B%5D=c&b%5B%5D=d&b%5B%5D=e"
>> CGI.unescape _
=> "a=a&b[]=c&b[]=d&b[]=e"
If you're on the browser side, you can serialize nestd objects to query strings using jQuery's $.param
.
Vertical align anything with just 3 lines of CSS
See attached link. The gist is:
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
Works in all web browsers and IE9+.
NoMethodError: undefined method `cache' for Gem:Module
I got this error when running Rails 2.3 tests for Rails LTS. More stacktrace:
NoMethodError: undefined method `cache' for Gem:Module
/vagrant/rails-2-3-lts-repository/railties/lib/rails_generator/lookup.rb:212:in `each'
/vagrant/rails-2-3-lts-repository/railties/lib/rails_generator/lookup.rb:146:in `to_a'
/vagrant/rails-2-3-lts-repository/railties/lib/rails_generator/lookup.rb:146:in `cache'
/opt/vagrant_ruby/lib/ruby/1.8/fileutils.rb:243:in `inject'
/vagrant/rails-2-3-lts-repository/railties/l...
Geordi 1.3 released
Changes:
- Geordi is now (partially) tested with Cucumber. Yay!
- geordi cucumber supports a new @solo tag. Scenarios tagged with
@solo
will be excluded from parallel runs, and run sequentially in a second run - Support for Capistrano 2 AND 3 (will deploy without
:migrations
on Capistrano 3) - Now requires a
.firefox-version
file to set up a test firefox. By default now uses the system Firefox/a test Chrome/whatever and doesn't print warnings any more. -
geordi deploy --no-migrations
(aliased-M
): Deploy with `cap ...
ActiveRecord meets database views with scenic
Using Scenic, you can bring the power of SQL views to your Rails application without having to switch your schema format to SQL. Scenic provides a convention for versioning views that keeps your migration history consistent and reversible and avoids having to duplicate SQL strings across migrations. As an added bonus, you define the structure of your view in a SQL file, meaning you get full SQL syntax highlighting in the editor of your choice and can easily test your SQL in the database console during development.
[https://robots.thoughtb...
Marvel | Elastic
Dashboard (Marvel Kibana) and query tool (Marvel Sense) for Elasticsearch.
Once installed you can access Kibana and Sense at these local URLs:
bash: print columns / a table
Ever wondered how you can create a simple table output in bash? You can use the tool column
for creating a simple table output.
Column gives you the possibility to indent text accurate to the same level. Pipe output to column -t
(maybe configure the delimeter with -s
) and see the magic happening.
detailed example
I needed to separate a list of databases and their corresponding size with a pipe symbol: |
Here is a example list.txt:
DB Size_in_MB
foobar 11011.2
barfoo 4582.9
donkey 4220.8
shoryuken 555.9
hadouken 220.0
k...
yujinakayama/transpec: The RSpec syntax converter
A comprehensive script to convert test suites from RSpec 2 to RSpec 3. This converts more than should/expect syntax.
find out which processes using swap
Wondering which processes are placed in your swap you can use this bash oneliner:
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r
You will see the swap usage of each process sorted by the highes amount of swap usage.
Please don't forget that swap usage of a process isn't an indicator for high memory usage of this process but for the frequency of access or activity of it. The kernel tries to avoid swapping pages which were recently accessed.
Also you should notic...
PostgreSQL: Show size of all databases
Use this:
SELECT pg_database.datname as "database_name", pg_database_size(pg_database.datname)/1024/1024 AS size_in_mb FROM pg_database ORDER by size_in_mb DESC;
Want to see database sizes in MySQL ?
How to render an html_safe string escaped
Once Rails knows a given string is html_safe
, it will never escape it. However, there may be times when you still need to escape it. Examples are some safe HTML that you pipe through JSON, or the display of an otherwise safe embed snippet.
There is no semantically nice way to do this, as even raw
and h
do not escape html_safe
strings (the former just marks its argument as html_safe
). You need to turn your string into an unsafe string to get the escaping love from Rails:
embed = javascript_tag('var foo = 1337;') # This is an h...
Top 10 Email Developments of 2015
You know that layouting HTML e-mails is terrible.
For more fun, check Litmus' list of top 10 e-mail developments of 2015 that did not make things better.
rack-mini-profiler - the Secret Weapon of Ruby and Rails Speed
rack-mini-profiler is a powerful Swiss army knife for Rack app performance. Measure SQL queries, memory allocation and CPU time.
This should probably not be loaded in production (the article recommends otherwise), but this looks like a useful tool.
Puppet: Delete certificate request
To delete a certificate request run sudo puppet ca destroy $your.full.hostname
on your puppetmaster.
AllThingsSmitty/css-protips
A list of surprisingly clever CSS expressions for common use cases.
Use jQuery's selector engine on vanilla DOM nodes
There are cases when you need to select DOM elements without jQuery, such as:
- when jQuery is not available
- when your code is is extremely performance-sensitive
- when you want to operate on an entire HTML document (which is hard to represent as a jQuery collection).
To select descendants of a vanilla DOM element (i.e. not a jQuery collection), one option is to use your browser's native querySelector
and [querySelectorAll
](https://developer.mozilla.org/de/docs/We...
WTF Opera Mini?!
In developing countries like Nigeria, Opera Mini is used by up to 70% of users on mobile.
This is a collection of front-end development features not supported by Opera Mini and, more importantly, some crowdsourced workarounds for them. This isn't about bashing the problem, but figuring out the solution.