Ruby: Find the most common string from an array
This will give you the string that appears most often in an array:
names = %w[ foo foo bar bar bar baz ]
names.group_by(&:to_s).values.max_by(&:size).try(:first)
=> "bar"
This is very similar to the linked StackOverflow thread, but does not break on empty arrays.
Note that try is provided by ActiveSupport (Rails). You could explicitly load activesupport or use andand on plain Ruby.
XHR is not JSON
When a Rails controller action should handle both HTML and JSON responses, do not use request.xhr? to decide that. Use respond_to.
I've too often seen code like this:
def show
# ...
if request.xhr?
render json: @user.as_json
else
# renders default HTML view
end
end
This is just plain wrong. Web browsers often fetch JSON via XHR, but they (should) also send the correct Accept HTTP header to tell the server the data they expect to receive.
If you say request.xhr? as a means for "wants JSON" you are ...
How to stub class constants in RSpec
Hint: There's another card with this helper for Cucumber features.
Sometimes you feel like you need to stub some CONSTANT you have defined in an other class. Since actually constants are called constants because they're constant, there's no way to easily stub a constant.
Here are three solutions for you.
Easiest solution
Rethink! Do you really need CONSTANT = %w[foo bar] to be constant? In many cases, setting it as a...
Run multiple local webricks at the same time using different ports
If you specify different ports, you can run multiple local webricks with rails server --port=300X at the same time.
We are no longer maintaining Dusen
If you were using Dusen for its query parsing and LIKE queries, we recommend to migrate to Minidusen, which extracts those parts from Dusen. Minidusen is compatible with MySQL, PostgreSQL and Rails 3.2, 4.2 and 5.0.
If you are looking for a full text indexing solution, we recommend to use PostgreSQL with pg_search.
Git: Issues with Gemfile.lock
When there's a Gemfile.lock in your working directory that you cannot remove by either checkout, reset [--hard], stash, probably Rails' Spring is the culprit and not Bundler itself.
Fix
spring stop
The author of the linked Stackoverflow post supposes Spring re-writes the Gemfile.lock on change to ensure all Spring processes are using the same gem versions. Meh.
danialfarid/angular-file-upload
Lightweight Angular JS directive to upload files
Includes polyfills for old IEs. Unfortunately, their auto-loading mechanism may not work properly on your Rails application due to the asset pipeline. They use FileAPI, so you could just include it manually for old browsers, or when you want to use FileAPI's toolkit anyway.
How much should I refactor?
The Rails community has been abuzz with object-oriented programming, SOLID principles, laws, design patterns, and other principles, practices, and patterns. We’ve (re)discovered new tools and techniques to separate and reuse logic, making code easier to test, understand, and maintain. Now that we’ve learned about all these new tools, when do we use them?
Pattern: Disabling a certain feature in tests
There is a kind of features in web applications that hinder automated integration tests. Examples include cookie consent banners or form captchas. Clearly, these should be disabled so you do not have to explicitly deal with them in each and every test (like, every test starting with accepting the cookies notice). On the other hand, they must be tested as well.
A good feature disabling solution should therefore meet these requirements:
-
The feature is generally disabled in tests. A test does not need to do anything manually.
-
It is *...
Incredibly fast web apps // Speaker Deck
Presentation about optimizing Ruby on Rails apps.
From Nico Hagenburger (homify's lead frontend developer).
Databound
Databound provides Javascript a simple API to the Ruby on Rails CRUD.
Tries to expose a full model CRUD as an API. Not sure if this is useful for more refined requirements.
ActiveRecord's where.not
Rails 4.0 introduced a helpful new method for ActiveRecord queries: where.not. It can make clunky queries easier to read.
RailsPanel chrome extension
Chrome extension that shows all info from your rails log (like parameters, response times, view rendering times, DB requests) inside a chrome panel.
Redactor WYSIWYG html editor
New WYSIWYG editor that claims to be lighter and prettier than TinyMCE and CKEditor. Has some Rails integration, too.
King of Nothing, the DCI paradigm is a scam
I’ve worked on huge applications in Ruby and Rails before. I very much want to believe in DCI, but I’m having a hard time accepting the promises of Clean Ruby when it seems like the work on this paradigm is half-done. If it weren’t so oversold and hyped, I think I’d be more patient, but right now I’m just frustrated and confused.
RubyMine 4.5.1: Sass/SCSS Support Improvements
A number of issues concerning Sass/SCSS autocompletion and syntax highlighting were submitted as a feedback for RubyMine 4.5. Web development with Rails can’t do without Sass/SCSS code writing so we’ve decided to fix the most annoying bugs as soon as possible.
Modifying Rake Tasks - Dan Manges's Blog
For custom Rake tasks, you shouldn't need to modify them after the original definition. However, if you want to add behavior to some vendor tasks (such as those defined with Rails), this blog post will cover how to do that.
Difference between class_inheritable_attribute and class_attribute | martinciu's dev blog
How to make class_attribute behave like class_inheritable_attribte which no longer exists in Rails.
Aspect Oriented Programming in Ruby
Slides presenting ways to integrate the ideas of Aspect-Oriented Programming in Ruby.
Outline
- Why Aspect-Oriented Programming?
- AOP in Java and AspectJ (a Review).
- AOP in Ruby.
- What you can do today.
- Example AOP-isms in Ruby on Rails.
- Aspect-Oriented Design.
- The AOP Promise for Tomorrow.
fnando/i18n-js
A small library to provide the Rails I18n translations in Javascript clients.
Update: Aggregated RSpec/Cucumber test coverage with RCov
Our rcov:all task for aggregated RSpec/Cucumber coverage was overhauled extensively. Among other things it now works for Rails 2 and 3 and has an option to ignore shared traits.