Module: I18nScopes::Scope::ClassMethods

Defined in:
lib/i18n_scopes/scope.rb

Instance Method Summary collapse

Instance Method Details

#i18n_default_scope(*path) ⇒ Object

this method creates a default scope this is equivalent to calling

i18n_scope :default, :as => :t_scoped


57
58
59
60
61
62
# File 'lib/i18n_scopes/scope.rb', line 57

def i18n_default_scope(*path)
  options = path.extract_options!
  options[:as] ||= :t_scoped
  path << options
  i18n_scope(:default, *path)
end

#i18n_scope(name, *path) ⇒ Object

Use this method to configure how the scope will be built when using translations

Params:

  • path: Either a single String or Symbol, or an Array of Symbols and Strings

  • options: options to be used within this class, they will overwrite the configuration made in Configuration

    • :plural_classes: if the class name should be pluralized or singularized

    • :strip_controller_suffix: if the _controller suffix of classes should be stripped or not

Usage:

i18n_scope :flash, :modules, :class_name, :action_name, :custom_value, "static_prefix", :plural_classes => true, :strip_controller_suffix => true

this will create a t_flash method

with a Tweets::PostsController, the action index and a custom_value method that returns a_value this will led to the following scope:

[:tweets, :posts, :index, :a_value, "static_prefix"]


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/i18n_scopes/scope.rb', line 38

def i18n_scope(name, *path)
  @i18n_scope_initialized ||= begin
    self.send :include, I18nScopes::TranslationMethods
    self.i18n_scope_helper ||= {}
    true
  end

  options = path.extract_options!
  as = options.delete(:as) || :"t_#{name}"
  self.i18n_scope_helper[name] = I18nScopes::TranslationHelper.new(path.flatten, options)

  define_method(as) do |*args|
    scoped_translation(self.class.i18n_scope_helper[name], *args)
  end
end

#inheritable_attr(name) ⇒ Object

defines an inheriteable attribut which clones the parents value

Params:

  • name: the attribtues name



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/i18n_scopes/scope.rb', line 67

def inheritable_attr(name)
  instance_eval <<-eos
    def #{name}=(v)
      @#{name} = v
    end

    def #{name}
      return @#{name} unless superclass.respond_to?(:#{name}) and value = superclass.#{name}
      @#{name} ||= value.clone # only do this once.
    end
  eos
end