Exporting to Excel from Rails without a gem
See this Railscast.
Basically you can simply write views like index.xlsx.erb
:
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell><Data ss:Type="String">ID</Data></Ce...
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...
Rails: How to provide a public link in a mail
Lets say we have a user
with a contract
whereas contract
is a mounted carrierwave file.
Now we want to send the link to the contract in a mail. For this use case join the root_url
with the public contract path in the mailer view:
Plain text email
URI.join(root_url, @user.contract.url)
HTML email
link_to('Show contract', URI.join(root_url, @user.contract.url).to_s)
Note: You need to follow [http://guides.rubyonrails.org/action_mailer_basics.html#g...
Introducing GoodJob 1.0, a new Postgres-based, multithreaded, ActiveJob backend for Ruby on Rails
GoodJob is a new background worker gem. It's compatible with ActiveJob.
We're huge fans of Sidekiq for its stability and features. One advantage of GoodJob over Sidekiq is that GoodJob doesn't require Redis. So in cases where you don't have Redis or don't want to pay for a Redis HA quorum node, this might be an alternative worth checking out.
wycats/artifice
Artifice allows you to replace the Net::HTTP subsystem of Ruby with an equivalent that routes all requests to a Rack application.
You can use Sinatra, raw Rack, or even Rails as your application, allowing you to build up an equivalent to the remote service you are mocking out using familiar and convenient tools to route requests and build up responses.
Rails 4.2 Foreign Key Support
The migration DSL now supports adding and removing foreign keys. They are dumped to schema.rb as well. At this time, only the mysql, mysql2 and postgresql adapters support foreign keys. @rubyonrails
Workings
add_foreign_key(:comments, :users)
adds a database constraint
ALTER TABLE "comments" ADD CONSTRAINT comments_user_id_fk FOREIGN KEY ("user_id") REFERENCES "user" ("id")
Disadvantage
Foreign key constraints double the validatio...
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.
Rails 3/4: How to add routes for specs only
If you want to have routes that are only available in tests (e.g. for testing obscure redirects), you can use the with_routing
helper -- but that one destroys existing routes which may break a specs that require them to work.
To keep both "regular" and test routes, do this:
class MyApplicationController < ActionController::Base
def show
render text: 'Welcome to my application'
end
end
test_routes = Proc.new do
get '/my_application' => 'my_application#show'
end
Rails.application.routes.ev...
Hack of the day: A reverse for Rails' &:
Ever wondered if there is a reverse for Rails' .each(&:to_s)
idiom? Turns out there is...
You probably already know, that you can abbreviate code like
dogs.each { |dog| dog.bark! }
with
dogs.each(&:bark!)
Now suppose it is the other way round and you have
bones.each { |bone| dog.eat!(bone) }
Good old Ruby already has a solution:
bones.each(&dog.method(:eat!))
dbconsole in Rails 3 requires the environment as the first argument
There is a bug in Rails 3's dbconsole
script, which makes the following command open a database console for the development
environment:
rails dbconsole -p test
You need to write this instead:
rails dbconsole test -p
Passenger booting Rails 3 application in wrong environment
Passenger gives you the possibility to define in which environment your app should be started.
This has to be added to the VirtualHost configuration for Apache for Rails 2 applications:
RailsEnv development
When running Rails 3, you need
RackEnv development
postgres_ext: additional Rails bindings for PostgreSQL
Adds missing native PostgreSQL data types to ActiveRecord and convenient querying extensions for ActiveRecord and Arel for Rails 4.x
Common table expressions
- Relation#with
- Model.from_cte
Arrays
Face.where.contains tags: %w[happy smiling] # Matching faces have both 'happy' and 'smiling' tags
Face.where.overlap tags: %w[happy smiling] # Matching faces have at least one of these tags
Face.where.any tags: 'happy' # Matching faces include the 'happy' tag
Face.where.all tags: 'dunno' # Not documented, try for yourself
...
Failtale
Free Hoptoad/Airbrake alternative which can capture exceptions from any platform. It comes with a Rails notifier and a RESTful API to write your own notifiers for Javascript, etc.
Should we some day wish to do away with the current way we deal with exceptions, this might be a good place to start.
makandra/consul
Our new scope-based authorization gem for Ruby on Rails has been released. This might one day replace Aegis as our standard authorization solution.
plus2/whereuat - GitHub
Adds a slide out panel to your Rails application that directs clients to test stories that have been marked as 'delivered' in Pivotal Tracker.
lib/rucksack_extras.rb at master from jamesu's rucksack — GitHub
Wrapper from Globalite API to the Rails 2.2 I18n API
SASS: The Better, More Powerful CSS - Intridea Design Blog
I am a huge fan of SASS (Syntactically Awesome Stylesheets) for styling Rails applications. I have been using it on all of my projects for quite a while now and have developed some great techniques that make it much easier to organize, write, and read stylesheets in an application
Agile Ajax » The Incredible Rising Version Number » Pathfinder Development
Noel Rappin discusses ways to keep up with edge Rails.
justinfrench's formtastic at master - GitHub
A Rails FormBuilder with semantically rich and accessible markup.
Tagaholic - Console Update With Your Editor
Rails’ script/console makes it easy to fetch, view and edit your database records. But can you edit those records as quickly as you edit code in your text editor? Riiight, like editing our database records in an editor is gonna happen? It already has.
pluginaweek's plugin_test_helper at master - GitHub
Simplifies plugin testing by creating an isolated Rails environment that simulates its usage in a real application
sandofsky's acts_against_douchebar at master - GitHub
A Rails plugin that delivers a special message to DiggBar users, or runs frame-killer javascript.
fesplugas's typus at master - GitHub
Effortless backend interface for Ruby on Rails applications. (Admin scaffold generator.)