Module: CckForms::ParameterTypeClass::Base::ClassMethods

Defined in:
lib/cck_forms/parameter_type_class/base.rb

Instance Method Summary collapse

Instance Method Details

#codeObject

CckForms::ParameterTypeClass::Checkboxes -> ‘checkboxes’ CckForms::ParameterTypeClass::RichText -> ‘rich_text’



116
117
118
# File 'lib/cck_forms/parameter_type_class/base.rb', line 116

def code
  self.to_s.demodulize.underscore
end

#demongoize(something_from_database) ⇒ Object

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



68
69
70
# 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) ⇒ Object

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



74
75
76
# 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) ⇒ Object

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.



104
105
106
107
# 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) ⇒ Object

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



110
111
112
# 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) ⇒ Object

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



80
81
82
83
84
85
86
87
# 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

#nameObject

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



121
122
123
# File 'lib/cck_forms/parameter_type_class/base.rb', line 121

def name
  I18n.t("cck_forms.parameter_type_name.#{code}")
end