Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mattetti/wd_sinatra_active_record
ActiveRecord helper for Weasel Diesel Sinatra apps. See https://github.com/mattetti/wd-sinatra
https://github.com/mattetti/wd_sinatra_active_record
Last synced: about 12 hours ago
JSON representation
ActiveRecord helper for Weasel Diesel Sinatra apps. See https://github.com/mattetti/wd-sinatra
- Host: GitHub
- URL: https://github.com/mattetti/wd_sinatra_active_record
- Owner: mattetti
- License: mit
- Created: 2012-11-14T00:59:33.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2012-11-14T20:47:18.000Z (about 12 years ago)
- Last Synced: 2024-11-27T12:44:39.112Z (2 months ago)
- Language: Ruby
- Size: 121 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# WdSinatraActiveRecord
A Ruby gem to avoid reinventing the wheel every time you want to use
`ActiveRecord` in a [WeaselDiesel](https://github.com/mattetti/Weasel-Diesel) app backed by Sinatra ([wd_sinatra](https://github.com/mattetti/wd-sinatra)).Use this gem to easily get connected to one or multiple databases and to
enjoy some of the common ActiveRecord Rake tasks available in Rails.## Installation
Add this line to your application's Gemfile:
gem 'wd_sinatra_active_record'
And then execute:
$ bundle
Or install it yourself as:
$ gem install wd_sinatra_active_record
Don't forget to set a gem dependency for the DB adapter you need.
For instance:mysql2
## Usage
Add an ActiveRecord `database.yml` file in your config folder and then require this
gem in your `app.rb` file and connect to the DB:require 'wd_sinatra_active_record'
WdSinatraActiveRecord::DBConnector.set_db_connection
WdSinatraActiveRecord::DBConnector.connect_to_db unless ENV['DONT_CONNECT']The DB settings can be accessed via:
DBConnector::DB_CONFIG[RACK_ENV]
If you need a secondary DB connection (to another DB for instance),
add a new entry to your `database.yml` config file:development:
adapter: mysql2
encoding: utf8
reconnect: true
database: app_development
pool: 5
username: root
password:
socket: /tmp/mysql.socksecondary:
adapter: mysql2
encoding: utf8
reconnect: true
database: secondary_development
pool: 5
username: root
password:
socket: /tmp/mysql.sockThen in your `app.rb` after requiring this gem:
class SecondaryConnection < ActiveRecord::Base
self.abstract_class = true
end
secondary_config = DBConnector::DB_CONFIG[RACK_ENV]['secondary']
SecondaryConnection.establish_connection(secondary_config) unless ENV['DONT_CONNECT']Then whichever `ActiveRecord` that needs to connect to the secondary DB
can inherit from `SecondaryConnection` instead of `ActiveRecord::Base`.## Rake tasks
A Rake task file is also provided so you can load ActiveRecord specific
tasks. To do that, create a new rake file in your lib/tasks folder, load
`WDSinatra` and the rake task file:```
$ echo "require 'wd_sinatra_active_record'
load WdSinatraActiveRecord.task_path" > lib/tasks/db.rake
```The tasks are very basic and mainly copied from Rails, feel free to send
patches and improvements.(Note: WDSinatra version 1.0.4 or newer required)
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request