Collection of Rails development boosting frameworks
Development environment setup
- Rails Composer
-
Basically a comprehensive Rails Template. Prepares your development environment and lets you select web server, template engine, unit and integration testing frameworks and more.
Generate an app in minutes using an application template. With all the options you want!
Code generators
- Rails Bricks
-
A command line wizard. Once you get it running, it creates sleek applications.
RailsBricks enables you to cre...
Bash: Heavy headings for CLI
To print a colored full-width bar on the bash, use this bash script expression:
echo -e '\033[37;44m\nHEADING\033[0m\nLorem ipsum ...'
In Ruby:
puts "\033[37;44m\n #{text}\033[0m" # blue bar
Notes: -e
turns on escape character interpretation for echo
. See this card for details on bash formatting.
The line above will print:
Some nifty Rails Rake tasks
Did you know?
rake stats # => LOC per controllers, models, helpers; code ratios, and more
rake notes # => collects TODO, FIXME and other Tags from comments and displays them
rake about # (Rails 3+) => Rails, Ruby, Rake, Rack etc. versions, used middlewares, root dir, etc.
How to force a client-side refresh on a new favicon
Browsers usually cache favicons. If you update the favicon of your web site and want all visitors to see the new icon, you need to give the icon a different URL. You will not have this issue if you cache your assets properly, which appends a fingerprint to your image URL (like favicon.ico?432423432
):
<link href="<%= image_path('favicon.ico') %>" rel="icon" type="image/vnd.microsoft.icon">
String#indent: Know your definitions!
String#indent
is not a standard Ruby method. When you use it, be sure to know where this method comes from. Many Gems shamelessly define this method for internal usage, and you'll never know when it may be removed (since it's usually not part of the Gem's API).
Unless you're using Rails 4 (which brings String#indent
in ActiveSupport), you'll be best of defining it yourself. This card has it for you.
Gems that define String#indent
(incomplete)
----------------------------...
Spreewald version 0.9.4 released
- "I click on ..." step fixed
- Errors added to
ToleranceForSeleniumSyncIssues::RETRY_ERRORS
Capybara::ElementNotFound
Selenium::WebDriver::Error::ElementNotVisibleError
Selenium::WebDriver::Error::NoSuchFrameError
Fix when assigning nested attributes raises "undefined method `to_sym' for nil:NilClass"
You might have a table without a primary key set in MySQL.
You can fix this by adding a primary key index to the guilty MySQL table, or by setting self.primary_key = "id"
in your class definition.
Related, but different issue: Rails 2 does not find an association when it is named with a string instead of a symbol
Debugging in Cucumber
Spreewald includes a few useful steps for debugging a Capybara session.
Then show me the page # => Opens the page in the browser
Then debugger # => Open a debugging session
Unicode support in MySQL is ...
For reasons that completely escape me, MySQL 5.x limits UTF-8 strings to U+FFFF and smaller.
Cucumber does not find neither env.rb nor step definitions when running features in nested directories
Usually, Cucumber feature files live in features/
. When you group them in sub directories, make sure to add -r features
to the standard Cucumber options.
In Rails apps, Cucumber options are likely to be stored in config/cucumber.yml
.
Cucumber: Wait until CKEditor is loaded
I had to deal with JavaScript Undefined Error
while accessing a specific CKEditor instance to fill in text.
Ensure everything is loaded with
patiently do
page.execute_script("return isCkeditorLoaded('#{selector}');").should be_true
end
Example
The fill in text
snippet for Cucumber:
When /^I fill in the "([^\"]+)" WYSIWYG editor with:$/ do |selector, html|
patiently do
page.execute_script("return isCkeditorLoaded('#{selector}');").should be_true
end
html.gsub!(/\n+/, "") # otherwise: unterminated string lit...
Using Arel to Compose SQL Queries
Arel is a library that was introduced in Rails 3 for use in constructing SQL queries. Every time you pass a hash to where, it goes through Arel eventually. Rails exposes this with a public API that we can hook into when we need to build a more complex query.
Namespacing: why `uninitialized constant` error may occour in `development` but not in `test` environment
Example:
class Book::Page
end
class MyBook < Book
def new_page
Page.new # has to be `Book::Page` in development to make it work
end
end
Method new_page
may throw an error when it was called during browser interaction in development but doesn't make the test fail.
The reason
Development autoloading isn't smart enough to find the referenced class
At other environments (test, staging, production) autoloading is disabled, that all classes are already loaded when browser interaction takes place what makes...
Howto prompt before accidentally discarding unsaved changes with JavaScript
Ask before leaving an unsaved CKEditor
Vanilla JavaScript way, but removes any other onbeforeunload
handlers:
$(function(){
document.body.onbeforeunload = function() {
for(editorName in CKEDITOR.instances) {
if (CKEDITOR.instances[editorName].checkDirty()) {
return "Unsaved changes present!"
}
}
}
}
A robuster implementation example
Note: Don't forget to mark the 'search as you type' forms with the skip_pending_changes_warning
class.
var WarnBeforeAccidentallyDiscard...
Make custom web font available within CKEditor content
-
Add to
ckeditor/config.js
CKEDITOR.editorConfig = function(config) { config.contentsCss = [ '/assets/myCkeditorStyles.css', // any other file to encapsulate custom styles '/assets/myFontFaceTags.css' ]; }
It's not enough to provide the font face tags within your public folder. You have to explixitly call it within the ckeditor/config.js
.
...
Redis Desktop Manager
"Redis Desktop Manager (aka RDM)— is a cross-platform open source Redis DB management tool (i.e. Admin GUI). Redis Desktop Manager developed to replace a hundreds of slow and ugly tools for redis."
Rails 4 Engines - TechRabbit
At TaskRabbit, we have gone through a few iterations on how we make our app(s). In the beginning, there was the monolithic Rails app in the standard way with 100+ models and their many corresponding controllers and views. Then we moved to several apps with their own logic and often using the big one via API. Our newest project is a single “app” made up of several Rails engines. We have found that this strikes a great balance between the (initial) straightforwardness of the single Rails app and the modularity of the more service-oriented ar...
How to call overwritten methods of parent classes in Backbone.js
When you are working with Backbone models and inheritance, at some point you want to overwrite inherited methods but call the parent's implementation, too.
In JavaScript, there is no simple "super
" method like in Ruby -- so here is how to do it with Backbone.
Example
BaseClass = Backbone.Model.extend({
initialize: function(options) {
console.log(options.baseInfo);
}
});
MyClass = BaseClass.extend({
initialize: function(options) {
console.log(options.myInfo);
}
});
ne...
Removing Dot-Underscore Files ~ Kadin2048's Weblog
This is just a quick note, mostly for my own reference, of a few ways to easily delete the dot-underscore (._foo, ._bar, etc.) files created by (badly-behaved) Mac OS X systems on non-AFP server volumes.
RulersGuides.js demo
RulersGuides.js is a Javascript library which enables Photoshop-like rulers and guides interface on a web page
Also available as a bookmarklet.
Checking the character length of a text containing markup (e.g. WSYIWYG)
If you have a text that is edited by WSYIWYG-Editor but want some length checking nevertheless, you need to strip all tags and then the special characters:
def hard_sanitize(text)
ActionController::Base.helpers.strip_tags(text).gsub(/[^[:word:]]+/, " ")
end
:001 > hard_sanitize("This is <strong>beautiful</strong> <h1>markup<h1>")
=> "This is beautiful markup"
If you allready have nokogiri on board, you can use that as well, though it has no extra benefit:
:001 > Nokogiri::HTML("This is <strong>beau...
Stripping all non-word-characters (a.k.a \W) and preserve diacritics (a.k.a Umlaute) in utf-8
Sometimes you want to strip a text of every special char. If you use \W, the result might not be what you wanted:
irb> "Eine längliche Kristall §$&&%& Lampe über dem Esstisch verschönert jedes noch so kahle \n Speisezimmer".gsub(/\W+/, " ")
=> "Eine l ngliche Kristall Lampe ber dem Esstisch versch nert jedes noch so kahle Speisezimmer"
You need to use "[^[:word:]]", which is magically given to us by the spirit of the community
irb> "Eine längliche Kristall §$&&%& ...
Protip: Always leave a failing test
Mornings can be rough. To make them a little easier, leave yourself a failing test if your work isn’t finished. When you come back to the project, it makes it much easier to understand where you were and figure out what you still need to do.