module CckForms::ParameterTypeClass::Base::ClassMethods

Public Instance Methods

code() click to toggle source

CckForms::ParameterTypeClass::Checkboxes -> 'checkboxes' CckForms::ParameterTypeClass::RichText -> 'rich_text'

# File lib/cck_forms/parameter_type_class/base.rb, line 116
def code
  self.to_s.demodulize.underscore
end
demongoize(something_from_database) click to toggle source

Called on a class to construct its instance from a MongoDB Hash. By default simply create a new object.

# File lib/cck_forms/parameter_type_class/base.rb, line 68
def demongoize(something_from_database)
  new value: demongoize_value(something_from_database)
end
demongoize_value(value, parameter_type_class=nil) click to toggle source

Only converts a value from MongoDB to its in-memory (Ruby) form. By default, return the value itself.

# File lib/cck_forms/parameter_type_class/base.rb, line 74
def demongoize_value(value, parameter_type_class=nil)
  value
end
emit_map_reduce(feild_name) click to toggle source

Returns Javascript emit function body to be used as a part of map/reduce “emit” step, see docs.mongodb.org/manual/applications/map-reduce/

The reason for this is every type class has its own notion of “current value” and stores it specifically. Say, Checkboxes store an array of values and if we want to get distinct values we need a way to extract each “single value” from this array.

Example: imagine a field “city” of type Checkboxes. To make an aggregate query (a subtype of map-reduce) to count objects in different cities, for example, we can not run emit for the field as a whole since it is an array. We must call emit for each array value, that is for each city ID. This method does exactly this.

In particular, it is used in counting popular values (like “give me a list of cities sorted by the number of host objects (ads? companies?) in them”).

By default considers a value in “#{feild_name}” atomic and call emit for it.

# File lib/cck_forms/parameter_type_class/base.rb, line 104
def emit_map_reduce(feild_name)
  field_name = 'this.' + feild_name
  "if(#{field_name} && #{field_name} != '') emit(#{field_name}, 1)"
end
form_name_to_id(name) click to toggle source

Converts input name intp HTML ID, e.g. facility[1] -> facility_cck_params_1_value.

# File lib/cck_forms/parameter_type_class/base.rb, line 110
def form_name_to_id(name)
  name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, '_').sub(/_\z/, '')
end
mongoize(object) click to toggle source

Called on an class to get a MongoDB Hash form of an instance object. By default simply calls mongoize of the instance.

# File lib/cck_forms/parameter_type_class/base.rb, line 80
def mongoize(object)
  case object
  when self then object.mongoize
  # TODO: why only these classes? does any scalar fit?
  when Hash, Array, String then new(value: object).mongoize
  else object
  end
end
name() click to toggle source

A type name, e.g. “A text string”

# File lib/cck_forms/parameter_type_class/base.rb, line 121
def name
  I18n.t("cck_forms.parameter_type_name.#{code}")
end