Module: RailsDictionary::ActsAsDictType::ClassMethods

Defined in:
lib/rails_dictionary/acts_as_dict_type.rb

Instance Method Summary collapse

Instance Method Details

#all_typesObject



10
11
12
13
# File 'lib/rails_dictionary/acts_as_dict_type.rb', line 10

def all_types
  whole_types ||
    self.whole_types = all.map(&:name).map(&:to_sym)
end

#revert(arg) ⇒ Object

short method to transfer id to name or name to id TODO: cache it



17
18
19
20
21
22
23
# File 'lib/rails_dictionary/acts_as_dict_type.rb', line 17

def revert(arg)
  if arg.is_a?(String)
    DictType.where(name: arg).first.try(:id)
  elsif arg.is_a?(Integer)
    DictType.where(id: arg).first.try(:name)
  end
end

#tab_and_columnObject

Parse the name value to get which column and model(or table) are listed in DictType

Programmer DOC:

There are two chooses to get subclass,one is subclasses the other is descendants,
I don't know which is better,but descendants contains subclass of subclass,it contains more.

Class like +Ckeditor::Asset+ transfer to "ckeditor/asset",but we can not naming method like that,
So it still not support, the solution may be simple,just make another convention to escape "/"

Seems this method did not need to be cached in production.
Because everyclass was cached before application was run.So after application was run, it never be run again.

TODO:

To cache this method output need more skills on how to caculate ActiveRecord::Base.descendants
Temply remove the cache
And add test for this situation


41
42
43
44
# File 'lib/rails_dictionary/acts_as_dict_type.rb', line 41

def tab_and_column
  all_model_class=ActiveRecord::Base.descendants.map(&:name).map(&:underscore)
  all_types.map(&:to_s).extract_to_hash(all_model_class)
end