https://github.com/sage/rake_helper
A set of common helper methods to DRY up Rails rake tasks
https://github.com/sage/rake_helper
Last synced: about 1 year ago
JSON representation
A set of common helper methods to DRY up Rails rake tasks
- Host: GitHub
- URL: https://github.com/sage/rake_helper
- Owner: Sage
- License: apache-2.0
- Archived: true
- Created: 2016-08-01T02:23:52.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-09-19T07:34:59.000Z (almost 7 years ago)
- Last Synced: 2025-03-12T21:28:13.887Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 25.4 KB
- Stars: 4
- Watchers: 16
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: change_log/next/.gitkeep
- License: LICENSE
Awesome Lists containing this project
README
# rake_helper [](https://travis-ci.org/Sage/rake_helper) [](https://codeclimate.com/github/Sage/rake_helper/maintainability) [](https://codeclimate.com/github/Sage/rake_helper/test_coverage) [](https://badge.fury.io/rb/rake_helper)
A set of common helper methods to DRY up Rails rake tasks
## Installation
Add to your project's Gemfile:
```ruby
gem 'rake_helper'
```
Run:
```
bundle install
```
Include in your project's Rakefile:
```ruby
include RakeHelper
```
## Usage
### Logging Messages
These output a timestamped `puts` statement in the terminal for the benefit
of the person running the rake task, but also log a message in the Rails
log in case it is missed in a long stream of output.
There are 3 predefined methods which prepend a standardized keyword:
#### start
Logs as type `:info`
```ruby
start('Updating user records')
# => 2016-08-07 13:02:51 -0400 START: Updating user records
```
#### finish
Logs as type `:info`
```ruby
finish('Updating user records')
# => 2016-08-07 13:04:28 -0400 FINISH: Updating user records
```
#### failure
Logs as type `:error`
```ruby
failure("Updating user records: #{e}")
# => 2016-08-07 13:03:16 -0400 FAILURE: Updating user records: SomeError
```
The standard logger severities can also be called directly as methods with a
single `message` parameter:
* `warn`
* `unknown`
* `info`
* `fatal`
* `debug`
* `error`
For example:
```ruby
info('Useful information')
# => 2016-08-07 13:03:16 -0400 Useful information
```
### Running SQL Statements
The `run_sql` method will execute one or more SQL statements and return the
results as an `Array`. The first param is required and should be a valid SQL
string. The string can include several statements separated by semicolons.
You can pass an `action` option which should be any valid
[ActiveRecord::ConnectionAdapters::DatabaseStatements](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html)
method name as a symbol.
#### :update example
```ruby
sql = <<-SQL
UPDATE users SET activated = 1 WHERE created_at > 2016-01-01;
UPDATE businesses SET name = 'Widgets Inc' WHERE id = 22;
SQL
run_sql(sql)
# OR capturing the record count for each query:
results = run_sql(sql, action: :update)
info("User count: #{results.first}, Business count: #{results.last}")
# => 2016-08-07 13:11:45 -0400 "User count: 10, Business count: 1"
```
#### :select_value example
```ruby
sql = <<-SQL
SELECT id FROM users WHERE email = 'bob@example.com';
SQL
results = run_sql(sql, action: :select_value)
info("Bob's ID: #{results.first}")
# => 2016-08-07 13:11:45 -0400 "Bob's ID: 15"
```
#### :delete example
```ruby
sql = <<-SQL
DELETE FROM users WHERE created_at < 2016-01-01 AND activated = 0;
SQL
run_sql(sql)
# OR capturing the record count for each query:
results = run_sql(sql, action: :delete)
info("Num users deleted: #{results.first}")
# => 2016-08-07 13:11:45 -0400 "Num users deleted: 52"
```
### Full Example
#### Rake File
```ruby
# lib/tasks/update_locales.rake
desc 'Update US locales to en-US'
task update_locales: :environment do
message = 'Updating US locales'
start(message)
sql = <<-SQL
UPDATE users SET locale = 'en-US' WHERE locale IS NULL OR locale = 'en';
UPDATE countries SET locale = 'en-US' where code = 'US';
SQL
begin
run_sql(sql)
finish(message)
rescue Exception => e
failure(e)
end
end
```
#### Successful Run
```sh
$ bundle exec rake update_locales
2016-08-07 13:57:56 -0400 START: Updating US locales
2016-08-07 13:57:56 -0400 FINISH: Updating US locales
```
#### Failed Run
```sh
$ bundle exec rake update_locales
2016-08-07 14:07:50 -0400 START: Updating US locales
2016-08-07 14:07:50 -0400 FAILURE: ActiveRecord::ConnectionTimeoutError
```