Sha256: e662e918ddc1290554ff4563a6f1a006c206be1d2c647fcce42f08fa90debdad
Contents?: true
Size: 801 Bytes
Versions: 18
Compression:
Stored size: 801 Bytes
Contents
module Spotlight # Mixin for adding translatable ActiveRecord accessors module Translatables extend ActiveSupport::Concern class_methods do def translates(*attr_names) attr_names.map(&:to_sym) attr_names.map(&method(:define_translated_attr_reader)) end ## # Set up a reader for the specified attribute that uses the I18n backend, # and defaults to the ActiveRecord value def define_translated_attr_reader(attr_name) define_method(:"#{attr_name}") do I18n.translate(attr_name, scope: slug, default: attr_translation(attr_name)) end end end private ## # Will return the default ActiveRecord value for the value def attr_translation(attr_name) self[attr_name] end end end
Version data entries
18 entries across 18 versions & 1 rubygems