Sha256: 4d79a1b0d87b4fc59329bbb9632def76c7678d51ddf90692721ad964d54f4a20

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'i18n'

#
# Use as follows
# class Thing < ActiveRecord::Base
#   include ActsAsReadOnlyI18nLocalised
#   validates :slug, format: {with: /^[a-z]+[\-?[a-z]*]*$/},
#                    uniqueness: true,
#                    presence: true
#   acts_as_read_only_i18n_localised :name
# end
#
# thing = Thing.create(stub: 'test')
# puts(thing.name)
#
module ActsAsReadOnlyI18nLocalised
  def self.included(base)
    base.extend(ClassMethods)
  end

  #
  # Standard Ruby idiom for auto-adding class methods
  #
  module ClassMethods
    def underscore(string)
      string
    end

    def acts_as_read_only_i18n_localised(*attributes)
      attributes.each do |attribute|
        define_method attribute do
          I18n.t("#{self.class.name.gsub(/::/, '/')
                                    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
                                    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
                                    .tr('-', '_')}.#{send(:slug)}.#{attribute}"
                 .downcase.to_sym)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_read_only_i18n_localised-0.0.1 lib/acts_as_read_only_i18n_localised.rb