Skip to content

Instantly share code, notes, and snippets.

@garethrees
Last active April 12, 2017 10:26
Show Gist options
  • Select an option

  • Save garethrees/d632f1f8d6177d59331f63f4f3927ea8 to your computer and use it in GitHub Desktop.

Select an option

Save garethrees/d632f1f8d6177d59331f63f4f3927ea8 to your computer and use it in GitHub Desktop.
Translated Constants
# 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