Last active
April 12, 2017 10:26
-
-
Save garethrees/d632f1f8d6177d59331f63f4f3927ea8 to your computer and use it in GitHub Desktop.
Translated Constants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A | |
| class TrackThing | |
| # The module can either be inline, or in its own file | |
| module TranslatedConstants | |
| def track_types | |
| { 'request_updates' => _('Individual requests'), | |
| 'all_new_requests' => _('Many requests') } | |
| end | |
| end | |
| end | |
| class TrackThing < ActiveRecord::Base | |
| extend TranslatedConstants | |
| def some_method(key) | |
| self.class.track_types[key] | |
| end | |
| end | |
| # B | |
| class TrackThing < ActiveRecord::Base | |
| def some_method(key) | |
| TranslatedConstants.track_types[key] | |
| end | |
| end | |
| class TrackThing | |
| module TranslatedConstants | |
| def self.track_types | |
| { 'request_updates' => _('Individual requests'), | |
| 'all_new_requests' => _('Many requests') } | |
| end | |
| end | |
| end | |
| # C | |
| class TrackThing < ActiveRecord::Base | |
| def some_method(key) | |
| TranslatedConstants.track_types(key) | |
| end | |
| end | |
| class TrackThing | |
| module TranslatedConstants | |
| TRACK_TYPES = | |
| { 'request_updates' => Proc.new(_('Individual requests')), | |
| 'all_new_requests' => Proc.new(_('Many requests')) } | |
| def self.track_types(key) | |
| TRACK_TYPES[key].call | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment