module CckForms::ParameterTypeClass::Base
Base
module for all field types. Is included in type classes like Album
, Image
etc.
class CckForms::ParameterTypeClass::NewType include CckForms::ParameterTypeClass::Base end
Standalone usage of real classes:
field :cover_photo, type: CckForms::ParameterTypeClass::Image field :gallery, type: CckForms::ParameterTypeClass::Album field :description, type: CckForms::ParameterTypeClass::Text
Base
module includes:
1) URL helpers like edit_article_path accessible via include Rails.application.routes.url_helpers; 2) methods cck_param & value returning current CCK parameter (module CckForms::*::Parameter) and his current value; 3) methods with_cck_param(param) do ..., with_value(value) do ... and with_cck_param_and_value(param, value) do ... which set the corresponding values for the block duration; 4) dynamic method (via method_missing & respond_to?) ..._with_value(value, args*) which is basically the same as with_value do... but for one method invocation only: foo_with_value(v) <=> with_value(v) { foo } 5) utility set_value_in_hash(hash) placing value in hash[:value]; 6) utilities to get HTML ID: form(_builder)?_name_to_id; 7) methods to be consumed by the type classes: self.code - type code (e.g. rich_text for CckForms::ParameterType::RichText) self.name - type name (e.g. "A text string")
Public Class Methods
Load all type classes TODO: relies on all classes to reside in this class' directory
# File lib/cck_forms/parameter_type_class/base.rb, line 130 def self.load_type_classes return if @type_classes_loaded path = File.dirname(__FILE__) Dir[path + '/*.rb'].each do |filename| require_dependency filename unless filename.ends_with? '/base.rb' end @type_classes_loaded = true end
Store options into instance variables like @valid_values and so on
# File lib/cck_forms/parameter_type_class/base.rb, line 44 def initialize(options) options = options.symbolize_keys self.value = options[:value] if value.is_a?(Hash) && value.has_key?('value') self.value = self.value['value'] end valid_values = options.delete(:valid_values).presence valid_values_class_name = options.delete(:valid_values_class_name) cck_parameter = options.delete(:cck_parameter) @valid_values = valid_values @valid_values_class_name = valid_values_class_name @cck_parameter = cck_parameter @extra_options = options[:extra_options] || options.dup end
Public Instance Methods
Builds an edit form in HTML. By default an input:text (value can be set via options).
# File lib/cck_forms/parameter_type_class/base.rb, line 205 def build_form(form_builder, options) set_value_in_hash options form_builder.text_field :value, options end
For Rails.application.routes.url_helpers
# File lib/cck_forms/parameter_type_class/base.rb, line 234 def default_url_options {} end
# File lib/cck_forms/parameter_type_class/base.rb, line 244 def demongoize_value self.class.demongoize_value value, self end
# File lib/cck_forms/parameter_type_class/base.rb, line 248 def demongoize_value! self.value = demongoize_value end
Transforms the value into MongoDB form. By default returns the value itself.
# File lib/cck_forms/parameter_type_class/base.rb, line 240 def mongoize value end
Transforms DSL-like query (specific to each type) into Mongoid query. By default use simple where(field: query.to_s).
# File lib/cck_forms/parameter_type_class/base.rb, line 229 def search(selectable, field, query) selectable.where(field => query.to_s) end
Was-became HTML for admin panels etc. (like a type image with map preview for Map
).
# File lib/cck_forms/parameter_type_class/base.rb, line 223 def to_diff_value(options = nil) to_html options end
HTML form of a type class. By default to_s.
# File lib/cck_forms/parameter_type_class/base.rb, line 212 def to_html(options = nil) to_s options end
Redefines to allow passing of options. By default, call to_s
on the current value.
# File lib/cck_forms/parameter_type_class/base.rb, line 218 def to_s(options = nil) value.to_s end
If valid_values
is empty and valid_values_class
is not, extracts all values from this class into valid_values. Makes use of ActiveRecord-like method .all for this
# File lib/cck_forms/parameter_type_class/base.rb, line 193 def valid_values @valid_values ||= begin if vv_class = valid_values_class valid_values = {} vv_class.all.each { |valid_value_object| valid_values[valid_value_object.id] = valid_value_object.to_s } valid_values end end end
Generates valid values as a comma-separated string: “georgian: грузинская, albanian: албанская” (for HTML puproses)
# File lib/cck_forms/parameter_type_class/base.rb, line 158 def valid_values_as_string valid_values_enum.map { |enum| "#{enum[1]}: #{enum[0]}" }.join "\n" end
Convert HTML form back into Hash again:
= f.text_field :valid_values_as_string
# File lib/cck_forms/parameter_type_class/base.rb, line 164 def valid_values_as_string=(string) new_valid_values = {} string.split("\n").reject { |line| line.blank? }.each do |line| splitted = line.split(':', 2) new_valid_values[splitted[0].strip] = splitted[1].strip if splitted.length == 2 and splitted[0].present? end self.valid_values = new_valid_values end
“City” -> City
# File lib/cck_forms/parameter_type_class/base.rb, line 174 def valid_values_class if valid_values_class_name.present? if valid_values_class_name.is_a? Class valid_values_class_name else # raises exception if this is not a string valid_values_class_name.constantize end else nil end end
Is valid_values_class
exist at all?
# File lib/cck_forms/parameter_type_class/base.rb, line 187 def valid_values_class? not valid_values_class.nil? end
Generates valid_values
in form consumable to SELECT helper builders: [[key1, value1], [key2, value2], …]
# File lib/cck_forms/parameter_type_class/base.rb, line 146 def valid_values_enum valid_values = self.valid_values return [] if valid_values.blank? result = [] method_for_enumerating = valid_values.is_a?(Array) ? :each_with_index : :each_pair valid_values.send(method_for_enumerating) do |key, value| result.push [value, key] end result end
Private Instance Methods
Converts FormBuilder name to ID, see form_name_to_id
# File lib/cck_forms/parameter_type_class/base.rb, line 267 def form_builder_name_to_id(form_builder, suffix = '') form_name_to_id([form_builder.options[:namespace], form_builder.object_name].compact.join('_') + suffix) end
See ClassMethod.form_name_to_id
# File lib/cck_forms/parameter_type_class/base.rb, line 262 def form_name_to_id(name) self.class.form_name_to_id name end
options = value
# File lib/cck_forms/parameter_type_class/base.rb, line 257 def set_value_in_hash(options) options[:value] = value unless options.has_key? :value end