https://github.com/pinzolo/mastar
MASTer table on Active Record
https://github.com/pinzolo/mastar
Last synced: 7 months ago
JSON representation
MASTer table on Active Record
- Host: GitHub
- URL: https://github.com/pinzolo/mastar
- Owner: pinzolo
- License: mit
- Created: 2012-10-05T13:02:07.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2014-09-24T06:49:47.000Z (about 11 years ago)
- Last Synced: 2024-10-23T14:16:49.842Z (12 months ago)
- Language: Ruby
- Homepage: http://rubygems.org/gems/mastar
- Size: 340 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mastar
[](http://travis-ci.org/pinzolo/mastar)
[](https://coveralls.io/r/pinzolo/mastar)Add some features to master table class on ActiveRecord.
## Installation
Add this line to your application's Gemfile:
gem 'mastar'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mastar
## Usage
1. Call `include Mastar` in your ActiveRecord classes.
2. Set key column name by way of `mastar.key` method.## Feature
### Ruby code
```ruby
class Country < ActiveRecord::Base
include Mastar
mastar.key :code
endclass City < ActiveRecord::Base
belongs_to :country
end
```
### Countries table data| id | name | code |
|---:|:-----|:-----|
|1 |USA |us |
|2 |Japan |jp |1. Direct access by key method.
* `Country.jp` returns `#`
* `Country.jp(:name)` returns `"Japan"`
* `Country.jp(:code, :name, :id)` returns `["jp", "Japan", 2]`
2. Judge method by key. (ex. `City.first.country.jp?`)
3. `.get` method like as `.find`, `.get` preferentially returns cached object.
* `Country.find` returns from DB.
* `Country.get` returns from inner cache when already cached.
* When not cached, `Country.get` returns from DB, and cache it.
4. `.pairs` method for writing select element shortly in Rails.
```ruby
# Normal
f.select :country_id, Country.all.map { |c| [c.name, c.id] }
f.collection_select :country_id, Country.all, :id, :name
# In using Mastar
f.select :country_id, Country.pairs
```
## Supported versions- Ruby: 1.9.3, 2.0.0, 2.1.0
- ActiveRecord: 3.2.x, 4.0.x## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request